- First WIP on form handling
This commit is contained in:
1 parent
cb2caf895e
commit
a8a99f1852
6 files changed
+173
-38
No files matched your search
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Ui\Method;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface,
|
||||
Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class FormContext implements FormContextInterface
|
||||
{
|
||||
public bool $formSent;
|
||||
|
||||
public array $values;
|
||||
|
||||
public array $messages = [];
|
||||
|
||||
public ServerRequestInterface $request;
|
||||
|
||||
public ? ResponseInterface $response = null;
|
||||
|
||||
public function __construct(ServerRequestInterface $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->values = $request->getParsedBody() ?: [];
|
||||
}
|
||||
|
||||
public function valid() : bool
|
||||
{
|
||||
foreach($this->messages as $message) {
|
||||
if ( $message->isError() ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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, $_SESSION);
|
||||
}
|
||||
|
||||
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 $_SESSION[$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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user