- Added some checkup on constructor properties
This commit is contained in:
parent
3d412d6a46
commit
94903d6f05
@ -29,12 +29,19 @@ class FormContext implements FormContextInterface
|
||||
|
||||
protected array $formContextCatchedMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE', ];
|
||||
|
||||
protected array $formConstructorProperties = [];
|
||||
|
||||
public ? ResponseInterface $formContextResponse = null;
|
||||
|
||||
public function __construct(
|
||||
public ServerRequestInterface $formContextRequest,
|
||||
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() ?: [];
|
||||
|
||||
if ( ! $this->formContextValues ) {
|
||||
@ -194,7 +201,7 @@ class FormContext implements FormContextInterface
|
||||
# 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));
|
||||
|
||||
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
|
||||
@ -206,7 +213,7 @@ class FormContext implements FormContextInterface
|
||||
{
|
||||
$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())))),
|
||||
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),
|
||||
class_exists($e) => new $e($value),
|
||||
interface_exists($e) => throw new \InvalidArgumentException("There is no way provided to match an interface object yet."),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user