- Added enums to select values

This commit is contained in:
Dave M. 2026-07-01 12:54:53 -04:00
parent 363da77669
commit 9ddb93b847
2 changed files with 13 additions and 3 deletions

View File

@ -79,13 +79,23 @@ class UiSelect extends UiElement implements Extension {
return $option; return $option;
} }
protected function isSelected($check, $value, bool $strict = true) : bool protected function isSelected($check, mixed $value, bool $strict = true) : bool
{ {
if ( is_array($value) ) { if ( is_array($value) ) {
return in_array($check, $value, $strict); return in_array($check, array_map(fn($e) => $this->normalizeValue($e), $value), $strict);
} }
$value = $this->normalizeValue($value);
return $strict ? $check === $value : $check == $value; return $strict ? $check === $value : $check == $value;
} }
protected function normalizeValue(mixed $value) : mixed
{
if ($value instanceof \BackedEnum) {
return $value->value;
}
return $value;
}
} }

View File

@ -218,7 +218,7 @@ class FormContext implements FormContextInterface
is_null($value) => null, is_null($value) => null,
enum_exists($e) => ( $value instanceof \BackedEnum ? $value : $e::tryFrom($value) ) ?? throw new \InvalidArgumentException(sprintf("Field '\$$property' awaiting values : '%s'.", implode("', '", array_map(fn($e) => $e->value, $e::cases())))), enum_exists($e) => ( $value instanceof \BackedEnum ? $value : $e::tryFrom($value) ) ?? throw new \InvalidArgumentException(sprintf("Field '\$$property' awaiting values : '%s'.", implode("', '", array_map(fn($e) => $e->value, $e::cases())))),
function_exists($e) => $e($value), function_exists($e) => $e($value),
class_exists($e) => new $e($value), class_exists($e) => new $e(...$value),
interface_exists($e) => throw new \InvalidArgumentException("There is no way provided to match an interface object yet."), interface_exists($e) => throw new \InvalidArgumentException("There is no way provided to match an interface object yet."),
default => $value default => $value
}; };