From 33e77b5129d7f21b8a3e42529dfae932fba1ebeb Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 21 May 2025 18:38:25 +0000 Subject: [PATCH] - Added getRequest() method to replace direct property access --- src/Method/Form.php | 2 +- src/Method/FormContext.php | 84 +++++++++++++++++++---------- src/Method/FormContextInterface.php | 4 ++ 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/src/Method/Form.php b/src/Method/Form.php index c53042e..3514c3c 100644 --- a/src/Method/Form.php +++ b/src/Method/Form.php @@ -36,6 +36,6 @@ class Form implements Extension, FunctionExtension { public function formClass(FormInterface $form, ? FormContext $formContext = null) : FormHandler { - return new FormHandler($formContext ? $formContext->request : $this->request, $form, $formContext); + return new FormHandler($formContext ? $formContext->getRequest() : $this->request, $form, $formContext); } } diff --git a/src/Method/FormContext.php b/src/Method/FormContext.php index a08d86d..5b4e05d 100644 --- a/src/Method/FormContext.php +++ b/src/Method/FormContext.php @@ -7,7 +7,7 @@ use Psr\Http\Message\ServerRequestInterface, class FormContext implements FormContextInterface { - public string $method; + public string $formContextMethod; public string $formName; @@ -17,43 +17,41 @@ class FormContext implements FormContextInterface public mixed $formExecutionStatus = null; - public array $values = []; + public array $formContextValues = []; - public array $files = []; + public array $formContextFiles = []; - public array $messages = []; + public array $formContextMessages = []; - public bool $skipCsrf = false; + public bool $formSkipCsrf = false; - protected array $catchedMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE', ]; + protected array $formContextCatchedMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE', ]; - public ServerRequestInterface $request; + public ServerRequestInterface $formContextRequest; - public ? ResponseInterface $response = null; + public ? ResponseInterface $formContextResponse = null; public function __construct(ServerRequestInterface $request, ? string $formName = null) { - $this->request = $request; + $this->formContextRequest = $request; if ( $formName ) { $this->formName = $formName; } - $this->values = $request->getParsedBody() ?: []; + $this->formContextValues = $request->getParsedBody() ?: []; - $this->method = $this->request->getMethod(); - - if ( ! $this->values ) { + if ( ! $this->formContextValues ) { $content = mb_convert_encoding((string) $request->getBody(), 'UTF-8'); if ( $content && ( $json = json_decode($content, true) ) ) { - $this->values = $json; + $this->formContextValues = $json; } } $this->fillValues(); - $this->files = $request->getUploadedFiles() ?: []; + $this->formContextFiles = $request->getUploadedFiles() ?: []; $this->initialize(); } @@ -62,7 +60,7 @@ class FormContext implements FormContextInterface public function valid() : bool { - foreach($this->messages as $message) { + foreach($this->formContextMessages as $message) { if ( $message->isError() ) { return false; } @@ -78,9 +76,9 @@ class FormContext implements FormContextInterface public function formSent() : bool { - $valid = in_array($this->method, $this->catchedMethods); + $valid = in_array($this->requestMethod(), $this->formContextCatchedMethods); - if ( ! $this->skipCsrf && ($this->formName ?? false) ) { + if ( ! $this->formSkipCsrf && ($this->formName ?? false) ) { $token = $this->get('picea-ui-form')[$this->formName] ?? false; if ( $token ) { @@ -99,9 +97,19 @@ class FormContext implements FormContextInterface return $this->formSent = $valid; } + public function getExecutionStatus() : mixed + { + return $this->formExecutionStatus; + } + + public function getRequest() : ServerRequestInterface + { + return $this->formContextRequest; + } + public function requestMethod() : string { - return $this->method; + return $this->formContextRequest->getMethod(); } public function __set($key, $value) @@ -116,7 +124,7 @@ class FormContext implements FormContextInterface public function __isset($key) { - return array_key_exists($key, $this->values); + return array_key_exists($key, $this->formContextValues); } public function __unset($key) @@ -126,38 +134,56 @@ class FormContext implements FormContextInterface public function get(string $key, $default = null) { - return $this->has($key) ? $this->values[$key] : $default; + return $this->has($key) ? $this->formContextValues[$key] : $default; } public function set(string $key, $value) { - return $this->values[$key] = $value; + if ($this->canWriteProperty($key)) { + $this->$key = $value; + } + + return $this->formContextValues[$key] = $value; } + public function sets(array $values) : void + { + foreach($values as $key =>$value) { + $this->set($key, $value); + } + } + + public function delete(string $key) : void { - unset($this->values[$key]); + unset($this->formContextValues[$key]); } public function has(string $key) : bool { - return array_key_exists($key, $this->values); + return array_key_exists($key, $this->formContextValues); } public function pushMessage(FormMessage $message) : void { - $this->messages[] = $message; + $this->formContextMessages[] = $message; } protected function fillValues() : void { - # Skipping overrides of this particular class vars as a security measure - $skipping = array_keys(array_change_key_case(get_class_vars(FormContext::class), CASE_LOWER)); - foreach($this->values as $property => $value) { - if ( ! in_array(strtolower($property), $skipping) && property_exists($this, $property)) { + foreach($this->formContextValues as $property => $value) { + if ($this->canWriteProperty($property)) { $this->$property = $value; } } } + + protected function canWriteProperty(string $property) : bool + { + # 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); + } } diff --git a/src/Method/FormContextInterface.php b/src/Method/FormContextInterface.php index e0ca0c4..f5cc575 100644 --- a/src/Method/FormContextInterface.php +++ b/src/Method/FormContextInterface.php @@ -2,10 +2,14 @@ namespace Picea\Ui\Method; +use Psr\Http\Message\ServerRequestInterface; + interface FormContextInterface { public function valid() : bool; public function formSent() : bool; public function requestMethod() : string; + + public function getRequest() : ServerRequestInterface; } \ No newline at end of file