mirror of
https://github.com/archtechx/enums.git
synced 2025-12-13 21:04:04 +00:00
Metadata
This commit is contained in:
parent
cc5bba1912
commit
7b0871db00
7 changed files with 389 additions and 46 deletions
43
src/Metadata.php
Normal file
43
src/Metadata.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace ArchTech\Enums;
|
||||
|
||||
use ArchTech\Enums\Meta\MetaProperty;
|
||||
use ValueError;
|
||||
|
||||
trait Metadata
|
||||
{
|
||||
/** Try to get the first case with this meta property value. */
|
||||
public static function tryFromMeta(MetaProperty $metaProperty): static|null
|
||||
{
|
||||
foreach (static::cases() as $case) {
|
||||
if (Meta\Reflection::metaValue($metaProperty::class, $case) === $metaProperty->value) {
|
||||
return $case;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Get the first case with this meta property value. */
|
||||
public static function fromMeta(MetaProperty $metaProperty): static
|
||||
{
|
||||
return static::tryFromMeta($metaProperty) ?? throw new ValueError(
|
||||
'Enum ' . static::class . ' does not have a case with a meta property "' .
|
||||
$metaProperty::class . '" of value "' . $metaProperty->value . '"'
|
||||
);
|
||||
}
|
||||
|
||||
public function __call(string $property, $arguments): mixed
|
||||
{
|
||||
$metaProperties = Meta\Reflection::metaProperties($this);
|
||||
|
||||
foreach ($metaProperties as $metaProperty) {
|
||||
if ($metaProperty::method() === $property) {
|
||||
return Meta\Reflection::metaValue($metaProperty, $this);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue