Compare commits

...

1 Commits

Author SHA1 Message Date
Dave Mc Nicoll
33e77b5129 - Added getRequest() method to replace direct property access 2025-05-21 18:38:25 +00:00
3 changed files with 60 additions and 30 deletions

View File

@ -36,6 +36,6 @@ class Form implements Extension, FunctionExtension {
public function formClass(FormInterface $form, ? FormContext $formContext = null) : FormHandler 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);
} }
} }

View File

@ -7,7 +7,7 @@ use Psr\Http\Message\ServerRequestInterface,
class FormContext implements FormContextInterface class FormContext implements FormContextInterface
{ {
public string $method; public string $formContextMethod;
public string $formName; public string $formName;
@ -17,43 +17,41 @@ class FormContext implements FormContextInterface
public mixed $formExecutionStatus = null; 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) public function __construct(ServerRequestInterface $request, ? string $formName = null)
{ {
$this->request = $request; $this->formContextRequest = $request;
if ( $formName ) { if ( $formName ) {
$this->formName = $formName; $this->formName = $formName;
} }
$this->values = $request->getParsedBody() ?: []; $this->formContextValues = $request->getParsedBody() ?: [];
$this->method = $this->request->getMethod(); if ( ! $this->formContextValues ) {
if ( ! $this->values ) {
$content = mb_convert_encoding((string) $request->getBody(), 'UTF-8'); $content = mb_convert_encoding((string) $request->getBody(), 'UTF-8');
if ( $content && ( $json = json_decode($content, true) ) ) { if ( $content && ( $json = json_decode($content, true) ) ) {
$this->values = $json; $this->formContextValues = $json;
} }
} }
$this->fillValues(); $this->fillValues();
$this->files = $request->getUploadedFiles() ?: []; $this->formContextFiles = $request->getUploadedFiles() ?: [];
$this->initialize(); $this->initialize();
} }
@ -62,7 +60,7 @@ class FormContext implements FormContextInterface
public function valid() : bool public function valid() : bool
{ {
foreach($this->messages as $message) { foreach($this->formContextMessages as $message) {
if ( $message->isError() ) { if ( $message->isError() ) {
return false; return false;
} }
@ -78,9 +76,9 @@ class FormContext implements FormContextInterface
public function formSent() : bool 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; $token = $this->get('picea-ui-form')[$this->formName] ?? false;
if ( $token ) { if ( $token ) {
@ -99,9 +97,19 @@ class FormContext implements FormContextInterface
return $this->formSent = $valid; return $this->formSent = $valid;
} }
public function getExecutionStatus() : mixed
{
return $this->formExecutionStatus;
}
public function getRequest() : ServerRequestInterface
{
return $this->formContextRequest;
}
public function requestMethod() : string public function requestMethod() : string
{ {
return $this->method; return $this->formContextRequest->getMethod();
} }
public function __set($key, $value) public function __set($key, $value)
@ -116,7 +124,7 @@ class FormContext implements FormContextInterface
public function __isset($key) public function __isset($key)
{ {
return array_key_exists($key, $this->values); return array_key_exists($key, $this->formContextValues);
} }
public function __unset($key) public function __unset($key)
@ -126,38 +134,56 @@ class FormContext implements FormContextInterface
public function get(string $key, $default = null) 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) 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 public function delete(string $key) : void
{ {
unset($this->values[$key]); unset($this->formContextValues[$key]);
} }
public function has(string $key) : bool 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 public function pushMessage(FormMessage $message) : void
{ {
$this->messages[] = $message; $this->formContextMessages[] = $message;
} }
protected function fillValues() : void 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) { foreach($this->formContextValues as $property => $value) {
if ( ! in_array(strtolower($property), $skipping) && property_exists($this, $property)) { if ($this->canWriteProperty($property)) {
$this->$property = $value; $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);
}
} }

View File

@ -2,10 +2,14 @@
namespace Picea\Ui\Method; namespace Picea\Ui\Method;
use Psr\Http\Message\ServerRequestInterface;
interface FormContextInterface { interface FormContextInterface {
public function valid() : bool; public function valid() : bool;
public function formSent() : bool; public function formSent() : bool;
public function requestMethod() : string; public function requestMethod() : string;
public function getRequest() : ServerRequestInterface;
} }