Skip to content

Commit

Permalink
Enum add Hidden attr and hide them in ListField #1096
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Sep 17, 2024
1 parent e0b18ac commit c06432d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/form/src/Field/Concern/ListOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Windwalker\DOM\HTMLFactory;
use Windwalker\Utilities\Assert\TypeAssert;
use Windwalker\Utilities\Contract\LanguageInterface;
use Windwalker\Utilities\Enum\EnumMetaInterface;
use Windwalker\Utilities\Enum\EnumTranslatableInterface;

/**
Expand Down Expand Up @@ -135,6 +136,10 @@ public function registerFromEnums(iterable|string $enums, ?LanguageInterface $la
$options = [];

foreach ($enums as $enum) {
if (($enum instanceof EnumMetaInterface) && $enum->isHidden()) {
continue;
}

if ($enum instanceof EnumTranslatableInterface) {
$options[$enum->getValue()] = $enum->getTitle($lang) ?: $enum->getKey();
} elseif ($enum instanceof Enum) {
Expand Down
17 changes: 17 additions & 0 deletions packages/utilities/src/Attributes/Enum/Hidden.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Windwalker\Utilities\Attributes\Enum;

/**
* The EnumString class.
*/
#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)]
class Hidden
{
public function __construct()
{
//
}
}
7 changes: 7 additions & 0 deletions packages/utilities/src/Enum/EnumBCTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
*/
trait EnumBCTrait
{
/**
* @param $variable
*
* @return bool
*
* @deprecated Use 8.2 enums compare
*/
public function equals($variable = null): bool
{
return $variable === $this;
Expand Down
6 changes: 6 additions & 0 deletions packages/utilities/src/Enum/EnumMetaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public function getTitle(?LanguageInterface $lang = null, ...$args): string;

public static function getTitles(): array;

public static function fromTitle(string $title): self;

public static function tryFromTitle(string $title): ?self;

public function getIcon(): string;

public static function getIcons(): array;
Expand All @@ -28,4 +32,6 @@ public function getMeta(): array;
public static function getMetas(): array;

public static function maxLength(): int;

public function isHidden(): bool;
}
30 changes: 30 additions & 0 deletions packages/utilities/src/Enum/EnumMetaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Windwalker\Attributes\AttributesAccessor;
use Windwalker\Utilities\Attributes\Enum\Color;
use Windwalker\Utilities\Attributes\Enum\Hidden;
use Windwalker\Utilities\Attributes\Enum\Icon;
use Windwalker\Utilities\Attributes\Enum\Meta;
use Windwalker\Utilities\Attributes\Enum\Title;
Expand Down Expand Up @@ -44,6 +45,28 @@ public static function getTitles(): array
return $values;
}

public static function fromTitle(string $title): self
{
$titles = self::getTitles();

$value = array_search($title, $titles);

return self::wrap($value);
}

public static function tryFromTitle(string $title): ?self
{
$titles = self::getTitles();

$value = array_search($title, $titles);

if ($value === false) {
return null;
}

return self::wrap($value);
}

public function getIcon(): string
{
return $this->getAttr(Icon::class)?->getIcon() ?? '';
Expand Down Expand Up @@ -92,6 +115,13 @@ public static function getMetas(): array
return $values;
}

public function isHidden(): bool
{
$hidden = $this->getAttr(Hidden::class);

return $hidden instanceof Hidden;
}

/**
* @template T
*
Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/src/Enum/EnumPhpAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function tryWrap(mixed $value): ?static
}

/**
* @return array<static>
* @return array<string, static>
*
* @throws \ReflectionException
*/
Expand Down

0 comments on commit c06432d

Please sign in to comment.