- Fixed conflict

This commit is contained in:
Dave Mc Nicoll 2020-01-30 11:21:26 -05:00
parent bf7605eef6
commit e2c1c7ac39
3 changed files with 34 additions and 44 deletions

View File

@ -39,10 +39,14 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
protected ?UiQuery $kwery = null; protected ?UiQuery $kwery = null;
public function __construct() { 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") );
} }
if ($tag !== null) {
$this->tag = $tag;
}
} }
public static function pushConfigArray(array $array) : void public static function pushConfigArray(array $array) : void
@ -237,55 +241,36 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
return $this->content; return $this->content;
} }
public function append( ...$arguments ) { public function append( ...$arguments ) : self
return $this->insert( self::INSERT_MODE_APPEND, ...$arguments); {
return $this->insert( static::INSERT_MODE_APPEND, ...$arguments);
} }
public function prepend( ...$arguments ) { public function prepend( ...$arguments ) : self
return $this->insert( self::INSERT_MODE_PREPEND, ...(is_array($arguments) ? array_reverse($arguments) : $arguments)); {
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 ) { protected function insert(int $mode, ...$elements) : self
if ( self::is_node($content) ) { {
if ($insert_mode === self::INSERT_MODE_APPEND ) { foreach($elements as $item) {
$this->childs[] = $content; switch($mode) {
} case static::INSERT_MODE_APPEND:
elseif ($insert_mode === self::INSERT_MODE_PREPEND ) { array_push($this->childs, $item);
array_unshift($this->childs, $content); break;
}
}
};
if ( $count == 1 && !is_array($arguments) ) { case static::INSERT_MODE_PREPEND:
// Single node to add array_unshift($this->childs, $item);
$insert($arguments); break;
}
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);
}
} }
} }
return $this; return $this;
} }
public function attributes(array $attributes) : self public function attributes(array $attributes) : self
{ {
foreach($attributes as $key => $value) { foreach($attributes as $key => $value) {
@ -416,7 +401,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
public static function is_node($obj) : bool public static function is_node($obj) : bool
{ {
return is_object($obj) && get_class($obj) === static::class; return $obj instanceof UiElement;
} }
public function __toString() : string public function __toString() : string

View File

@ -40,6 +40,7 @@ class UiSelect extends UiElement implements Extension {
foreach($list as $key => $item) { foreach($list as $key => $item) {
if ($item instanceof UiElement) { if ($item instanceof UiElement) {
$this->append($item); $this->append($item);
continue; continue;
} }
elseif ( is_array($item) ) { elseif ( is_array($item) ) {
@ -64,6 +65,10 @@ class UiSelect extends UiElement implements Extension {
$option->text($name); $option->text($name);
$option->attributes($attributes + [ 'value' => $value ]); $option->attributes($attributes + [ 'value' => $value ]);
if ($selected) {
$option->attributes(['selected' => 'selected']);
}
return $option; return $option;
} }
} }