12
12
13
13
class Element
14
14
{
15
- const openingFormat = " <%s%s> " ;
15
+ protected $ element ;
16
16
17
- protected $ element = '' ;
17
+ protected $ content ;
18
18
19
- protected $ content = [];
20
-
21
- protected $ extends = '' ;
19
+ protected $ extends ;
22
20
23
21
protected $ omitEndTag = false ;
24
22
@@ -32,70 +30,41 @@ static public function fold($element, ...$content)
32
30
public function __construct ($ element , ...$ content )
33
31
{
34
32
$ 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);
38
35
}
39
36
40
37
public function unfold ()
41
38
{
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 ();
90
54
}
91
55
92
56
public function attr (string ...$ attributes ): Element
93
57
{
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
+
99
68
}
100
69
return $ this ;
101
70
}
@@ -113,6 +82,30 @@ public function omitEndTag(bool $omit = true): Element
113
82
return $ this ;
114
83
}
115
84
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
+
116
109
public function __toString ()
117
110
{
118
111
return $ this ->unfold ();
0 commit comments