major update
This commit is contained in:
38
lib/Domain/Taxonomy/SupportsStringTaxonomy.php
Normal file
38
lib/Domain/Taxonomy/SupportsStringTaxonomy.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Domain\Taxonomy;
|
||||
|
||||
trait SupportsStringTaxonomy
|
||||
{
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function values(): array
|
||||
{
|
||||
return array_map(
|
||||
static fn (self $case): string => $case->value,
|
||||
self::cases()
|
||||
);
|
||||
}
|
||||
|
||||
public static function tryNormalize(string $value): ?self
|
||||
{
|
||||
$value = strtolower(trim($value));
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (self::cases() as $case) {
|
||||
if (strtolower($case->value) === $value) {
|
||||
return $case;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function normalizeOr(string $value, self $fallback): self
|
||||
{
|
||||
return self::tryNormalize($value) ?? $fallback;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user