Skip to content

Commit

Permalink
Rearrange Link class members
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Nov 28, 2024
1 parent 27f0dcf commit 6688e61
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions src/fields/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,59 +162,6 @@ private static function types(): array
*/
private array $_linkTypes;

/**
* Returns the link types available to the field.
*
* @return array<string,BaseLinkType>
*/
public function getLinkTypes(): array
{
if (!isset($this->_linkTypes)) {
$this->_linkTypes = [];
$types = self::types();

foreach ($this->types as $typeId) {
if (isset($types[$typeId])) {
$this->_linkTypes[$typeId] = Component::createComponent([
'type' => $types[$typeId],
'settings' => $this->typeSettings[$typeId] ?? [],
], BaseLinkType::class);
}
}
}

return $this->_linkTypes;
}

private function resolveType(string $value): string
{
$linkTypes = $this->getLinkTypes();

// check URL last, if it's selected
$urlType = ArrayHelper::remove($linkTypes, UrlType::id());
if ($urlType) {
$linkTypes[UrlType::id()] = $urlType;
}

foreach ($linkTypes as $id => $linkType) {
if ($linkType->supports($value)) {
return $id;
}
}

// See if any unselected types support it
foreach (self::types() as $typeId => $type) {
if (!isset($linkTypes[$typeId]) && $type !== UrlType::class) {
$linkType = Component::createComponent($type, BaseLinkType::class);
if ($linkType->supports($value)) {
return $linkType::id();
}
}
}

return UrlType::id();
}

/**
* @var string[] Allowed link types
*/
Expand Down Expand Up @@ -276,6 +223,59 @@ protected function defineRules(): array
return $rules;
}

/**
* Returns the link types available to the field.
*
* @return array<string,BaseLinkType>
*/
public function getLinkTypes(): array
{
if (!isset($this->_linkTypes)) {
$this->_linkTypes = [];
$types = self::types();

foreach ($this->types as $typeId) {
if (isset($types[$typeId])) {
$this->_linkTypes[$typeId] = Component::createComponent([
'type' => $types[$typeId],
'settings' => $this->typeSettings[$typeId] ?? [],
], BaseLinkType::class);
}
}
}

return $this->_linkTypes;
}

private function resolveType(string $value): string
{
$linkTypes = $this->getLinkTypes();

// check URL last, if it's selected
$urlType = ArrayHelper::remove($linkTypes, UrlType::id());
if ($urlType) {
$linkTypes[UrlType::id()] = $urlType;
}

foreach ($linkTypes as $id => $linkType) {
if ($linkType->supports($value)) {
return $id;
}
}

// See if any unselected types support it
foreach (self::types() as $typeId => $type) {
if (!isset($linkTypes[$typeId]) && $type !== UrlType::class) {
$linkType = Component::createComponent($type, BaseLinkType::class);
if ($linkType->supports($value)) {
return $linkType::id();
}
}
}

return UrlType::id();
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit 6688e61

Please sign in to comment.