- Refractoring on the old code base

This commit is contained in:
Dave M. 2023-03-29 15:11:54 +00:00
parent 72940b3615
commit 6aaa05b37d
6 changed files with 53 additions and 52 deletions

View File

@ -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;
}

View File

@ -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 "<?php echo 'ui-popup=\"' . ( new \\" . static::class . "() )->buildAttributes($arguments) . '\"' ?>";

View File

@ -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
{

View File

@ -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 "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";

View File

@ -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 "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";

View File

@ -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
{