- Added some checkup on constructor properties

This commit is contained in:
Dave Mc Nicoll 2026-05-21 15:00:37 +00:00
parent 3d412d6a46
commit 94903d6f05

View File

@ -29,12 +29,19 @@ class FormContext implements FormContextInterface
protected array $formContextCatchedMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE', ]; protected array $formContextCatchedMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE', ];
protected array $formConstructorProperties = [];
public ? ResponseInterface $formContextResponse = null; public ? ResponseInterface $formContextResponse = null;
public function __construct( public function __construct(
public ServerRequestInterface $formContextRequest, public ServerRequestInterface $formContextRequest,
public readonly ? string $formName = null public readonly ? string $formName = null
) { ) {
# From constructor
$reflection = new \ReflectionClass($this);
$this->formConstructorProperties = array_map(fn($e) => strtolower($e->getName()), array_filter($reflection->getConstructor()->getParameters(), fn($e) => $e->isPromoted()));
$this->formContextValues = $formContextRequest->getParsedBody() ?: []; $this->formContextValues = $formContextRequest->getParsedBody() ?: [];
if ( ! $this->formContextValues ) { if ( ! $this->formContextValues ) {
@ -194,7 +201,7 @@ class FormContext implements FormContextInterface
# Skipping overrides of this particular class vars as a security measure # Skipping overrides of this particular class vars as a security measure
static $skipping = array_keys(array_change_key_case(get_class_vars(FormContext::class), CASE_LOWER)); static $skipping = array_keys(array_change_key_case(get_class_vars(FormContext::class), CASE_LOWER));
return ! in_array(strtolower($property), $skipping) && property_exists($this, $property); return ! in_array(strtolower($property), array_merge($skipping, $this->formConstructorProperties)) && property_exists($this, $property);
} }
protected function definedProperty(string $property) : bool protected function definedProperty(string $property) : bool
@ -206,7 +213,7 @@ class FormContext implements FormContextInterface
{ {
$cast = fn($e) => match(true) { $cast = fn($e) => match(true) {
is_null($value) => null, 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())))), 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."),