From 00e5a0380a15485179276a0c06f29f84fc8759cf Mon Sep 17 00:00:00 2001 From: Dave M Date: Tue, 14 Jul 2026 15:19:25 +0000 Subject: [PATCH] - Added some more flexibility to castValue method --- src/Method/Context/FormContextCastableInterface.php | 8 ++++++++ src/Method/FormContext.php | 11 +++++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/Method/Context/FormContextCastableInterface.php diff --git a/src/Method/Context/FormContextCastableInterface.php b/src/Method/Context/FormContextCastableInterface.php new file mode 100644 index 0000000..8003f95 --- /dev/null +++ b/src/Method/Context/FormContextCastableInterface.php @@ -0,0 +1,8 @@ + match(true) { + is_a($e, Context\FormContextCastableInterface::class, true) => $e::instance($value), + $e === "array" => (function(array|string $value) use ($property) { + if (is_string($value)) { + return json_validate($value) ? json_decode($value, true) ?? [] : throw new \InvalidArgumentException("Field '$property' is awaiting JSON encoded content."); + } + + return $value; + })($value), 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())))), function_exists($e) => $e($value), @@ -233,6 +241,9 @@ class FormContext implements FormContextInterface return $cast($type->getName()); } } + elseif ($types->isBuiltin()) { + return $cast($types->getName()); + } return $value; }