From 6e7a8f12fbff2a438cfd3e0bc183c2db2f0e7779 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 27 Oct 2025 18:32:56 +0000 Subject: [PATCH] - Added casting of values from FormContext set property --- src/Method/FormContext.php | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Method/FormContext.php b/src/Method/FormContext.php index e154513..763ae43 100644 --- a/src/Method/FormContext.php +++ b/src/Method/FormContext.php @@ -9,8 +9,6 @@ class FormContext implements FormContextInterface { public string $formContextMethod; - public string $formName; - public bool $formSent = false; public bool $formExecuted = false; @@ -29,22 +27,21 @@ class FormContext implements FormContextInterface protected array $formContextCatchedMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE', ]; - public ServerRequestInterface $formContextRequest; - public ? ResponseInterface $formContextResponse = null; - public function __construct(ServerRequestInterface $request, ? string $formName = null) + public function __construct( + public ServerRequestInterface $formContextRequest, + ? string $formName = null + ) { - $this->formContextRequest = $request; - if ( $formName ) { $this->formName = $formName; } - $this->formContextValues = $request->getParsedBody() ?: []; + $this->formContextValues = $formContextRequest->getParsedBody() ?: []; if ( ! $this->formContextValues ) { - $content = mb_convert_encoding((string) $request->getBody(), 'UTF-8'); + $content = mb_convert_encoding((string) $formContextRequest->getBody(), 'UTF-8'); if ( $content && ( $json = json_decode($content, true) ) ) { $this->formContextValues = $json; @@ -53,7 +50,7 @@ class FormContext implements FormContextInterface $this->fillValues(); - $this->formContextFiles = $request->getUploadedFiles() ?: []; + $this->formContextFiles = $formContextRequest->getUploadedFiles() ?: []; $this->initialize(); } @@ -198,16 +195,23 @@ class FormContext implements FormContextInterface protected function castValue(string $property, mixed $value) : mixed { - $isEnum = fn($e) => enum_exists($e) ? $e::tryFrom($value) : $value; + $cast = fn($e) => match(true) { + is_null($value) => null, + enum_exists($e) => $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), + class_exists($e) => new $e($value), + interface_exists($e) => throw new \InvalidArgumentException("There is no way provided to match an interface object yet."), + default => $value + }; $types = ( new \ReflectionProperty($this, $property) )->getType(); if ($types instanceof \ReflectionNamedType) { - $value = $isEnum($types->getName()); + $value = $cast($types->getName()); } elseif ($types instanceof \ReflectionIntersectionType || $types instanceof \ReflectionUnionType) { foreach($types->getTypes() as $type) { - $value = $isEnum($type->getName()); + return $value = $cast($type->getName()); } }