From 48536ab11d6b3c11bb8b25aea1cb892ea00300b5 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 9 Apr 2020 14:35:24 -0400 Subject: [PATCH 1/6] - Fixed a bug caused by JsonSerialization and old code accessing attributes. - Fixed Checkbox and Radio checked using a 4th option parameter --- src/Common/UiElement.php | 12 ++++++------ src/Form/UiCheckbox.php | 14 +++++++------- src/Form/UiInput.php | 26 ++++++++++++++++++++++---- src/Form/UiRadio.php | 21 ++++++++++++++++----- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/Common/UiElement.php b/src/Common/UiElement.php index a869fdc..07bfb8b 100644 --- a/src/Common/UiElement.php +++ b/src/Common/UiElement.php @@ -178,7 +178,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { } else if ( !is_numeric($key) ) { # will output something like - $attributesList[] = "$key=\"$value\""; + $attributesList[] = strpos($value, '"') !== false ? "$key='$value'" : "$key=\"$value\""; } else { # will output something like @@ -186,7 +186,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { } } - + $content = ""; foreach ( $this->childs as $item ) { @@ -198,14 +198,14 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { } } - if ( in_array('no-tag', $this->options) ) { + if ( in_array('no-tag', $this->options, true) ) { return $this->content . $content; } else { - $compiledAttributes = (count($attributesList) && ! in_array('no-attr', $this->options) ? " " . implode(" ", $attributesList) : ""); + $compiledAttributes = (count($attributesList) && ! in_array('no-attr', $this->options, true) ? " " . implode(" ", $attributesList) : ""); # Force the node to contain a certain opening tag (php is a good example) - if ( in_array('force-tag-open', $this->options) ) { + if ( in_array('force-tag-open', $this->options, true) ) { $opentag = $this->options['force-tag-open'] . $compiledAttributes; } else { @@ -217,7 +217,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { $closetag = ""; } } - elseif ( in_array('force-tag-close', $this->options) ) { + elseif ( in_array('force-tag-close', $this->options, true) ) { $closetag = $this->options['force-tag-close']; } else { diff --git a/src/Form/UiCheckbox.php b/src/Form/UiCheckbox.php index 59bd1fb..7a9fcd8 100644 --- a/src/Form/UiCheckbox.php +++ b/src/Form/UiCheckbox.php @@ -2,12 +2,12 @@ namespace Picea\Ui\Form; -class UiCheckbox extends UiInput { +class UiCheckbox extends UiRadio { - public string $token = "ui.checkbox"; - - public array $attributes = [ - 'class' => "ui-checkbox", - 'type' => "checkbox", - ]; + public string $token = "ui.checkbox"; + + public array $attributes = [ + 'class' => "ui-checkbox", + 'type' => "checkbox", + ]; } diff --git a/src/Form/UiInput.php b/src/Form/UiInput.php index 2abeeda..fb8a63d 100644 --- a/src/Form/UiInput.php +++ b/src/Form/UiInput.php @@ -21,15 +21,29 @@ class UiInput extends UiElement implements Extension { 'tag-type' => "single", ]; + protected ? string $value; + + protected string $name; + public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string { return "buildHtml($arguments) ?>"; } - public function buildHtml(string $name, ? string $value = null, array $attributes = []) : string + public function buildHtml(string $name, ? string $value = null, array $attributes = [], array $options = []) : string { - $this->setValue($value); - $this->attributes([ 'name' => $name ] + $attributes + $this->attributes); + $this->name = $name; + + if (null !== $this->value = $value) { + $this->setValue($value); + } + + if ($options) { + $this->options += $options; + } + + $this->attributes([ 'name' => $name ] + $attributes + $this->attributes + $this->objectAttribute()); + return $this->render(); } @@ -37,5 +51,9 @@ class UiInput extends UiElement implements Extension { { $this->attributes['value'] = $value; } - + + protected function objectAttribute() : array + { + return []; + } } diff --git a/src/Form/UiRadio.php b/src/Form/UiRadio.php index be8d1d7..b51b523 100644 --- a/src/Form/UiRadio.php +++ b/src/Form/UiRadio.php @@ -4,10 +4,21 @@ namespace Picea\Ui\Form; class UiRadio extends UiInput { - public string $token = "ui.radio"; + public string $token = "ui.radio"; - public array $attributes = [ - 'class' => "ui-radio", - 'type' => "radio", - ]; + public array $attributes = [ + 'class' => "ui-radio", + 'type' => "radio", + ]; + + protected function objectAttribute() : array + { + if ( $this->options['value'] ?? false && ( $this->options['value'] === $this->value ) ) { + return [ + 'checked' => "checked" + ]; + } + + return []; + } } From db17c890c5ddbf02679ade79c57dae038bd58c15 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 9 Apr 2020 14:39:25 -0400 Subject: [PATCH 2/6] - Forced form's name when one is given to a contact (will make sure the good form has been passed) --- src/Common/UiElement.php | 4 ++-- src/Method/FormHandler.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Common/UiElement.php b/src/Common/UiElement.php index a869fdc..fb4596a 100644 --- a/src/Common/UiElement.php +++ b/src/Common/UiElement.php @@ -346,7 +346,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { public function jsonSerialize() { return [ 'tag' => $this->tag, - 'attr' => $this->attr, + 'attr' => $this->attributes, 'childs' => $this->childs, 'options' => $this->options, ]; @@ -404,7 +404,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { return ! in_array(key($this->childs), [ NULL, FALSE ], true); } - public static function is_node($obj) : bool + public static function isNode($obj) : bool { return $obj instanceof UiElement; } diff --git a/src/Method/FormHandler.php b/src/Method/FormHandler.php index 64acb60..ebc1a52 100644 --- a/src/Method/FormHandler.php +++ b/src/Method/FormHandler.php @@ -34,7 +34,7 @@ class FormHandler { { if ( false !== $this->context->formSent = $this->sent ) { if ( $this->context->formName ?? false ) { - $this->context->formSent = (bool) ( $this->request->getParsedBody()['picea-ui-form'][$this->context->formName] ?? false ); + $this->sent = $this->context->formSent = (bool) ( $this->request->getParsedBody()['picea-ui-form'][$this->context->formName] ?? false ); } } } From 69c712198172090d1d1497ca07c2f28d4501c11a Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 20 May 2020 15:32:41 -0400 Subject: [PATCH 3/6] - Small bugfixes within value comparison for UiInput and small bugfixe in select repairing broken options. --- src/Form/UiInput.php | 9 +++++++-- src/Form/UiRadio.php | 4 ++-- src/Form/UiSelect.php | 8 +++----- src/Method/FormHandler.php | 2 ++ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Form/UiInput.php b/src/Form/UiInput.php index fb8a63d..d3812ff 100644 --- a/src/Form/UiInput.php +++ b/src/Form/UiInput.php @@ -21,7 +21,7 @@ class UiInput extends UiElement implements Extension { 'tag-type' => "single", ]; - protected ? string $value; + protected /* ? mixed */ $value; protected string $name; @@ -30,7 +30,7 @@ class UiInput extends UiElement implements Extension { return "buildHtml($arguments) ?>"; } - public function buildHtml(string $name, ? string $value = null, array $attributes = [], array $options = []) : string + public function buildHtml(string $name, /*? mixed */ $value = null, array $attributes = [], array $options = []) : string { $this->name = $name; @@ -41,6 +41,11 @@ class UiInput extends UiElement implements Extension { if ($options) { $this->options += $options; } + + if ($attributes['class'] ?? false) { + $attributes['class'] .= " {$this->attributes['class']}"; + unset($this->attributes['class']); + } $this->attributes([ 'name' => $name ] + $attributes + $this->attributes + $this->objectAttribute()); diff --git a/src/Form/UiRadio.php b/src/Form/UiRadio.php index b51b523..d335d22 100644 --- a/src/Form/UiRadio.php +++ b/src/Form/UiRadio.php @@ -12,8 +12,8 @@ class UiRadio extends UiInput { ]; protected function objectAttribute() : array - { - if ( $this->options['value'] ?? false && ( $this->options['value'] === $this->value ) ) { + { + if ( ( $this->options['value'] ?? false ) && ( $this->options['value'] === $this->value ) ) { return [ 'checked' => "checked" ]; diff --git a/src/Form/UiSelect.php b/src/Form/UiSelect.php index 4d6bc31..2ad269b 100644 --- a/src/Form/UiSelect.php +++ b/src/Form/UiSelect.php @@ -38,24 +38,22 @@ class UiSelect extends UiElement implements Extension { protected function setList(array $list, $selected, bool $strict = true) : void { foreach($list as $key => $item) { - $isSelected = $this->isSelected($key, $selected, $strict); if ($item instanceof UiElement) { - $this->append($item); continue; } elseif ( is_array($item) ) { $obj = new UiElement("optgroup"); - $obj->text($key); + $obj->attributes(['label' => $key]); foreach($item as $subKey => $subItem) { - $obj->append($this->createOption($subItem, $subKey, $isSelected)); + $obj->append($this->createOption($subItem, $subKey, $this->isSelected($subKey, $selected, $strict))); } } else { - $obj = $this->createOption($item, $key, $isSelected); + $obj = $this->createOption($item, $key, $this->isSelected($key, $selected, $strict)); } $this->append($obj); diff --git a/src/Method/FormHandler.php b/src/Method/FormHandler.php index d49e145..4b7cd1c 100644 --- a/src/Method/FormHandler.php +++ b/src/Method/FormHandler.php @@ -28,6 +28,8 @@ class FormHandler { $this->context = method_exists($form, 'getContext') ? $form->getContext($request) : new FormContext($request); } + $this->request->context = $this->context; + $this->formSent(); $this->initialize(); } From 8842ae4eaa94a1232560b6498c630037cc38ff54 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 6 Oct 2020 11:04:13 -0400 Subject: [PATCH 4/6] - Fixed a bug where "002" value was set as "2", therefore failling to match selected items; a proper value selection must be implemented. --- src/Form/UiSelect.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Form/UiSelect.php b/src/Form/UiSelect.php index 2ad269b..5246c74 100644 --- a/src/Form/UiSelect.php +++ b/src/Form/UiSelect.php @@ -75,14 +75,6 @@ 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; - } - elseif (false !== ( $f = filter_var($value, FILTER_VALIDATE_FLOAT) ) ) { - $value = $f; - } - return $strict ? $check === $value : $check == $value; } } From fd73d8e61466939b90f1c3f0d95a803143e3c77c Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 6 Oct 2020 11:27:56 -0400 Subject: [PATCH 5/6] - Set formSent() public on FormHandler class - Fixed class attributes on form which was overwritting it instead of adding to base class --- src/Form/UiForm.php | 6 ++++++ src/Method/FormHandler.php | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Form/UiForm.php b/src/Form/UiForm.php index 1a0450f..5d5b5e2 100644 --- a/src/Form/UiForm.php +++ b/src/Form/UiForm.php @@ -57,6 +57,12 @@ class UiForm extends UiElement implements Extension { $this->option('tag-type', 'single'); + + if ($attributes['class'] ?? false) { + $attributes['class'] .= " {$this->attributes['class']}"; + unset($this->attributes['class']); + } + $this->attributes([ 'method' => $method, 'action' => $action ] + $attributes); if ( $method !== "get" ) { diff --git a/src/Method/FormHandler.php b/src/Method/FormHandler.php index 4b7cd1c..223bb44 100644 --- a/src/Method/FormHandler.php +++ b/src/Method/FormHandler.php @@ -34,7 +34,7 @@ class FormHandler { $this->initialize(); } - protected function formSent() : void + public function formSent() : void { if ( false !== $this->context->formSent = $this->sent ) { if ( $this->context->formName ?? false ) { From d05a750b391f53b5f7a59602248786c47d99dd5d Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 6 Oct 2020 11:40:27 -0400 Subject: [PATCH 6/6] - Fixed Numeric types (number) --- src/Form/UiNumeric.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Form/UiNumeric.php b/src/Form/UiNumeric.php index 2c3ad5c..c0ab774 100644 --- a/src/Form/UiNumeric.php +++ b/src/Form/UiNumeric.php @@ -8,6 +8,6 @@ class UiNumeric extends UiInput { public array $attributes = [ 'class' => "ui-numeric", - 'type' => "numeric", + 'type' => "number", ]; }