From f28ace745d55cf9cb0d4956f2f04427e9f1e07e6 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 1 Nov 2019 14:47:54 -0400 Subject: [PATCH] - Corrected inputs handling from view to compiled states - Added UiSelect widget - Fixed UiForm widget - Fixed UiInput widget - Added mocks of UiMessage and UiPopup. Not even a WIP at this stage. --- src/Common/UiElement.php | 2 +- src/Component/UiMessage.php | 24 +++++++++++++ src/Component/UiPopup.php | 24 +++++++++++++ src/Form/UiForm.php | 46 ++++++++++++++----------- src/Form/UiInput.php | 13 +++---- src/Form/UiSelect.php | 68 +++++++++++++++++++++++++++++++++++++ src/Ui.php | 3 ++ 7 files changed, 153 insertions(+), 27 deletions(-) create mode 100644 src/Component/UiMessage.php create mode 100644 src/Component/UiPopup.php create mode 100644 src/Form/UiSelect.php diff --git a/src/Common/UiElement.php b/src/Common/UiElement.php index 31edf7a..8e5e55d 100644 --- a/src/Common/UiElement.php +++ b/src/Common/UiElement.php @@ -230,7 +230,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { public function text(?string $set = null) { if ($set !== null) { - $this->content = htmlspecialchars( $args[0], ENT_NOQUOTES ); + $this->content = htmlspecialchars( $set, ENT_NOQUOTES ); return $this; } diff --git a/src/Component/UiMessage.php b/src/Component/UiMessage.php new file mode 100644 index 0000000..9bff04d --- /dev/null +++ b/src/Component/UiMessage.php @@ -0,0 +1,24 @@ + 'ui-message', + ]; + + public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string + { + return ""; + } +} diff --git a/src/Component/UiPopup.php b/src/Component/UiPopup.php new file mode 100644 index 0000000..e4521f1 --- /dev/null +++ b/src/Component/UiPopup.php @@ -0,0 +1,24 @@ + 'ui-popup', + ]; + + public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string + { + return ""; + } +} diff --git a/src/Form/UiForm.php b/src/Form/UiForm.php index b393275..ec817b8 100644 --- a/src/Form/UiForm.php +++ b/src/Form/UiForm.php @@ -11,7 +11,7 @@ class UiForm extends UiElement implements Extension { public string $defaultMethod = "get"; - public array $token = [ "ui.form", "ui.endform", "ui.form.get", "ui.form.post" ]; + 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"; @@ -21,33 +21,39 @@ class UiForm extends UiElement implements Extension { public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string { - if ( $token === 'ui.endform' ) { - return ""; - } - - list($action, $options) = array_pad(explode(',', $arguments), 2, null); - - $action = $this->trim($action); - switch($token) { + case 'ui.endform': + return ""; + case "ui.form.get": - $this->attributes['method'] = "get"; + $method = "get"; + break; + + case "ui.form.patch": + $method = "patch"; + break; + + case "ui.form.delete": + $method = "delete"; + break; + + case "ui.form.put": + $method = "put"; break; case "ui.form.post": - $this->attributes['method'] = "post"; - $this->attributes['enctype'] = "multipart/form-data"; - break; - - default: - case $this->token: - $this->attributes['method'] = $this->defaultMethod; + $method = "post"; break; } + $method ??= $this->defaultMethod; + return "buildHtml('$method', $arguments) ?>"; + } + + public function buildHtml(string $method, string $action, array $attributes = []) : string + { $this->option('tag-type', 'single'); - $this->attributes['action'] = $this->trim($action); - $this->attributes($this->parseOptions($options)); - return $this->render(); + $this->attributes([ 'action' => $action ] + $attributes); + return $this->render() . PHP_EOL; } } diff --git a/src/Form/UiInput.php b/src/Form/UiInput.php index ff7e911..1490b15 100644 --- a/src/Form/UiInput.php +++ b/src/Form/UiInput.php @@ -19,14 +19,15 @@ class UiInput extends UiElement implements Extension { public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string { - list($name, $value, $options) = array_pad(explode(',', $arguments), 3, null); - - $this->attributes['name'] = $this->trim($name); - $this->setValue($this->trim($value)); - $this->attributes($this->parseOptions($options)); - return $this->render(); + return "buildHtml($arguments) ?>"; } + public function buildHtml(string $name, string $value, array $attributes = []) : string + { + $this->setValue($value); + $this->attributes([ 'name' => $name ] + $attributes + $this->attributes); + return $this->render(); + } protected function setValue($value) : void { diff --git a/src/Form/UiSelect.php b/src/Form/UiSelect.php new file mode 100644 index 0000000..597251f --- /dev/null +++ b/src/Form/UiSelect.php @@ -0,0 +1,68 @@ + 'ui-select', + ]; + + public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string + { + return "buildHtml($arguments) ?>"; + } + + public function buildHtml(string $name, array $list, $value, array $attributes = [], bool $strictComparison = true) : string + { + $this->attributes([ 'name' => $name ] + $attributes + $this->attributes); + $this->setList($list, $value, $strictComparison); + + return $this->render(); + } + + protected function setValue($value) : void + { + $this->attributes['value'] = $value; + } + + protected function setList(array $list, $selected, bool $strict = true) : void + { + foreach($list as $key => $item) { + if ($item instanceof UiElement) { + $this->append($item); + } + elseif ( is_array($item) ) { + $obj = new UiElement("optgroup"); + $obj->text($key); + + foreach($item as $subKey => $subItem) { + $obj->append($this->createOption($subItem, $subKey, $strict ? $subKey === $selected : $subKey == $selected)); + } + } + else { + $obj = $this->createOption($item, $key, $strict ? $key === $selected : $key == $selected); + } + + $this->append($obj); + } + } + + protected function createOption(string $name, string $value, bool $selected, array $attributes = []) : UiElement + { + $option = new UiElement("option"); + $option->text($name); + $option->attributes($attributes + [ 'value' => $value ]); + + return $option; + } +} diff --git a/src/Ui.php b/src/Ui.php index a39425f..379f6b0 100644 --- a/src/Ui.php +++ b/src/Ui.php @@ -27,6 +27,9 @@ class Ui { $compiler->registerExtension(new Form\UiTime()); $compiler->registerExtension(new Form\UiUrl()); $compiler->registerExtension(new Form\UiWeek()); + $compiler->registerExtension(new Form\UiSelect()); + + $compiler->registerExtension(new Component\UiPopup()); return $this; }