Compare commits

..

No commits in common. "master" and "tokens_v2" have entirely different histories.

17 changed files with 204 additions and 261 deletions

View File

@ -6,7 +6,7 @@
"authors": [ "authors": [
{ {
"name": "Dave Mc Nicoll", "name": "Dave Mc Nicoll",
"email": "info@mcnd.ca" "email": "mcndave@gmail.com"
} }
], ],
"require": { "require": {
@ -22,14 +22,5 @@
"psr-4": { "psr-4": {
"Picea\\Ui\\": "src/" "Picea\\Ui\\": "src/"
} }
},
"extra": {
"lean": {
"autoload": {
"definitions": [
"meta/definitions.php"
]
}
}
} }
} }

View File

@ -16,19 +16,3 @@ and will typically be rendered such as :
<input class="ui-hidden" type="hidden" name="picea-ui-form[$name]" value="{ random md5 hash }"> <input class="ui-hidden" type="hidden" name="picea-ui-form[$name]" value="{ random md5 hash }">
</form> </form>
``` ```
### Additional options
You can now remove the enctype attribute `no-enctype` or the CSRF token `no-csrf` using those option
on the form tag :
```html
{% ui:form.post.no-enctype.no-csrf "my.form" %} {% endform %}
```
and would render as such :
```html
<form class="ui-form" action="$action" method="$method">
</form>
```

View File

@ -1,27 +0,0 @@
<?php
use function DI\autowire, DI\create, DI\get;
use Picea\{ Method\Request };
use Picea\Extension\{ LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
use Picea\Ui\{ Method, Ui };
return [
Ui::class => autowire(Ui::class),
'picea.extensions' => function(\Psr\Container\ContainerInterface $c) {
return array_merge([
$c->get(LanguageExtension::class),
$c->get(TitleExtension::class),
$c->get(NumberExtension::class),
$c->get(UrlExtension::class),
$c->get(Method\Form::class),
$c->get(Method\Pagination::class),
$c->get(Request::class),
], class_exists(\Taxus\Picea\Extension::class) ? [ $c->get(\Taxus\Picea\Extension::class) ] : [],
array_map(fn($class) => $c->get($class), $c->get(Lean\Lean::class)->getPiceaExtensions())
);
},
];

View File

@ -2,35 +2,20 @@
namespace Picea\Ui\Common; namespace Picea\Ui\Common;
enum UiElementInsertMode class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
{
case append;
case prepend;
}
class UiElement implements \JsonSerializable { static array $config = [];
public const TAG_CONFIG_OPTIONS = [
"!doctype" => [ "attributes" => [ "html" ], "options" => [ "tag-type" => "single" ] ], const INSERT_MODE_APPEND = 1;
"js" => [ "tag" => "script", "attributes" => [ "type" => "text/javascript" ] ], const INSERT_MODE_PREPEND = 2;
"css" => [ "tag" => "link", "attributes" => [ "rel" => "stylesheet" , "type" => "text/css" ] ],
"document" => [ "options" => [ "no-tag" => true ] ], public string $tag = 'div';
"link" => [ "options" => [ "tag-type" => "single" ] ],
"meta" => [ "options" => [ "tag-type" => "single" ] ], public array $attributes = [
"img" => [ "options" => [ "tag-type" => "single" ] ], 'style' => [],
"input" => [ "options" => [ "tag-type" => "single" ] ], 'class' => [],
"br" => [ "options" => [ "tag-type" => "single" ] ],
"hr" => [ "options" => [ "tag-type" => "single" ] ],
"iframe" => [ "options" => [ "tag-type" => "single" ] ],
"area" => [ "options" => [ "tag-type" => "single" ] ],
"col" => [ "options" => [ "tag-type" => "single" ] ],
"frame" => [ "options" => [ "tag-type" => "single" ] ],
"param" => [ "options" => [ "tag-type" => "single" ] ],
"video" => [ "options" => [ "tag-type" => "single" ] ],
"wbr" => [ "options" => [ "tag-type" => "single" ] ]
]; ];
public array $attributes = [ 'style' => [], 'class' => [], ];
public array $childs = []; public array $childs = [];
/** /**
@ -48,13 +33,28 @@ class UiElement implements \JsonSerializable {
public array $options = []; public array $options = [];
public $selected = null;
public string $content = ""; public string $content = "";
public function __construct( protected ?UiQuery $kwery = null;
public string $tag = "div",
) {}
public static function stylesheet(string $href, $attributes = [], $options = []) : self public function __construct(? string $tag = null) {
if ( ! static::$config ) {
static::pushConfigArray( include(dirname(__FILE__) . "/taglist.php") );
}
if ($tag !== null) {
$this->tag = $tag;
}
}
public static function pushConfigArray(array $array) : void
{
static::$config = array_replace_recursive(static::$config, $array);
}
public static function createStylesheet(string $href, $attributes = [], $options = []) : self
{ {
return static::create('link', $attributes + [ return static::create('link', $attributes + [
'href' => $href, 'href' => $href,
@ -63,7 +63,7 @@ class UiElement implements \JsonSerializable {
], [ 'tag-type' => 'single' ] + $options); ], [ 'tag-type' => 'single' ] + $options);
} }
public static function script(string $src, array $attributes = [], array $options = []) : self public static function createScript(string $src, array $attributes = [], array $options = []) : self
{ {
return static::create('script', $attributes + [ return static::create('script', $attributes + [
'src' => $src, 'src' => $src,
@ -71,7 +71,7 @@ class UiElement implements \JsonSerializable {
], $options); ], $options);
} }
public static function php(array $attributes = [], array $options = []) : self public static function createPhp(array $attributes = [], array $options = []) : self
{ {
return static::create('', $attributes, [ return static::create('', $attributes, [
'force-tag-open' => '<?php ', 'force-tag-open' => '<?php ',
@ -108,7 +108,7 @@ class UiElement implements \JsonSerializable {
$obj->attributes($attributes); $obj->attributes($attributes);
} }
if ( false !== ( $custom = static::TAG_CONFIG_OPTIONS[strtolower($tag)] ?? false ) ) { if ( false !== ( $custom = static::$config["tags"][$tag] ?? false ) ) {
if ( $custom['tag'] ?? false ) { if ( $custom['tag'] ?? false ) {
$obj->tag = $custom['tag']; $obj->tag = $custom['tag'];
} }
@ -252,23 +252,23 @@ class UiElement implements \JsonSerializable {
public function append( ...$arguments ) : self public function append( ...$arguments ) : self
{ {
return $this->insert( UiElementInsertMode::append, ...$arguments); return $this->insert( static::INSERT_MODE_APPEND, ...$arguments);
} }
public function prepend( ...$arguments ) : self public function prepend( ...$arguments ) : self
{ {
return $this->insert( UiElementInsertMode::prepend, ...( is_array($arguments) ? array_reverse($arguments) : $arguments) ); return $this->insert( static::INSERT_MODE_PREPEND, ...( is_array($arguments) ? array_reverse($arguments) : $arguments) );
} }
protected function insert(UiElementInsertMode $mode, ...$elements) : self protected function insert(int $mode, ...$elements) : self
{ {
foreach($elements as $item) { foreach($elements as $item) {
switch($mode) { switch($mode) {
case UiElementInsertMode::append: case static::INSERT_MODE_APPEND:
array_push($this->childs, $item); array_push($this->childs, $item);
break; break;
case UiElementInsertMode::prepend: case static::INSERT_MODE_PREPEND:
array_unshift($this->childs, $item); array_unshift($this->childs, $item);
break; break;
} }
@ -327,6 +327,7 @@ class UiElement implements \JsonSerializable {
*/ */
public function css(...$arguments) { public function css(...$arguments) {
foreach($arguments as $item) { foreach($arguments as $item) {
if ( is_array($item) ) { if ( is_array($item) ) {
foreach($item as $key => $value) { foreach($item as $key => $value) {
$this->css($key, $value); $this->css($key, $value);
@ -345,6 +346,10 @@ class UiElement implements \JsonSerializable {
return array_search(strtolower($className), array_map('strtolower', $this->attributes['class']), true); return array_search(strtolower($className), array_map('strtolower', $this->attributes['class']), true);
} }
public function delete() {
}
public function count() { public function count() {
return count($this->childs); return count($this->childs);
} }
@ -359,6 +364,65 @@ class UiElement implements \JsonSerializable {
]; ];
} }
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value) {
if ( is_numeric($offset) ) {
return $this->selected[$offset] = $value;
}
elseif ( is_null($offset) ) {
return $this->childs[] = $value;
}
else {
return $this->childs[$offset] = $value;
}
}
public function offsetExists($query) : bool
{
return count($this->kwery()->find($query)) > 0;
}
public function offsetUnset($query) : void
{
$this->kwery()->find($query)->remove();
}
public function offsetGet($query) : self
{
return $this->kwery()->find($query);
}
public function rewind() : void
{
reset($this->childs);
}
public function current() : self
{
return current($this->childs);
}
public function key() : int
{
return key($this->childs);
}
#[\ReturnTypeWillChange]
public function next() : self
{
return next($this->childs);
}
public function valid() : bool
{
return ! in_array(key($this->childs), [ NULL, FALSE ], true);
}
public static function isNode($obj) : bool
{
return $obj instanceof UiElement;
}
public function __toString() : string public function __toString() : string
{ {
return $this->render(); return $this->render();

View File

@ -17,7 +17,7 @@ class UiMessage extends UiElement implements Extension {
'class' => 'ui-message', 'class' => 'ui-message',
]; ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
{ {
return ""; return "";
} }

View File

@ -11,16 +11,13 @@ class UiPopup extends UiElement implements Extension {
public string $token = "ui:popup"; public string $token = "ui:popup";
public string $tag = "div";
public array $attributes = [ public array $attributes = [
'class' => 'ui-popup', 'class' => 'ui-popup',
]; ];
public function __construct( public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
public string $tag = "div",
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
return "<?php echo 'ui-popup=\"' . ( new \\" . static::class . "() )->buildAttributes($arguments) . '\"' ?>"; return "<?php echo 'ui-popup=\"' . ( new \\" . static::class . "() )->buildAttributes($arguments) . '\"' ?>";
} }

View File

@ -17,112 +17,113 @@ class Ui extends UiElement implements Extension {
protected string $name; protected string $name;
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
{ {
$opt = $context->tokenOptions($token);
switch(true) { switch(true) {
case in_array('checkbox', $options): case in_array('checkbox', $opt):
$input = new UiCheckbox(); $input = new UiCheckbox();
break; break;
case in_array('color', $options): case in_array('color', $opt):
$input = new UiColor(); $input = new UiColor();
break; break;
case in_array('date', $options): case in_array('date', $opt):
$input = new UiDate(); $input = new UiDate();
break; break;
case in_array('datetime', $options): case in_array('datetime', $opt):
$input = new UiDatetime(); $input = new UiDatetime();
break; break;
case in_array('email', $options): case in_array('email', $opt):
$input = new UiEmail(); $input = new UiEmail();
break; break;
case in_array('file', $options): case in_array('file', $opt):
$input = new UiFile(); $input = new UiFile();
break; break;
case in_array('form', $options): case in_array('form', $opt):
case in_array('endform', $options): case in_array('endform', $opt):
$input = new UiForm(); $input = new UiForm();
break; break;
case in_array('hidden', $options): case in_array('hidden', $opt):
$input = new UiHidden(); $input = new UiHidden();
break; break;
case in_array('img', $options): case in_array('image', $opt):
case in_array('image', $options):
$input = new UiImage(); $input = new UiImage();
break; break;
case in_array('input', $options): case in_array('input', $opt):
$input = new UiInput(); $input = new UiInput();
break; break;
case in_array('numeric', $options): case in_array('numeric', $opt):
$input = new UiNumeric(); $input = new UiNumeric();
break; break;
case in_array('password', $options): case in_array('password', $opt):
$input = new UiPassword(); $input = new UiPassword();
break; break;
case in_array('radio', $options): case in_array('radio', $opt):
$input = new UiRadio(); $input = new UiRadio();
break; break;
case in_array('range', $options): case in_array('range', $opt):
$input = new UiRange(); $input = new UiRange();
break; break;
case in_array('search', $options): case in_array('search', $opt):
$input = new UiSearch(); $input = new UiSearch();
break; break;
case in_array('select', $options): case in_array('select', $opt):
$input = new UiSelect(); $input = new UiSelect();
break; break;
case in_array('tel', $options): case in_array('tel', $opt):
$input = new UiTel(); $input = new UiTel();
break; break;
case in_array('text', $options): case in_array('text', $opt):
$input = new UiText(); $input = new UiText();
break; break;
case in_array('textarea', $options): case in_array('textarea', $opt):
$input = new UiTextarea(); $input = new UiTextarea();
break; break;
case in_array('time', $options): case in_array('time', $opt):
$input = new UiTime(); $input = new UiText();
break; break;
case in_array('url', $options): case in_array('url', $opt):
$input = new UiUrl(); $input = new UiUrl();
break; break;
case in_array('week', $options): case in_array('week', $opt):
$input = new UiWeek(); $input = new UiWeek();
break; break;
case in_array('popup', $options): case in_array('popup', $opt):
$input = new UiPopup(); $input = new UiPopup();
break; break;
case in_array('message', $options): case in_array('message', $opt):
$input = new UiMessage(); $input = new UiMessage();
break; break;
} }
if (empty($input)) { if (empty($input)) {
throw new \Exception("Missing token ? $token $arguments"); throw new \Exception("Missing token ? $token");
} }
return $input->parse($context, $arguments, $token, $options); return $input->parse($context, $arguments, $token);
} }
} }

View File

@ -11,65 +11,54 @@ class UiForm extends UiElement implements Extension {
public string $defaultMethod = "get"; public string $defaultMethod = "get";
public array $token = [ "ui:form", "ui:endform" ]; public array $token = [ "ui:form", "ui:endform" /*, "ui:form.get", "ui:form.post", "ui:form.patch", "ui:form.delete", "ui:form.put"*/ ];
public string $tag = "form";
public array $attributes = [ public array $attributes = [
'class' => 'ui-form', 'class' => 'ui-form',
]; ];
public function __construct( public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
public string $tag = "form",
public bool $enctype = true,
public bool $csrf = true,
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
$constructor = [];
switch($token) { switch($token) {
case 'ui.endform': # bw compat
case 'ui:endform': case 'ui:endform':
return "</form>"; return "</form>";
} }
if (in_array('no-enctype', $options)) { $opt = $context->tokenOptions($token, true);
$constructor[] = "enctype: false";
}
if (in_array('no-csrf', $options)) { if (in_array('get', $opt)) {
$constructor[] = "csrf: false";
}
if (in_array('get', $options)) {
$method = "get"; $method = "get";
} }
elseif (in_array('post', $options)) { elseif (in_array('post', $opt)) {
$method = "post"; $method = "post";
} }
elseif (in_array('put', $options)) { elseif (in_array('put', $opt)) {
$method = "put"; $method = "put";
} }
elseif (in_array('delete', $options)) { elseif (in_array('delete', $opt)) {
$method = "delete"; $method = "delete";
} }
elseif (in_array('patch', $options)) { elseif (in_array('patch', $opt)) {
$method = "patch"; $method = "patch";
} }
$method ??= $this->defaultMethod; $method ??= $this->defaultMethod;
$opt = var_export($options, true); return "<?php echo ( new \\" . static::class . "() )->parseOptions($opt)->buildHtml('$method', $arguments) ?>";
}
$constructor = implode(',', $constructor); public function parseOptions($options) : self
{
$this->options = $options;
return "<?php echo ( new \\" . static::class . "($constructor) )->buildHtml('$method', $arguments) ?>"; return $this;
} }
public function buildHtml(string $method = "get", string $name = "", string $action = "", array $attributes = []) : string public function buildHtml(string $method = "get", string $name = "", string $action = "", array $attributes = []) : string
{ {
# Method passed in arguments take precedents over options $method = strtolower($method);
$method = strtolower($attributes['method'] ?? $method);
$this->option('tag-type', 'single'); $this->option('tag-type', 'single');
@ -78,29 +67,24 @@ class UiForm extends UiElement implements Extension {
unset($this->attributes['class']); unset($this->attributes['class']);
} }
$this->attributes([ 'action' => $action, 'method' => $method, ] + $attributes); $this->attributes([ 'method' => $method, 'action' => $action ] + $attributes);
if ( $method !== "get" ) { if ( $method !== "get" ) {
$token = md5( $name . microtime());
$key = "picea-ui:form:{$name}";
if ($this->csrf) { if (count($_SESSION[$key] ?? []) > 100) {
$token = md5($name . microtime()); array_shift($_SESSION[$key]);
$key = "picea-ui:form:{$name}";
if (count($_SESSION[$key] ?? []) > 100) {
array_shift($_SESSION[$key]);
}
$_SESSION[$key][] = $token;
$this->append((new UiHidden())->attributes([
'name' => "picea-ui-form[$name]",
'value' => $token,
]));
} }
if ($this->enctype) { $_SESSION[$key][] = $token;
$this->attributes([ 'enctype' => "multipart/form-data" ]);
} $this->append( ( new UiHidden() )->attributes([
'name' => "picea-ui-form[$name]",
'value' => $token,
]));
$this->attributes([ 'enctype' => "multipart/form-data" ]);
} }
return $this->render() . PHP_EOL; return $this->render() . PHP_EOL;

View File

@ -11,6 +11,8 @@ class UiImage extends UiElement implements Extension {
public array $tokens = [ "ui:img", "ui:image" ]; public array $tokens = [ "ui:img", "ui:image" ];
public string $tag = "img";
public array $attributes = [ public array $attributes = [
'class' => 'ui-image', 'class' => 'ui-image',
]; ];
@ -19,11 +21,7 @@ class UiImage extends UiElement implements Extension {
'tag-type' => "single", 'tag-type' => "single",
]; ];
public function __construct( public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
public string $tag = "img",
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>"; return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";
} }

View File

@ -3,13 +3,16 @@
namespace Picea\Ui\Form; namespace Picea\Ui\Form;
use Picea\Ui\Common\UiElement; use Picea\Ui\Common\UiElement;
use Picea\Extension\{ Extension, FunctionExtension, ExtensionTrait }; use Picea\Extension\Extension;
use Picea\Extension\ExtensionTrait;
class UiInput extends UiElement implements Extension, FunctionExtension { class UiInput extends UiElement implements Extension {
use ExtensionTrait; use ExtensionTrait;
public string $token = "ui:input"; public string $token = "ui:input";
public string $tag = "input";
public array $attributes = [ public array $attributes = [
'class' => 'ui-input', 'class' => 'ui-input',
]; ];
@ -18,32 +21,15 @@ class UiInput extends UiElement implements Extension, FunctionExtension {
'tag-type' => "single", 'tag-type' => "single",
]; ];
protected mixed $value; protected /* ? mixed */ $value;
protected string $name; protected string $name;
public function __construct( public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
public string $tag = "input",
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>"; return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";
} }
public function exportFunctions(): array
{
if ( null === $type = $this->attributes['type'] ?? null ) {
return [];
}
$key = str_replace('-', '_', 'ui_'. ( $this->attributes['type'] ?? 'text' ));
return [
$key => fn(string $name, mixed $value = null, array $attributes = [], array $options = []) : string => $this->buildHtml($name, $value, $attributes, $options),
];
}
public function buildHtml(string $name, mixed $value = null, array $attributes = [], array $options = []) : string public function buildHtml(string $name, mixed $value = null, array $attributes = [], array $options = []) : string
{ {
$this->name = $name; $this->name = $name;

View File

@ -13,7 +13,7 @@ class UiRadio extends UiInput {
protected function objectAttribute() : array protected function objectAttribute() : array
{ {
if ( isset($this->options['value']) && ( $this->options['value'] === $this->value ) ) { if ( ( $this->options['value'] ?? false ) && ( $this->options['value'] === $this->value ) ) {
return [ return [
'checked' => "checked" 'checked' => "checked"
]; ];

View File

@ -11,15 +11,13 @@ class UiSelect extends UiElement implements Extension {
public string $token = "ui:select"; public string $token = "ui:select";
public string $tag = "select";
public array $attributes = [ public array $attributes = [
'class' => 'ui-select', 'class' => 'ui-select',
]; ];
public function __construct( public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
public string $tag = "select",
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>"; return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";
} }

View File

@ -6,6 +6,8 @@ class UiTextarea extends UiInput {
public array $tokens = [ "ui:textarea" ]; public array $tokens = [ "ui:textarea" ];
public string $tag = "textarea";
public array $attributes = [ public array $attributes = [
'class' => "ui-textarea", 'class' => "ui-textarea",
]; ];
@ -14,25 +16,21 @@ class UiTextarea extends UiInput {
protected bool $echoRaw = false; protected bool $echoRaw = false;
public function __construct( public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
public string $tag = "textarea",
public bool $raw = false,
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
$raw = in_array('raw', $options) ? 'true' : 'false'; $raw = in_array('raw', $context->tokenOptions($token, true)) ? 'true' : 'false';
return "<?php echo ( new \\" . static::class . "(raw: $raw) )->buildHtml($arguments) ?>"; return "<?php echo ( new \\" . static::class . "() )->echoRaw($raw)->buildHtml($arguments) ?>";
} }
protected function setValue($value) : void protected function setValue($value) : void
{ {
$this->echoRaw ? $this->html($value) : $this->text($value); $this->echoRaw ? $this->html($value) : $this->text($value);
} }
protected function echoRaw(bool $value) : self public function echoRaw(bool $set) : self
{ {
$this->echoRaw = $value; $this->echoRaw = $set;
return $this; return $this;
} }

View File

@ -25,7 +25,7 @@ class Form implements Extension, FunctionExtension {
$this->request = $request; $this->request = $request;
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { } public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string { }
public function exportFunctions(): array public function exportFunctions(): array
{ {

View File

@ -7,8 +7,6 @@ use Psr\Http\Message\ServerRequestInterface,
class FormContext implements FormContextInterface class FormContext implements FormContextInterface
{ {
public string $method;
public string $formName; public string $formName;
public bool $formSent = false; public bool $formSent = false;
@ -25,8 +23,6 @@ class FormContext implements FormContextInterface
public bool $skipCsrf = false; public bool $skipCsrf = false;
protected array $catchedMethods = [ 'POST', 'PUT', 'PATCH', 'DELETE', ];
public ServerRequestInterface $request; public ServerRequestInterface $request;
public ? ResponseInterface $response = null; public ? ResponseInterface $response = null;
@ -41,25 +37,17 @@ class FormContext implements FormContextInterface
$this->values = $request->getParsedBody() ?: []; $this->values = $request->getParsedBody() ?: [];
$this->method = $this->request->getMethod();
if ( ! $this->values ) { if ( ! $this->values ) {
$content = mb_convert_encoding((string) $request->getBody(), 'UTF-8'); $content = utf8_encode((string) $request->getBody());
if ( $content && ( $json = json_decode($content, true) ) ) { if ( $content && ( $json = json_decode($content, true) ) ) {
$this->values = $json; $this->values = $json;
} }
} }
$this->fillValues();
$this->files = $request->getUploadedFiles() ?: []; $this->files = $request->getUploadedFiles() ?: [];
$this->initialize();
} }
public function initialize() : void {}
public function valid() : bool public function valid() : bool
{ {
foreach($this->messages as $message) { foreach($this->messages as $message) {
@ -73,12 +61,12 @@ class FormContext implements FormContextInterface
public function executed() : bool public function executed() : bool
{ {
return $this->formExecutionStatus === false ? false : $this->formExecuted; return $this->formExecuted;
} }
public function formSent() : bool public function formSent() : bool
{ {
$valid = in_array($this->method, $this->catchedMethods); $valid = in_array($this->request->getMethod(), [ 'POST', 'PUT', 'PATCH', 'DELETE', ]);
if ( ! $this->skipCsrf && ($this->formName ?? false) ) { if ( ! $this->skipCsrf && ($this->formName ?? false) ) {
$token = $this->get('picea-ui-form')[$this->formName] ?? false; $token = $this->get('picea-ui-form')[$this->formName] ?? false;
@ -99,11 +87,6 @@ class FormContext implements FormContextInterface
return $this->formSent = $valid; return $this->formSent = $valid;
} }
public function requestMethod() : string
{
return $this->method;
}
public function __set($key, $value) public function __set($key, $value)
{ {
return $this->set($key, $value); return $this->set($key, $value);
@ -148,16 +131,4 @@ class FormContext implements FormContextInterface
{ {
$this->messages[] = $message; $this->messages[] = $message;
} }
protected function fillValues() : void
{
# Skipping overrides of this particular class vars as a security measure
$skipping = array_keys(array_change_key_case(get_class_vars(FormContext::class), CASE_LOWER));
foreach($this->values as $property => $value) {
if ( ! in_array(strtolower($property), $skipping) && property_exists($this, $property)) {
$this->$property = $value;
}
}
}
} }

View File

@ -6,6 +6,4 @@ interface FormContextInterface {
public function valid() : bool; public function valid() : bool;
public function formSent() : bool; public function formSent() : bool;
public function requestMethod() : string;
} }

View File

@ -23,7 +23,7 @@ class Pagination implements Extension, FunctionExtension {
$this->request = $request; $this->request = $request;
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { } public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string { }
public function exportFunctions(): array public function exportFunctions(): array