- Fixed class attributes on form which was overwritting it instead of adding to base class
69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Picea\Ui\Method;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
class FormHandler {
|
|
public bool $sent = false;
|
|
|
|
public ? bool $executionStatus = null;
|
|
|
|
public FormContext $context;
|
|
|
|
protected ServerRequestInterface $request;
|
|
|
|
protected FormInterface $form;
|
|
|
|
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->context = $this->context;
|
|
|
|
$this->formSent();
|
|
$this->initialize();
|
|
}
|
|
|
|
public function formSent() : void
|
|
{
|
|
if ( false !== $this->context->formSent = $this->sent ) {
|
|
if ( $this->context->formName ?? false ) {
|
|
$this->sent = $this->context->formSent = (bool) ( $this->request->getParsedBody()['picea-ui-form'][$this->context->formName] ?? false );
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function initialize() : void
|
|
{
|
|
$this->form->initialize($this->context);
|
|
|
|
if ( $this->sent ) {
|
|
if ( $this->form->validate($this->context) ) {
|
|
$this->executionStatus = $this->form->execute($this->context);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function requestSent() : bool
|
|
{
|
|
return in_array($this->request->getMethod(), [
|
|
"DELETE", "PATCH", "POST", "PUT",
|
|
]);
|
|
}
|
|
|
|
protected function honeyPot() : bool
|
|
{
|
|
$this->request->getServerParams();
|
|
}
|
|
}
|