Skip to content

Commit 5838b22

Browse files
committed
incorporate Shoop more
1 parent 9efa1b6 commit 5838b22

File tree

4 files changed

+76
-54
lines changed

4 files changed

+76
-54
lines changed

.editorconfig

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ root = true
22

33
[*]
44
indent_style = tab
5+
indent_size = 4
56
end_of_line = lf
67
charset = utf-8
78
trim_trailing_whitespace = true
@@ -11,5 +12,6 @@ insert_final_newline = true
1112
indent_style = space
1213
indent_size = 4
1314

14-
[*.yml, *.js, *.json]
15+
[*{.js,.json,.scss}]
16+
indent_style = space
1517
indent_size = 2

composer.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Element.php

+45-40
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class Element
1818

1919
protected $extends;
2020

21-
protected $omitEndTag = false;
21+
protected $attributes;
2222

23-
protected $attributes = [];
23+
protected $omitEndTag = false;
2424

2525
static public function fold($element, ...$content)
2626
{
@@ -32,47 +32,41 @@ public function __construct($element, ...$content)
3232
$this->element = Type::sanitizeType($element, ESString::class)
3333
->replace(["_" => "-"]);
3434
$this->content = Type::sanitizeType($content, ESArray::class);
35+
$this->attributes = Shoop::dictionary([]);
3536
}
3637

37-
public function unfold()
38+
public function unfold(string ...$attributes)
3839
{
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();
40+
$this->attr(...$attributes);
41+
$elem = $this->compiledElement();
42+
$attr = $this->compiledAttributes();
43+
$cont = $this->compiledContent();
44+
return Shoop::string($elem)->start("<")->plus($attr)->end(">")
45+
->plus($cont)->plus(
46+
($this->omitEndTag)
47+
? ""
48+
: Shoop::string($elem)->start("</")->end(">")
49+
)->unfold();
5450
}
5551

5652
public function attr(string ...$attributes): Element
5753
{
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-
68-
}
54+
Shoop::array($attributes)->each(function($string) {
55+
list($attribute, $value) = Shoop::string($string)
56+
->divide(" ", false, 2);
57+
$this->attributes = $this->attributes->plus($value, $attribute);
58+
});
6959
return $this;
7060
}
7161

7262
public function extends($extends): Element
7363
{
7464
$extends = Type::sanitizeType($extends, ESString::class);
75-
$this->extends = $extends;
65+
$elem = $this->element;
66+
67+
$this->element = $extends;
68+
$this->attr("is {$elem}");
69+
7670
return $this;
7771
}
7872

@@ -94,16 +88,27 @@ protected function compiledElement()
9488

9589
protected function compiledAttributes(): string
9690
{
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 "";
91+
return $this->attributes->each(function($value, $attribute) {
92+
return ($attribute === $value && strlen($attribute) > 0)
93+
? $attribute : "{$attribute}=\"{$value}\"";
94+
95+
})->isEmpty(function($result, $array) {
96+
return ($result) ? "" : $array->join(" ")->start(" ");
97+
98+
});
99+
}
100+
101+
protected function compiledContent()
102+
{
103+
return $this->content->each(function($value) {
104+
if (is_string($value) || is_int($value)) {
105+
return (string) $value;
106+
107+
} elseif (is_a($value, Element::class) || is_subclass_of($value, Element::class)) {
108+
return $value->unfold();
109+
110+
}
111+
})->join("");
107112
}
108113

109114
public function __toString()

tests/element/ElementTest.php

+23-8
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public function testHtmlComponent()
1515
$expected = '<html></html>';
1616

1717
$result = Element::fold("html");
18-
$this->assertEquals($expected, $result);
19-
20-
$result = Element::fold("html")->unfold('id my-component');
21-
$this->assertEquals($expected, $result);
18+
$this->assertSame($expected, $result->unfold());
2219

2320
$expected = '<html id="my-component"></html>';
21+
$result = Element::fold("html")->unfold("id my-component");
22+
$this->assertSame($expected, $result);
23+
2424
$result = Element::fold("html")->attr("id my-component")->unfold();
25-
$this->assertEquals($expected, $result);
25+
$this->assertSame($expected, $result);
2626
}
2727

2828
public function testParagraphSpanComponent()
@@ -31,15 +31,15 @@ public function testParagraphSpanComponent()
3131
$result = Element::fold("p",
3232
Element::fold("span", "Hello, World!")
3333
)->unfold();
34-
$this->assertEquals($expected, $result);
34+
$this->assertSame($expected, $result);
3535
}
3636

3737
public function testButtonWebComponentExtension()
3838
{
3939
$expected = '<button is="my-button">Save</button>';
4040
$result = Element::fold("my_button", "Save")
4141
->extends('button')->unfold();
42-
$this->assertEquals($expected, $result);
42+
$this->assertSame($expected, $result);
4343
}
4444

4545
public function testPage()
@@ -61,6 +61,21 @@ public function testPage()
6161
, '<p>Done!</p>'
6262
)
6363
)->unfold();
64-
$this->assertEquals($expected, $result);
64+
$this->assertSame($expected, $result);
65+
}
66+
67+
public function testAttributes()
68+
{
69+
$expected = '<container id="hello">';
70+
$actual = Element::fold("container")->attr("id hello")->omitEndTag();
71+
$this->assertSame($expected, $actual->unfold());
72+
73+
$expected = '<container id="goodbye">';
74+
$actual = $actual->attr("id goodbye");
75+
$this->assertSame($expected, $actual->unfold());
76+
77+
$expected = '<container id="hi">';
78+
$actual = $actual->unfold("id hi");
79+
$this->assertSame($expected, $actual);
6580
}
6681
}

0 commit comments

Comments
 (0)