- Added a new form() method to ease form handling
This commit is contained in:
1 parent
94e42747a4
commit
9c8a28d294
2 files changed
+80
-6
No files matched your search
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Ui\Method;
|
||||
|
||||
use Picea\Extension\Extension,
|
||||
Picea\Extension\ExtensionTrait;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class Form implements Extension {
|
||||
use ExtensionTrait;
|
||||
|
||||
public array $tokens;
|
||||
|
||||
public string $token;
|
||||
|
||||
public function __construct(Context $context) {
|
||||
$this->register($context);
|
||||
}
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string { }
|
||||
|
||||
public function register(Context $context) : void
|
||||
{
|
||||
$context->pushFunction("form", [ $this, 'formClass' ]);
|
||||
}
|
||||
|
||||
public function form_csrf(string $field, string $value) {
|
||||
$values = $this->session("View.form.csrf.$field") ?: [];
|
||||
|
||||
# keeps 20 (from config) latest CSRF key for this form into session,
|
||||
# allowing more than one tab opened and preventing information loss
|
||||
if ( count($values) >= 20 ) {
|
||||
#array_shift($values);
|
||||
}
|
||||
|
||||
$values[] = $value;
|
||||
|
||||
$this->session("View.form.csrf.$field", $values);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function formClass(ServerRequestInterface $request) {
|
||||
return new class($request) {
|
||||
|
||||
public bool $sent = false;
|
||||
|
||||
protected ServerRequestInterface $request;
|
||||
|
||||
public function __construct(ServerRequestInterface $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->sent = $this->requestSent();
|
||||
}
|
||||
|
||||
protected function requestSent() : bool
|
||||
{
|
||||
return in_array($this->request->getMethod(), [
|
||||
"DELETE", "PATCH", "POST", "PUT",
|
||||
]);
|
||||
}
|
||||
|
||||
protected function honeyPot() : bool
|
||||
{
|
||||
$this->request->getServerParams();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user