- Added casting of values from FormContext set property
This commit is contained in:
parent
a1c51bed10
commit
6e7a8f12fb
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user