Skip to content

Commit

Permalink
Fix typing issues in ElementSourceValidator. Bump to 2.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed Aug 2, 2022
1 parent e01088a commit 58c0f55
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for LinkMate

## 2.2.2 - 2022-08-02
### Fixed
- Fixes typing issues in ElementSourceValidator

## 2.2.1 - 2022-05-02
### Fixed
- Fixes an issue where the site menu would always display in element selector modals, regardless of whether the "Show site menu" option was actually checked or not.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vaersaagod/linkmate",
"description": "Let's hook you up, mate!",
"type": "craft-plugin",
"version": "2.2.1",
"version": "2.2.2",
"keywords": [
"craft",
"cms",
Expand Down
6 changes: 3 additions & 3 deletions src/LinkMate.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ private function createDefaultLinkTypes(): array
]),
'category' => new ElementLinkType([
'displayGroup' => 'Craft CMS',
'elementType' => Category::class
'elementType' => Category::class,
]),
'entry' => new ElementLinkType([
'displayGroup' => 'Craft CMS',
'elementType' => Entry::class
'elementType' => Entry::class,
]),
'user' => new ElementLinkType([
'displayGroup' => 'Craft CMS',
'elementType' => User::class
'elementType' => User::class,
]),
'site' => new SiteLinkType([
'displayGroup' => 'Craft CMS',
Expand Down
32 changes: 10 additions & 22 deletions src/models/ElementLinkType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
use craft\base\ElementInterface;
use craft\errors\SiteNotFoundException;
use craft\helpers\Html;

use Exception;
use Throwable;

use vaersaagod\linkmate\fields\LinkField;
use vaersaagod\linkmate\utilities\ElementSourceValidator;
use vaersaagod\linkmate\utilities\Url;

use yii\base\Model;

/**
Expand All @@ -24,25 +27,12 @@
*/
class ElementLinkType extends Model implements LinkTypeInterface
{
public ElementInterface|string $elementType;
public string $displayGroup = 'Common';

/**
* ElementLinkType constructor.
*
* @param string|array $elementType
* @param array $options
*/
public function __construct($elementType, array $options = [])
{
if (is_array($elementType)) {
$options = $elementType;
} else {
$options['elementType'] = $elementType;
}

parent::__construct($options);
}
/** @var string The fully qualified element class path, e.g. craft\\elements\\Entry */
public string $elementType;

/** @var string */
public string $displayGroup = 'Common';

/**
* @return array
Expand All @@ -61,17 +51,15 @@ public function getDefaultSettings(): array
*/
public function getDisplayName(): string
{
$elementType = $this->elementType;
return $elementType::displayName();
return $this->elementType::displayName();
}

/**
* @return string
*/
public function getPluralDisplayName(): string
{
$elementType = $this->elementType;
return $elementType::pluralDisplayName();
return $this->elementType::pluralDisplayName();
}

/**
Expand Down
19 changes: 10 additions & 9 deletions src/utilities/ElementSourceValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace vaersaagod\linkmate\utilities;

use craft\base\ElementInterface;
use Exception;
use Throwable;

use yii\helpers\ArrayHelper;

/**
Expand All @@ -26,15 +26,15 @@ class ElementSourceValidator
/**
* ElementSourceValidator constructor.
*
* @param ElementInterface $elementType
* @param string $elementType The fully qualified element class path, e.g. craft\\elements\\Entry
*
* @throws Exception
*/
public function __construct(ElementInterface $elementType)
public function __construct(string $elementType)
{
$idPath = self::getElementIdPath($elementType);
if (is_null($idPath)) {
throw new Exception('Unsupported element type: '.(string)$elementType);
throw new Exception('Unsupported element type: ' . (string)$elementType);
}

$availableSources = [];
Expand Down Expand Up @@ -112,12 +112,12 @@ private function validateSource(string $originalSource): ?string
}

/**
* @param ElementInterface $elementType
* @param array $sources
* @param string $elementType The fully qualified element class path, e.g. craft\\elements\\Entry
* @param array $sources
*
* @return array
*/
public static function apply(ElementInterface $elementType, array $sources): array
public static function apply(string $elementType, array $sources): array
{
try {
if (!array_key_exists($elementType, self::$validators)) {
Expand All @@ -126,17 +126,18 @@ public static function apply(ElementInterface $elementType, array $sources): arr

return self::$validators[(string)$elementType]->validate($sources);
} catch (Throwable) {

}

return $sources;
}

/**
* @param ElementInterface $elementType
* @param string $elementType The fully qualified element class path, e.g. craft\\elements\\Entry
*
* @return array|null
*/
public static function getElementIdPath(ElementInterface $elementType): ?array
public static function getElementIdPath(string $elementType): ?array
{
return match ($elementType) {
'craft\\elements\\Asset' => ['criteria', 'folderId'],
Expand Down

0 comments on commit 58c0f55

Please sign in to comment.