'ui-select', ]; public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string { return "buildHtml($arguments) ?>"; } public function buildHtml(string $name, array $list, $value = null, array $attributes = [], bool $strictComparison = true) : string { $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) { $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); foreach($item as $subKey => $subItem) { $obj->append($this->createOption((string) $subItem, $subKey, $isSelected)); } } else { $obj = $this->createOption((string) $item, $key, $isSelected); } $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 (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; } }