- Token name and options are now passed through parse() method

This commit is contained in:
Dave M. 2023-02-02 19:38:20 +00:00
parent 77d05ae051
commit aba66479be
12 changed files with 114 additions and 150 deletions

View File

@ -16,3 +16,19 @@ 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

@ -2,7 +2,7 @@
namespace Picea\Ui\Common; namespace Picea\Ui\Common;
class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { class UiElement implements \JsonSerializable {
static array $config = []; static array $config = [];
@ -37,9 +37,9 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
public string $content = ""; public string $content = "";
protected ?UiQuery $kwery = null; public function __construct(
? string $tag = null
public function __construct(? string $tag = null) { ) {
if ( ! static::$config ) { if ( ! static::$config ) {
static::pushConfigArray( include(dirname(__FILE__) . "/taglist.php") ); static::pushConfigArray( include(dirname(__FILE__) . "/taglist.php") );
} }
@ -54,7 +54,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
static::$config = array_replace_recursive(static::$config, $array); static::$config = array_replace_recursive(static::$config, $array);
} }
public static function createStylesheet(string $href, $attributes = [], $options = []) : self public static function stylesheet(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 \ArrayAccess, \Iterator, \JsonSerializable {
], [ 'tag-type' => 'single' ] + $options); ], [ 'tag-type' => 'single' ] + $options);
} }
public static function createScript(string $src, array $attributes = [], array $options = []) : self public static function script(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 \ArrayAccess, \Iterator, \JsonSerializable {
], $options); ], $options);
} }
public static function createPhp(array $attributes = [], array $options = []) : self public static function php(array $attributes = [], array $options = []) : self
{ {
return static::create('', $attributes, [ return static::create('', $attributes, [
'force-tag-open' => '<?php ', 'force-tag-open' => '<?php ',
@ -327,7 +327,6 @@ class UiElement implements \ArrayAccess, \Iterator, \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);
@ -346,10 +345,6 @@ class UiElement implements \ArrayAccess, \Iterator, \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);
} }
@ -364,65 +359,6 @@ class UiElement implements \ArrayAccess, \Iterator, \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) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
return ""; return "";
} }

View File

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

View File

@ -11,7 +11,7 @@ class UiForm extends UiElement implements Extension {
public string $defaultMethod = "get"; public string $defaultMethod = "get";
public array $token = [ "ui:form", "ui:endform" /*, "ui:form.get", "ui:form.post", "ui:form.patch", "ui:form.delete", "ui:form.put"*/ ]; public array $token = [ "ui:form", "ui:endform" ];
public string $tag = "form"; public string $tag = "form";
@ -19,43 +19,53 @@ class UiForm extends UiElement implements Extension {
'class' => 'ui-form', 'class' => 'ui-form',
]; ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string public function __construct(
public bool $enctype = true,
public bool $csrf = true,
) {
parent::__construct(null);
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
$constructor = [];
switch($token) { switch($token) {
case 'ui:endform': case 'ui:endform':
return "</form>"; return "</form>";
} }
$opt = $context->tokenOptions($token); if (in_array('no-enctype', $options)) {
$constructor[] = "enctype: false";
}
if (in_array('get', $opt)) { if (in_array('no-csrf', $options)) {
$constructor[] = "csrf: false";
}
if (in_array('get', $options)) {
$method = "get"; $method = "get";
} }
elseif (in_array('post', $opt)) { elseif (in_array('post', $options)) {
$method = "post"; $method = "post";
} }
elseif (in_array('put', $opt)) { elseif (in_array('put', $options)) {
$method = "put"; $method = "put";
} }
elseif (in_array('delete', $opt)) { elseif (in_array('delete', $options)) {
$method = "delete"; $method = "delete";
} }
elseif (in_array('patch', $opt)) { elseif (in_array('patch', $options)) {
$method = "patch"; $method = "patch";
} }
$method ??= $this->defaultMethod; $method ??= $this->defaultMethod;
$opt = var_export($opt, true); $opt = var_export($options, true);
return "<?php echo ( new \\" . static::class . "() )->parseOptions($opt)->buildHtml('$method', $arguments) ?>"; $constructor = implode(',', $constructor);
}
public function parseOptions($options) : self return "<?php echo ( new \\" . static::class . "($constructor) )->buildHtml('$method', $arguments) ?>";
{
$this->options = $options;
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
@ -72,7 +82,9 @@ class UiForm extends UiElement implements Extension {
$this->attributes([ 'method' => $method, 'action' => $action ] + $attributes); $this->attributes([ 'method' => $method, 'action' => $action ] + $attributes);
if ( $method !== "get" ) { if ( $method !== "get" ) {
$token = md5( $name . microtime());
if ($this->csrf) {
$token = md5($name . microtime());
$key = "picea-ui:form:{$name}"; $key = "picea-ui:form:{$name}";
if (count($_SESSION[$key] ?? []) > 100) { if (count($_SESSION[$key] ?? []) > 100) {
@ -81,13 +93,16 @@ class UiForm extends UiElement implements Extension {
$_SESSION[$key][] = $token; $_SESSION[$key][] = $token;
$this->append( ( new UiHidden() )->attributes([ $this->append((new UiHidden())->attributes([
'name' => "picea-ui-form[$name]", 'name' => "picea-ui-form[$name]",
'value' => $token, 'value' => $token,
])); ]));
}
if ($this->enctype) {
$this->attributes([ 'enctype' => "multipart/form-data" ]); $this->attributes([ 'enctype' => "multipart/form-data" ]);
} }
}
return $this->render() . PHP_EOL; return $this->render() . PHP_EOL;
} }

View File

@ -21,7 +21,7 @@ class UiImage extends UiElement implements Extension {
'tag-type' => "single", 'tag-type' => "single",
]; ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string 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

@ -25,7 +25,7 @@ class UiInput extends UiElement implements Extension {
protected string $name; protected string $name;
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string 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

@ -17,7 +17,7 @@ class UiSelect extends UiElement implements Extension {
'class' => 'ui-select', 'class' => 'ui-select',
]; ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string 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

@ -16,22 +16,21 @@ class UiTextarea extends UiInput {
protected bool $echoRaw = false; protected bool $echoRaw = false;
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string public function __construct(
{ public bool $raw = false
$raw = in_array('raw', $context->tokenOptions($token)) ? 'true' : 'false'; ) {
parent::__construct(null);
}
return "<?php echo ( new \\" . static::class . "() )->echoRaw($raw)->buildHtml($arguments) ?>"; public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
$raw = in_array('raw', $options) ? 'true' : 'false';
return "<?php echo ( new \\" . static::class . "(raw: $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);
} }
public function echoRaw(bool $set) : self
{
$this->echoRaw = $set;
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) : string { } public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { }
public function exportFunctions(): array public function exportFunctions(): array
{ {

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) : string { } public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { }
public function exportFunctions(): array public function exportFunctions(): array