104 lines
2.1 KiB
PHP
104 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Picea\Ui\Method;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface,
|
|
Psr\Http\Message\ResponseInterface;
|
|
|
|
class FormContext implements FormContextInterface
|
|
{
|
|
public string $formName;
|
|
|
|
public bool $formSent;
|
|
|
|
public bool $formExecuted = false;
|
|
|
|
public array $values = [];
|
|
|
|
public array $files = [];
|
|
|
|
public array $messages = [];
|
|
|
|
public ServerRequestInterface $request;
|
|
|
|
public ? ResponseInterface $response = null;
|
|
|
|
public function __construct(ServerRequestInterface $request, ? string $formName = null)
|
|
{
|
|
$this->request = $request;
|
|
|
|
if ( $formName ) {
|
|
$this->formName = $formName;
|
|
}
|
|
|
|
$this->values = $request->getParsedBody() ?: [];
|
|
|
|
$this->files = $request->getUploadedFiles() ?: [];
|
|
}
|
|
|
|
public function valid() : bool
|
|
{
|
|
foreach($this->messages as $message) {
|
|
if ( $message->isError() ) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function executed() : bool
|
|
{
|
|
return $this->formExecuted;
|
|
}
|
|
|
|
public function formSent() : bool
|
|
{
|
|
return $this->formSent;
|
|
}
|
|
|
|
public function __set($key, $value)
|
|
{
|
|
return $this->set($key, $value);
|
|
}
|
|
|
|
public function __get($key)
|
|
{
|
|
return $this->get($key);
|
|
}
|
|
|
|
public function __isset($key)
|
|
{
|
|
return array_key_exists($key, $this->values);
|
|
}
|
|
|
|
public function __unset($key)
|
|
{
|
|
$this->delete($key);
|
|
}
|
|
|
|
public function get(string $key, $default = null)
|
|
{
|
|
return $this->has($key) ? $this->values[$key] : $default;
|
|
}
|
|
|
|
public function set(string $key, $value)
|
|
{
|
|
return $this->values[$key] = $value;
|
|
}
|
|
|
|
public function delete(string $key) : void
|
|
{
|
|
unset($this->values[$key]);
|
|
}
|
|
|
|
public function has(string $key) : bool
|
|
{
|
|
return array_key_exists($key, $this->values);
|
|
}
|
|
|
|
public function pushMessage(FormMessage $message) : void
|
|
{
|
|
$this->messages[] = $message;
|
|
}
|
|
} |