@@ -18,9 +18,9 @@ class Element
18
18
19
19
protected $ extends ;
20
20
21
- protected $ omitEndTag = false ;
21
+ protected $ attributes ;
22
22
23
- protected $ attributes = [] ;
23
+ protected $ omitEndTag = false ;
24
24
25
25
static public function fold ($ element , ...$ content )
26
26
{
@@ -32,47 +32,41 @@ public function __construct($element, ...$content)
32
32
$ this ->element = Type::sanitizeType ($ element , ESString::class)
33
33
->replace (["_ " => "- " ]);
34
34
$ this ->content = Type::sanitizeType ($ content , ESArray::class);
35
+ $ this ->attributes = Shoop::dictionary ([]);
35
36
}
36
37
37
- public function unfold ()
38
+ public function unfold (string ... $ attributes )
38
39
{
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 ();
54
50
}
55
51
56
52
public function attr (string ...$ attributes ): Element
57
53
{
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
+ });
69
59
return $ this ;
70
60
}
71
61
72
62
public function extends ($ extends ): Element
73
63
{
74
64
$ 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
+
76
70
return $ this ;
77
71
}
78
72
@@ -94,16 +88,27 @@ protected function compiledElement()
94
88
95
89
protected function compiledAttributes (): string
96
90
{
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 ("" );
107
112
}
108
113
109
114
public function __toString ()
0 commit comments