1212class Document
1313{
1414 public const TAG_NO_ENDING = [
15- "meta " , "link " , "img " , "br " , "hr " , "input " , "keygen " , "param " , "source " , "track " , "embed "
15+ "meta " ,
16+ "link " ,
17+ "img " ,
18+ "br " ,
19+ "hr " ,
20+ "input " ,
21+ "keygen " ,
22+ "param " ,
23+ "source " ,
24+ "track " ,
25+ "embed "
1626 ];
1727
1828 protected $ elements ;
1929 private $ html ;
20- private $ el ;
30+ private $ elem ;
2131 private static $ inst ;
2232
33+ public function __toString ()
34+ {
35+ return $ this ->get ();
36+ }
37+
38+ /**
39+ * Get get Dom/document (Will only trigger execute once per instance)
40+ * @return string
41+ */
42+ public function get ()
43+ {
44+ if (is_null ($ this ->html )) {
45+ $ this ->execute ();
46+ }
47+ return $ this ->html ;
48+ }
49+
2350 /**
2451 * Init DOM instance
2552 * @param string $key DOM access key
@@ -43,15 +70,13 @@ public static function dom(string $key)
4370 public function bindTag (string $ tag , string $ key , bool $ prepend = false )
4471 {
4572 if ($ prepend ) {
46- $ this ->el = $ this ->createPrepend ($ tag , null , $ key );
73+ $ this ->elem = $ this ->createPrepend ($ tag , null , $ key );
4774 } else {
48- $ this ->el = $ this ->create ($ tag , null , $ key );
75+ $ this ->elem = $ this ->create ($ tag , null , $ key );
4976 }
50- return $ this ->el ;
77+ return $ this ->elem ;
5178 }
5279
53-
54-
5580 /**
5681 * Create (append) element
5782 * @param string $element HTML tag (without brackets)
@@ -98,9 +123,9 @@ public function createPrepend(string $element, ?string $value = null, ?string $b
98123 * Get one element from key
99124 * @return Response\Dom\Element
100125 */
101- public function getElement ($ k )
126+ public function getElement (string $ key )
102127 {
103- return ($ this ->elements [$ k ] ?? null );
128+ return ($ this ->elements [$ key ] ?? null );
104129 }
105130
106131 /**
@@ -112,7 +137,12 @@ public function getElements()
112137 return $ this ->elements ;
113138 }
114139
115- public function getTag (string $ key )
140+ /**
141+ * Get html tag
142+ * @param string $key
143+ * @return string|null
144+ */
145+ public function getTag (string $ key ): ?string
116146 {
117147 return ($ this ->el [$ key ] ?? null );
118148 }
@@ -125,62 +155,57 @@ public function getTag(string $key)
125155 public function execute (?callable $ call = null )
126156 {
127157 $ this ->html = "" ;
128- if (is_null ($ this ->elements ) && ($ inst = $ this ->withElement ())) {
129- $ this ->elements [] = $ inst ;
158+ if (is_null ($ this ->elements )) {
159+ if (method_exists ($ this , "withElement " )) {
160+ $ inst = $ this ->withElement ();
161+ $ this ->elements [] = $ inst ;
162+ }
130163 }
131164 if (is_array ($ this ->elements )) {
132165 $ this ->build ($ this ->elements , $ call );
133166 }
134167 return $ this ->html ;
135168 }
136169
137- /**
138- * Get get Dom/document (Will only trigger execute once per instance)
139- * @return string
140- */
141- public function get ()
142- {
143- if (is_null ($ this ->html )) {
144- $ this ->execute ();
145- }
146- return $ this ->html ;
147- }
148-
149-
150- public function __toString ()
170+ protected function elemHasEnding (string $ elem ): bool
151171 {
152- return $ this -> get ( );
172+ return ( bool )( in_array ( $ elem , $ this :: TAG_NO_ENDING ) );
153173 }
154174
155-
156175 /**
157176 * Build document
158177 * @param array $arr elements
159178 * @param callable|null $call Can be used to manipulate element within feed
179+ * @return void
160180 */
161- private function build (array $ arr , ?callable $ call = null )
181+ private function build (array $ arr , ?callable $ call = null ): void
162182 {
163- foreach ($ arr as $ k => $ a ) {
164- $ hasNoEnding = in_array ($ a ->getEl (), $ this ::TAG_NO_ENDING );
165- if (!is_null ($ call )) {
166- $ call ($ a , $ k , $ hasNoEnding );
167- }
183+ foreach ($ arr as $ key => $ elemObj ) {
184+ $ hasNoEnding = $ this ->elemHasEnding ($ elemObj ->getEl ());
185+ $ this ->buildCallable ($ elemObj , $ key , $ hasNoEnding , $ call );
168186
169- if (!$ a ->hideTagValid ()) {
170- $ this ->html .= "\t< " . $ a ->getEl (). $ a ->buildAttr (). "> " ;
187+ if (!$ elemObj ->hideTagValid ()) {
188+ $ this ->html .= "\t< " . $ elemObj ->getEl () . $ elemObj ->buildAttr () . "> " ;
171189 }
172190 if (!$ hasNoEnding ) {
173- $ this ->html .= $ a ->getValue ();
191+ $ this ->html .= $ elemObj ->getValue ();
174192 }
175- if (isset ($ a ->elements )) {
176- $ this ->build ($ a ->elements , $ call );
193+ if (isset ($ elemObj ->elements )) {
194+ $ this ->build ($ elemObj ->elements , $ call );
177195 }
178- if (!$ hasNoEnding && !$ a ->hideTagValid ()) {
179- $ this ->html .= "</ " . $ a ->getEl (). "> \n" ;
196+ if (!$ hasNoEnding && !$ elemObj ->hideTagValid ()) {
197+ $ this ->html .= "</ " . $ elemObj ->getEl () . "> \n" ;
180198 }
181- if ($ hasNoEnding && !$ a ->hideTagValid ()) {
199+ if ($ hasNoEnding && !$ elemObj ->hideTagValid ()) {
182200 $ this ->html .= "\n" ;
183201 }
184202 }
185203 }
204+
205+ private function buildCallable ($ elemObj , $ key , $ hasNoEnding , ?callable $ call ): void
206+ {
207+ if (!is_null ($ call )) {
208+ $ call ($ elemObj , $ key , $ hasNoEnding );
209+ }
210+ }
186211}
0 commit comments