diff --git a/src/Common/UiElement.php b/src/Common/UiElement.php index 8e5e55d..838a25c 100644 --- a/src/Common/UiElement.php +++ b/src/Common/UiElement.php @@ -39,10 +39,14 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { protected ?UiQuery $kwery = null; - public function __construct() { + 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 @@ -237,54 +241,35 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { return $this->content; } - public function append( ...$arguments ) { - return $this->insert( self::INSERT_MODE_APPEND, ...$arguments); + public function append( ...$arguments ) : self + { + return $this->insert( static::INSERT_MODE_APPEND, ...$arguments); } - public function prepend( ...$arguments ) { - return $this->insert( self::INSERT_MODE_PREPEND, ...(is_array($arguments) ? array_reverse($arguments) : $arguments)); + public function prepend( ...$arguments ) : self + { + return $this->insert( static::INSERT_MODE_PREPEND, ...( is_array($arguments) ? array_reverse($arguments) : $arguments) ); } - public function insert($insert_mode = self::INSERT_MODE_APPEND, ...$arguments) { - if ( ! $arguments || !( $count = count($arguments) ) ) { - return $this; - } - - $insert = function($content) use ( $insert_mode ) { - if ( self::is_node($content) ) { - if ($insert_mode === self::INSERT_MODE_APPEND ) { - $this->childs[] = $content; - } - elseif ($insert_mode === self::INSERT_MODE_PREPEND ) { - array_unshift($this->childs, $content); - } - } - }; - - if ( $count == 1 && !is_array($arguments) ) { - // Single node to add - $insert($arguments); - } - else { - // multiple node to add - foreach ( $arguments as $item ) { - if ( is_array($item) ) { - foreach ( $item as $key => $value ) { - if ( !is_numeric($key) ) { - $value->name = $key; - } - - $this->insert($insert_mode, $value); - } - } - else { - $insert($item); - } + + protected function insert(int $mode, ...$elements) : self + { + foreach($elements as $item) { + switch($mode) { + case static::INSERT_MODE_APPEND: + array_push($this->childs, $item); + break; + + case static::INSERT_MODE_PREPEND: + array_unshift($this->childs, $item); + break; + } } - + return $this; } + public function attributes(array $attributes) : self { @@ -416,7 +401,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable { public static function is_node($obj) : bool { - return is_object($obj) && get_class($obj) === static::class; + return $obj instanceof UiElement; } public function __toString() : string diff --git a/src/Form/UiForm.php b/src/Form/UiForm.php index afaf2a9..9e2b8e7 100644 --- a/src/Form/UiForm.php +++ b/src/Form/UiForm.php @@ -55,7 +55,7 @@ class UiForm extends UiElement implements Extension { { $this->option('tag-type', 'single'); $this->attributes([ 'method' => $method, 'action' => $action ] + $attributes); - + return $this->render() . PHP_EOL; } } \ No newline at end of file diff --git a/src/Form/UiSelect.php b/src/Form/UiSelect.php index 0fb92b5..e5e5663 100644 --- a/src/Form/UiSelect.php +++ b/src/Form/UiSelect.php @@ -40,6 +40,7 @@ class UiSelect extends UiElement implements Extension { foreach($list as $key => $item) { if ($item instanceof UiElement) { $this->append($item); + continue; } elseif ( is_array($item) ) { @@ -53,7 +54,7 @@ class UiSelect extends UiElement implements Extension { else { $obj = $this->createOption($item, $key, $strict ? $key === $selected : $key == $selected); } - + $this->append($obj); } } @@ -64,6 +65,10 @@ class UiSelect extends UiElement implements Extension { $option->text($name); $option->attributes($attributes + [ 'value' => $value ]); + if ($selected) { + $option->attributes(['selected' => 'selected']); + } + return $option; } }