'ui-select', ]; public function __construct( public string $tag = "select", ) {} public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { return "buildHtml($arguments) ?>"; } public function buildHtml(string $name, array $list, mixed $value = null, array $attributes = [], bool $strictComparison = true) : string { if ($attributes['class'] ?? false) { $attributes['class'] = implode(" ", array_merge((array) $attributes['class'], (array) $this->attributes['class'])); unset($this->attributes['class']); } $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); continue; } elseif ( is_array($item) ) { $obj = new UiElement("optgroup"); $obj->attributes(['label' => $key]); foreach($item as $subKey => $subItem) { $obj->append($this->createOption((string) $subItem, $subKey, $this->isSelected($subKey, $selected, $strict))); } } else { $obj = $this->createOption((string) $item, $key, $this->isSelected($key, $selected, $strict)); } $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 ]); if ($selected) { $option->attributes(['selected' => 'selected']); } return $option; } protected function isSelected($check, $value, bool $strict = true) : bool { if ( is_array($value) ) { return in_array($check, $value, $strict); } return $strict ? $check === $value : $check == $value; } }