Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 7c9bc0f

Browse files
committed
"Modernized" Element, HtmlElement - deprecated AtriibuteHandler
1 parent fe5f96d commit 7c9bc0f

File tree

3 files changed

+162
-280
lines changed

3 files changed

+162
-280
lines changed

src/Element.php

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212

1313
class Element
1414
{
15-
const openingFormat = "<%s%s>";
15+
protected $element;
1616

17-
protected $element = '';
17+
protected $content;
1818

19-
protected $content = [];
20-
21-
protected $extends = '';
19+
protected $extends;
2220

2321
protected $omitEndTag = false;
2422

@@ -32,70 +30,41 @@ static public function fold($element, ...$content)
3230
public function __construct($element, ...$content)
3331
{
3432
$this->element = Type::sanitizeType($element, ESString::class)
35-
->replace("_", "-")->unfold();
36-
$this->content = Type::sanitizeType($content, ESArray::class)
37-
->unfold();
33+
->replace("_", "-");
34+
$this->content = Type::sanitizeType($content, ESArray::class);
3835
}
3936

4037
public function unfold()
4138
{
42-
$elem = $this->element;
43-
if (strlen($this->extends) > 0) {
44-
$elem = $this->extends;
45-
$this->attr("is {$this->element}");
46-
}
47-
48-
// $attributes = Shoop::dictionary($this->attributes)
49-
// ->each(function($value, $key) {
50-
// var_dump($value);
51-
// var_dump($key);
52-
// return "{$key}=\"{$value}\"";
53-
// })->join(" ")->unfold();
54-
// var_dump($attributes);
55-
56-
$attributes = Shoop::array([]);
57-
foreach ($this->attributes as $key => $value) {
58-
// TODO: Write test regarding single attributes like required
59-
if ($key === $value && strlen($value) > 0) {
60-
$attributes = $attributes->plus($value);
61-
62-
} else {
63-
$attributes = $attributes->plus("{$key}=\"{$value}\"");
64-
65-
}
66-
}
67-
$attributes = $attributes->join(" ");
68-
if (strlen($attributes) > 0) {
69-
$attributes = $attributes->start(" ");
70-
}
71-
72-
$opening = "<{$elem}{$attributes}>";
73-
74-
$content = "";
75-
foreach ($this->content as $value) {
76-
if (is_string($value) || is_int($value)) {
77-
$content .= (string) $value;
78-
79-
} elseif (is_a($value, Element::class) || is_subclass_of($value, Element::class)) {
80-
$content .= $value->unfold();
81-
82-
}
83-
}
84-
85-
$closing = "</{$elem}>";
86-
if ($this->omitEndTag) {
87-
$closing = "";
88-
}
89-
return $opening . $content . $closing;
39+
return Shoop::string($this->compiledElement())->start("<")->plus(
40+
$this->compiledAttributes()
41+
)->end(">")->plus(
42+
$this->content->each(function($value) {
43+
if (is_string($value) || is_int($value)) {
44+
return (string) $value;
45+
46+
} elseif (is_a($value, Element::class) || is_subclass_of($value, Element::class)) {
47+
return $value->unfold();
48+
49+
}
50+
})->join("")
51+
)->plus(
52+
($this->omitEndTag) ? "" : Shoop::string($this->compiledElement())->start("</")->end(">")
53+
)->unfold();
9054
}
9155

9256
public function attr(string ...$attributes): Element
9357
{
94-
foreach ($attributes as $attribute) {
95-
if (strlen($attribute) > 0) {
96-
list($key, $value) = explode(" ", $attribute, 2);
97-
$this->attributes[$key] = $value;
98-
}
58+
if ($this->attributes === null) {
59+
$this->attributes = Shoop::array($attributes)->unfold();
60+
61+
} else {
62+
$current = $this->attributes;
63+
$new = $attributes;
64+
$merged = array_merge($current, $new);
65+
$unique = array_unique($merged);
66+
$this->attributes = Shoop::array($unique)->unfold();
67+
9968
}
10069
return $this;
10170
}
@@ -113,6 +82,30 @@ public function omitEndTag(bool $omit = true): Element
11382
return $this;
11483
}
11584

85+
protected function compiledElement()
86+
{
87+
$elem = $this->element;
88+
if (strlen($this->extends) > 0) {
89+
$elem = $this->extends;
90+
$this->attributes[] = "is {$this->element}";
91+
}
92+
return $elem;
93+
}
94+
95+
protected function compiledAttributes(): string
96+
{
97+
$compiled = Shoop::array($this->attributes)->each(function($attribute) {
98+
list($member, $value) = explode(" ", $attribute, 2);
99+
return ($member === $value && strlen($member) > 0)
100+
? $member : "{$member}=\"{$value}\"";
101+
});
102+
103+
if ($compiled->int()->isGreaterThanUnfolded(0)) {
104+
return $compiled->join(" ")->start(" ");
105+
}
106+
return "";
107+
}
108+
116109
public function __toString()
117110
{
118111
return $this->unfold();

src/Html/Elements/AttributeHandler.php

Lines changed: 0 additions & 170 deletions
This file was deleted.

0 commit comments

Comments
 (0)