From b1129def4765ce34f064c8047bf7564c5d0af7dc Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 17 Mar 2020 16:14:47 -0400 Subject: [PATCH] - WIP on forms and UI widgets --- src/Common/UiElement.php | 31 ++++++++++++++++++------------- src/Form/UiForm.php | 6 ++++-- src/Form/UiInput.php | 5 ++++- src/Form/UiSelect.php | 1 + src/Form/UiTextarea.php | 2 ++ src/Method/FormHandler.php | 2 +- src/Method/FormInterface.php | 2 +- 7 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/Common/UiElement.php b/src/Common/UiElement.php index 838a25c..849b2f8 100644 --- a/src/Common/UiElement.php +++ b/src/Common/UiElement.php @@ -108,7 +108,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { $obj->attributes($attributes); } - if ( $custom = static::$config["tags"][$tag] ?? false ) { + if ( false !== ( $custom = static::$config["tags"][$tag] ?? false ) ) { if ( $custom['tag'] ?? false ) { $obj->tag = $custom['tag']; } @@ -129,34 +129,39 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { { $element = static::create($tag, $attributes); $this->append($element); + return $element; } public function addClass(string $classname) : self { $this->attributes['class'] = $classname; + return $this; } public function removeClass($classname) { - if ( $key = array_search(strtolower($classname), array_map('strtolower', $this->attributes['class'])) ) { + if ( false !== $key = array_search(strtolower($classname), array_map('strtolower', $this->attributes['class'])) ) { unset($this->attributes['class'][$key]); } return $this; } - public function option(string $var, $value = 1) { + public function option(string $var, $value = 1) : self + { $this->options[$var] = $value; + return $this; } - public function render() { + public function render() : string + { $attributesList = []; foreach ( $this->attributes as $key => $value ) { if ( is_array($value) ) { - if (empty($value)) continue; + if ( empty($value) ) continue; if ($key === 'style') { $style = []; @@ -183,7 +188,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { } $content = ""; - + foreach ( $this->childs as $item ) { if ( is_object($item) ) { $content .= $item->render(); @@ -197,22 +202,22 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { return $this->content . $content; } else { - $attributesstr = (count($attributesList) && ! in_array('no-attr', $this->options) ? " " . implode(" ", $attributesList) : ""); + $compiledAttributes = (count($attributesList) && ! in_array('no-attr', $this->options) ? " " . implode(" ", $attributesList) : ""); # Force the node to contain a certain opening tag (php is a good example) if ( in_array('force-tag-open', $this->options) ) { - $opentag = $this->options['force-tag-open'] . $attributesstr; + $opentag = $this->options['force-tag-open'] . $compiledAttributes; } else { - $opentag = $this->tag ? "<{$this->tag}" . $attributesstr . ">" : ""; + $opentag = $this->tag ? "<{$this->tag}" . $compiledAttributes . ">" : ""; } if ( $this->options['tag-type'] ?? false ) { if ( $this->options['tag-type'] === "single" ) { - return $opentag; + return $opentag . $content; } } - else if ( in_array('force-tag-close', $this->options) ) { + elseif ( in_array('force-tag-close', $this->options) ) { $closetag = $this->options['force-tag-close']; } else { @@ -226,6 +231,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { public function html($set = null) { if ($set !== null) { $this->content = $set; + return $this; } @@ -235,6 +241,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { public function text(?string $set = null) { if ($set !== null) { $this->content = htmlspecialchars( $set, ENT_NOQUOTES ); + return $this; } @@ -263,13 +270,11 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { case static::INSERT_MODE_PREPEND: array_unshift($this->childs, $item); break; - } } return $this; } - public function attributes(array $attributes) : self { diff --git a/src/Form/UiForm.php b/src/Form/UiForm.php index a0b74c0..1a0450f 100644 --- a/src/Form/UiForm.php +++ b/src/Form/UiForm.php @@ -47,10 +47,11 @@ class UiForm extends UiElement implements Extension { } $method ??= $this->defaultMethod; + return "buildHtml('$method', $arguments) ?>"; } - public function buildHtml(string $method = "get", string $action = "", array $attributes = []) : string + public function buildHtml(string $method = "get", string $name = "", string $action = "", array $attributes = []) : string { $method = strtolower($method); @@ -59,7 +60,8 @@ class UiForm extends UiElement implements Extension { $this->attributes([ 'method' => $method, 'action' => $action ] + $attributes); if ( $method !== "get" ) { - $this->append( ( new UiHidden() )->attributes([ 'name' =>"picea-csrf-protection", 'value' => "abcdefg" ]) ); + $this->append( ( new UiHidden() )->attributes([ 'name' =>"picea-ui-form[$name]", 'value' => md5( $name . microtime() ) ]) ); + $this->attributes([ 'enctype' => "multipart/form-data" ]); } diff --git a/src/Form/UiInput.php b/src/Form/UiInput.php index 0497fbb..2abeeda 100644 --- a/src/Form/UiInput.php +++ b/src/Form/UiInput.php @@ -16,6 +16,10 @@ class UiInput extends UiElement implements Extension { public array $attributes = [ 'class' => 'ui-input', ]; + + public array $options = [ + 'tag-type' => "single", + ]; public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string { @@ -26,7 +30,6 @@ class UiInput extends UiElement implements Extension { { $this->setValue($value); $this->attributes([ 'name' => $name ] + $attributes + $this->attributes); - return $this->render(); } diff --git a/src/Form/UiSelect.php b/src/Form/UiSelect.php index ac11db2..4d6bc31 100644 --- a/src/Form/UiSelect.php +++ b/src/Form/UiSelect.php @@ -77,6 +77,7 @@ class UiSelect extends UiElement implements Extension { protected function isSelected($check, $value, bool $strict = true) : bool { + if (false !== ( $f = filter_var($value, FILTER_VALIDATE_INT) ) ) { $value = $f; } diff --git a/src/Form/UiTextarea.php b/src/Form/UiTextarea.php index 41c94fc..1cfc674 100644 --- a/src/Form/UiTextarea.php +++ b/src/Form/UiTextarea.php @@ -11,6 +11,8 @@ class UiTextarea extends UiInput { public array $attributes = [ 'class' => "ui-textarea", ]; + + public array $options = []; protected function setValue($value) : void { diff --git a/src/Method/FormHandler.php b/src/Method/FormHandler.php index 219add9..ba3308c 100644 --- a/src/Method/FormHandler.php +++ b/src/Method/FormHandler.php @@ -36,7 +36,7 @@ class FormHandler { if ( $this->sent ) { if ( $this->form->validate($this->context) ) { - $this->form->run($this->context); + $this->form->execute($this->context); } } } diff --git a/src/Method/FormInterface.php b/src/Method/FormInterface.php index 15096f7..0471fc3 100644 --- a/src/Method/FormInterface.php +++ b/src/Method/FormInterface.php @@ -8,5 +8,5 @@ interface FormInterface public function validate(FormContextInterface $context) : bool; - public function run(FormContextInterface $context) : void; + public function execute(FormContextInterface $context) : void; } \ No newline at end of file