- Added request method to context

This commit is contained in:
Dave Mc Nicoll 2024-05-31 12:28:34 +00:00
parent 804deb69a2
commit 0a7b1cdc12
2 changed files with 16 additions and 3 deletions

View File

@ -7,8 +7,10 @@ use Psr\Http\Message\ServerRequestInterface,
class FormContext implements FormContextInterface
{
public string $method;
public string $formName;
public bool $formSent = false;
public bool $formExecuted = false;
@ -22,7 +24,9 @@ class FormContext implements FormContextInterface
public array $messages = [];
public bool $skipCsrf = false;
protected array $catchedMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE', ];
public ServerRequestInterface $request;
public ? ResponseInterface $response = null;
@ -37,6 +41,8 @@ class FormContext implements FormContextInterface
$this->values = $request->getParsedBody() ?: [];
$this->method = $this->request->getMethod();
if ( ! $this->values ) {
$content = mb_convert_encoding((string) $request->getBody(), 'UTF-8');
@ -66,7 +72,7 @@ class FormContext implements FormContextInterface
public function formSent() : bool
{
$valid = in_array($this->request->getMethod(), [ 'POST', 'PUT', 'PATCH', 'DELETE', ]);
$valid = in_array($this->method, $this->catchedMethods);
if ( ! $this->skipCsrf && ($this->formName ?? false) ) {
$token = $this->get('picea-ui-form')[$this->formName] ?? false;
@ -87,6 +93,11 @@ class FormContext implements FormContextInterface
return $this->formSent = $valid;
}
public function requestMethod() : string
{
return $this->method;
}
public function __set($key, $value)
{
return $this->set($key, $value);

View File

@ -6,4 +6,6 @@ interface FormContextInterface {
public function valid() : bool;
public function formSent() : bool;
public function requestMethod() : string;
}