68 lines
1.8 KiB
PHP
68 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Picea\Ui\Method;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
class FormHandler {
|
|
const DEFAULT_METHODS = [
|
|
"DELETE", "PATCH", "POST", "PUT",
|
|
];
|
|
|
|
public bool $sent = false;
|
|
|
|
public bool $validateCsrfToken = true;
|
|
|
|
public /* mixed */ $executionStatus = null;
|
|
|
|
public ? bool $validationStatus = null;
|
|
|
|
public FormContext $context;
|
|
|
|
protected ServerRequestInterface $request;
|
|
|
|
protected FormInterface $form;
|
|
|
|
public array $acceptedMethods = self::DEFAULT_METHODS;
|
|
|
|
public function __construct(ServerRequestInterface $request, FormInterface $form, ? FormContextInterface $context = null)
|
|
{
|
|
$this->request = $request;
|
|
$this->sent = $this->requestSent();
|
|
$this->form = $form;
|
|
|
|
if ( $context ) {
|
|
$this->context = $context;
|
|
}
|
|
else {
|
|
$this->context = method_exists($form, 'getContext') ? $form->getContext($request) : new FormContext($request);
|
|
}
|
|
|
|
$this->request->withAttribute('picea.context', $this->context);
|
|
|
|
$this->initialize();
|
|
}
|
|
|
|
protected function initialize() : void
|
|
{
|
|
$this->form->initialize($this->context);
|
|
|
|
if ( $this->sent && $this->context->formSent() ) {
|
|
if ( $this->validationStatus = $this->form->validate($this->context) ) {
|
|
$this->context->formExecutionStatus = $this->executionStatus = $this->form->execute($this->context);
|
|
$this->context->formExecuted = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function requestSent() : bool
|
|
{
|
|
return in_array(strtoupper($this->request->getMethod()), array_map('strtoupper', $this->acceptedMethods));
|
|
}
|
|
|
|
protected function honeyPot() : bool
|
|
{
|
|
$this->request->getServerParams();
|
|
}
|
|
}
|