diff --git a/src/Common/UiElement.php b/src/Common/UiElement.php index 81dcfaa..b3304da 100644 --- a/src/Common/UiElement.php +++ b/src/Common/UiElement.php @@ -2,20 +2,35 @@ namespace Picea\Ui\Common; +enum UiElementInsertMode +{ + case append; + case prepend; +} + class UiElement implements \JsonSerializable { - - static array $config = []; - - const INSERT_MODE_APPEND = 1; - const INSERT_MODE_PREPEND = 2; - - public string $tag = 'div'; - - public array $attributes = [ - 'style' => [], - 'class' => [], + public const TAG_CONFIG_OPTIONS = [ + "!doctype" => [ "attributes" => [ "html" ], "options" => [ "tag-type" => "single" ] ], + "js" => [ "tag" => "script", "attributes" => [ "type" => "text/javascript" ] ], + "css" => [ "tag" => "link", "attributes" => [ "rel" => "stylesheet" , "type" => "text/css" ] ], + "document" => [ "options" => [ "no-tag" => true ] ], + "link" => [ "options" => [ "tag-type" => "single" ] ], + "meta" => [ "options" => [ "tag-type" => "single" ] ], + "img" => [ "options" => [ "tag-type" => "single" ] ], + "input" => [ "options" => [ "tag-type" => "single" ] ], + "br" => [ "options" => [ "tag-type" => "single" ] ], + "hr" => [ "options" => [ "tag-type" => "single" ] ], + "iframe" => [ "options" => [ "tag-type" => "single" ] ], + "area" => [ "options" => [ "tag-type" => "single" ] ], + "col" => [ "options" => [ "tag-type" => "single" ] ], + "frame" => [ "options" => [ "tag-type" => "single" ] ], + "param" => [ "options" => [ "tag-type" => "single" ] ], + "video" => [ "options" => [ "tag-type" => "single" ] ], + "wbr" => [ "options" => [ "tag-type" => "single" ] ] ]; + public array $attributes = [ 'style' => [], 'class' => [], ]; + public array $childs = []; /** @@ -33,26 +48,11 @@ class UiElement implements \JsonSerializable { public array $options = []; - public $selected = null; - public string $content = ""; 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 - { - static::$config = array_replace_recursive(static::$config, $array); - } + public string $tag = "div", + ) {} public static function stylesheet(string $href, $attributes = [], $options = []) : self { @@ -108,7 +108,7 @@ class UiElement implements \JsonSerializable { $obj->attributes($attributes); } - if ( false !== ( $custom = static::$config["tags"][$tag] ?? false ) ) { + if ( false !== ( $custom = static::TAG_CONFIG_OPTIONS[strtolower($tag)] ?? false ) ) { if ( $custom['tag'] ?? false ) { $obj->tag = $custom['tag']; } @@ -252,23 +252,23 @@ class UiElement implements \JsonSerializable { public function append( ...$arguments ) : self { - return $this->insert( static::INSERT_MODE_APPEND, ...$arguments); + return $this->insert( UiElementInsertMode::append, ...$arguments); } public function prepend( ...$arguments ) : self { - return $this->insert( static::INSERT_MODE_PREPEND, ...( is_array($arguments) ? array_reverse($arguments) : $arguments) ); + return $this->insert( UiElementInsertMode::prepend, ...( is_array($arguments) ? array_reverse($arguments) : $arguments) ); } - protected function insert(int $mode, ...$elements) : self + protected function insert(UiElementInsertMode $mode, ...$elements) : self { foreach($elements as $item) { switch($mode) { - case static::INSERT_MODE_APPEND: + case UiElementInsertMode::append: array_push($this->childs, $item); break; - case static::INSERT_MODE_PREPEND: + case UiElementInsertMode::prepend: array_unshift($this->childs, $item); break; } diff --git a/src/Component/UiPopup.php b/src/Component/UiPopup.php index 0323f7f..71e6981 100644 --- a/src/Component/UiPopup.php +++ b/src/Component/UiPopup.php @@ -11,12 +11,15 @@ class UiPopup extends UiElement implements Extension { public string $token = "ui:popup"; - public string $tag = "div"; - public array $attributes = [ 'class' => 'ui-popup', ]; + public function __construct( + public string $tag = "div", + ) {} + + public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { return "buildAttributes($arguments) . '\"' ?>"; diff --git a/src/Form/UiForm.php b/src/Form/UiForm.php index c513943..546a730 100644 --- a/src/Form/UiForm.php +++ b/src/Form/UiForm.php @@ -13,18 +13,15 @@ class UiForm extends UiElement implements Extension { public array $token = [ "ui:form", "ui:endform" ]; - public string $tag = "form"; - public array $attributes = [ 'class' => 'ui-form', ]; public function __construct( + public string $tag = "form", public bool $enctype = true, public bool $csrf = true, - ) { - parent::__construct(null); - } + ) {} public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { diff --git a/src/Form/UiInput.php b/src/Form/UiInput.php index f8cd2ce..96a3dca 100644 --- a/src/Form/UiInput.php +++ b/src/Form/UiInput.php @@ -11,8 +11,6 @@ class UiInput extends UiElement implements Extension { public string $token = "ui:input"; - public string $tag = "input"; - public array $attributes = [ 'class' => 'ui-input', ]; @@ -24,7 +22,11 @@ class UiInput extends UiElement implements Extension { protected /* ? mixed */ $value; protected string $name; - + + public function __construct( + public string $tag = "input", + ) {} + public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { return "buildHtml($arguments) ?>"; diff --git a/src/Form/UiSelect.php b/src/Form/UiSelect.php index 3890e87..168c60f 100644 --- a/src/Form/UiSelect.php +++ b/src/Form/UiSelect.php @@ -11,12 +11,14 @@ class UiSelect extends UiElement implements Extension { public string $token = "ui:select"; - public string $tag = "select"; - public array $attributes = [ 'class' => '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) ?>"; diff --git a/src/Form/UiTextarea.php b/src/Form/UiTextarea.php index 6cb5da2..ab58fa6 100644 --- a/src/Form/UiTextarea.php +++ b/src/Form/UiTextarea.php @@ -6,8 +6,6 @@ class UiTextarea extends UiInput { public array $tokens = [ "ui:textarea" ]; - public string $tag = "textarea"; - public array $attributes = [ 'class' => "ui-textarea", ]; @@ -17,10 +15,9 @@ class UiTextarea extends UiInput { protected bool $echoRaw = false; public function __construct( - public bool $raw = false - ) { - parent::__construct(null); - } + public string $tag = "textarea", + public bool $raw = false, + ) {} public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {