Node::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BestServedCold\HTMLBuilder\Html;
4
5
use BestServedCold\HTMLBuilder\Html\Node\Children;
6
use BestServedCold\HTMLBuilder\Html\Node\Attribute;
7
use BestServedCold\HTMLBuilder\Html\Node\Override;
8
9
10
/**
11
 * Class Node
12
 *
13
 * @package BestServedCold\HTMLBuilder
14
 * @method $this accept($string = null)
15
 * @method $this accesskey($string = null)
16
 * @method $this action($string = null)
17
 * @method $this align($string = null)
18
 * @method $this alt($string = null)
19
 * @method $this async($string = null)
20
 * @method $this autocomplete($string = null)
21
 * @method $this autofocus($string = null)
22
 * @method $this autoplay($string = null)
23
 * @method $this autosave($string = null)
24
 * @method $this bgcolor($string = null)
25
 * @method $this border($string = null)
26
 * @method $this buffered($string = null)
27
 * @method $this challenge($string = null)
28
 * @method $this charset($string = null)
29
 * @method $this checked($string = null)
30
 * @method $this cite($string = null)
31
 * @method $this class($string = null)
32
 * @method $this code($string = null)
33
 * @method $this codebase($string = null)
34
 * @method $this color($string = null)
35
 * @method $this cols($string = null)
36
 * @method $this colspan($string = null)
37
 * @method $this contenteditable($string = null)
38
 * @method $this contextmenu($string = null)
39
 * @method $this controls($string = null)
40
 * @method $this coords($string = null)
41
 * @method $this datetime($string = null)
42
 * @method $this default($string = null)
43
 * @method $this defer($string = null)
44
 * @method $this dir($string = null)
45
 * @method $this dirname($string = null)
46
 * @method $this disabled($string = null)
47
 * @method $this download($string = null)
48
 * @method $this draggable($string = null)
49
 * @method $this dropzone($string = null)
50
 * @method $this enctype($string = null)
51
 * @method $this for($string = null)
52
 * @method $this form($string = null)
53
 * @method $this formaction($string = null)
54
 * @method $this headers($string = null)
55
 * @method $this height($string = null)
56
 * @method $this hidden($string = null)
57
 * @method $this high($string = null)
58
 * @method $this href($string = null)
59
 * @method $this hreflang($string = null)
60
 * @method $this http-equiv($string = null)
61
 * @method $this icon($string = null)
62
 * @method $this id($string = null)
63
 * @method $this integrity($string = null)
64
 * @method $this ismap($string = null)
65
 * @method $this itemprop($string = null)
66
 * @method $this keytype($string = null)
67
 * @method $this kind($string = null)
68
 * @method $this label($string = null)
69
 * @method $this lang($string = null)
70
 * @method $this language($string = null)
71
 * @method $this list($string = null)
72
 * @method $this loop($string = null)
73
 * @method $this low($string = null)
74
 * @method $this manifest($string = null)
75
 * @method $this max($string = null)
76
 * @method $this maxlength($string = null)
77
 * @method $this media($string = null)
78
 * @method $this method($string = null)
79
 * @method $this min($string = null)
80
 * @method $this multiple($string = null)
81
 * @method $this muted($string = null)
82
 * @method $this name($string = null)
83
 * @method $this novalidate($string = null)
84
 * @method $this onabort($string = null)
85
 * @method $this onautocomplete($string = null)
86
 * @method $this onautocompleteerror($string = null)
87
 * @method $this onblur($string = null)
88
 * @method $this oncancel($string = null)
89
 * @method $this oncanplay($string = null)
90
 * @method $this oncanplaythrough($string = null)
91
 * @method $this onchange($string = null)
92
 * @method $this onclick($string = null)
93
 * @method $this onclose($string = null)
94
 * @method $this oncontextmenu($string = null)
95
 * @method $this oncuechange($string = null)
96
 * @method $this ondblclick($string = null)
97
 * @method $this ondrag($string = null)
98
 * @method $this ondragend($string = null)
99
 * @method $this ondragenter($string = null)
100
 * @method $this ondragexit($string = null)
101
 * @method $this ondragleave($string = null)
102
 * @method $this ondragover($string = null)
103
 * @method $this ondragstart($string = null)
104
 * @method $this ondrop($string = null)
105
 * @method $this ondurationchange($string = null)
106
 * @method $this onemptied($string = null)
107
 * @method $this onended($string = null)
108
 * @method $this onerror($string = null)
109
 * @method $this onfocus($string = null)
110
 * @method $this oninput($string = null)
111
 * @method $this oninvalid($string = null)
112
 * @method $this onkeydown($string = null)
113
 * @method $this onkeypress($string = null)
114
 * @method $this onkeyup($string = null)
115
 * @method $this onload($string = null)
116
 * @method $this onloadeddata($string = null)
117
 * @method $this onloadedmetadata($string = null)
118
 * @method $this onloadstart($string = null)
119
 * @method $this onmousedown($string = null)
120
 * @method $this onmouseenter($string = null)
121
 * @method $this onmouseleave($string = null)
122
 * @method $this onmousemove($string = null)
123
 * @method $this onmouseout($string = null)
124
 * @method $this onmouseover($string = null)
125
 * @method $this onmouseup($string = null)
126
 * @method $this onmousewheel($string = null)
127
 * @method $this onpause($string = null)
128
 * @method $this onplay($string = null)
129
 * @method $this onplaying($string = null)
130
 * @method $this onprogress($string = null)
131
 * @method $this onratechange($string = null)
132
 * @method $this onreset($string = null)
133
 * @method $this onresize($string = null)
134
 * @method $this onscroll($string = null)
135
 * @method $this onseeked($string = null)
136
 * @method $this onseeking($string = null)
137
 * @method $this onselect($string = null)
138
 * @method $this onshow($string = null)
139
 * @method $this onsort($string = null)
140
 * @method $this onstalled($string = null)
141
 * @method $this onsubmit($string = null)
142
 * @method $this onsuspend($string = null)
143
 * @method $this ontimeupdate($string = null)
144
 * @method $this ontoggle($string = null)
145
 * @method $this onvolumechange($string = null)
146
 * @method $this onwaiting($string = null)
147
 * @method $this open($string = null)
148
 * @method $this optimum($string = null)
149
 * @method $this pattern($string = null)
150
 * @method $this ping($string = null)
151
 * @method $this placeholder($string = null)
152
 * @method $this poster($string = null)
153
 * @method $this preload($string = null)
154
 * @method $this radiogroup($string = null)
155
 * @method $this readonly($string = null)
156
 * @method $this rel($string = null)
157
 * @method $this required($string = null)
158
 * @method $this reversed($string = null)
159
 * @method $this rows($string = null)
160
 * @method $this rowspan($string = null)
161
 * @method $this sandbox($string = null)
162
 * @method $this scope($string = null)
163
 * @method $this scoped($string = null)
164
 * @method $this seamless($string = null)
165
 * @method $this selected($string = null)
166
 * @method $this shape($string = null)
167
 * @method $this size($string = null)
168
 * @method $this sizes($string = null)
169
 * @method $this slot($string = null)
170
 * @method $this span($string = null)
171
 * @method $this spellcheck($string = null)
172
 * @method $this src($string = null)
173
 * @method $this srcdoc($string = null)
174
 * @method $this srclang($string = null)
175
 * @method $this srcset($string = null)
176
 * @method $this start($string = null)
177
 * @method $this step($string = null)
178
 * @method $this style($string = null)
179
 * @method $this summary($string = null)
180
 * @method $this tabindex($string = null)
181
 * @method $this target($string = null)
182
 * @method $this title($string = null)
183
 * @method $this type($string = null)
184
 * @method $this usemap($string = null)
185
 * @method $this value($string = null)
186
 * @method $this width($string = null)
187
 * @method $this wrap($string = null)
188
 */
189
class Node
190
{
191
    use Override;
192
193
    /**
194
     * @var string $type
195
     */
196
    private $type;
197
198
    /**
199
     * @var array $value
200
     */
201
    private $content = [];
202
203
    /**
204
     * @var bool $void
205
     */
206
    private $void            = false;
207
208
    /**
209
     * @var array $children
210
     */
211
    private $children        = [];
212
213
    /**
214
     * @var array $attributes
215
     */
216
    private $attributes      = [];
217
218
    /**
219
     * Node constructor.
220
     *
221
     * @param string  $type
222
     * @param array   $children
223
     * @param boolean $void
224
     */
225 17
    public function __construct($type, $children = [], $void = false)
226
    {
227 17
        $this->type = $type;
228 17
        $this->void = $void;
229 17
        empty($children) ?: Children::run($this, $children);
230 17
    }
231
    
232
    /**
233
     * @param  string $name
234
     * @param  array  $arguments
235
     * @return Node
236
     * @throws \Exception
237
     */
238 5
    public function __call($name, $arguments)
239
    {
240 5
        if (! Attribute::isAttribute($name)) {
241 1
            throw new \Exception('Attribute [' . $name . '] is not valid.');
242
        }
243
244 4
        if (! Attribute::isElementAttribute($name, $this->getType())) {
245 1
            throw new \Exception(
246 1
                'Attribute [' . $name . '] is not a valid for element type [' . $this->getType() . '].'
247 1
            );
248
        }
249
250 3
        return $this->attribute($name, reset($arguments) ?: null);
251
    }
252
253
    /**
254
     * @param  array ...$content
255
     * @return $this
256
     */
257 2
    public function content(...$content)
258
    {
259 2
        $this->content = array_merge($this->content, $content);
260 2
        return $this;
261
    }
262
263
    /**
264
     * @return string|null
265
     */
266 3
    public function getContent()
267
    {
268 3
        return $this->content;
269
    }
270
271
    /**
272
     * @return array
273
     */
274 4
    public function getChildren()
275
    {
276 4
        return $this->children;
277
    }
278
279
    /**
280
     * @return bool
281
     */
282 1
    public function hasChildren()
283
    {
284 1
        return count($this->getChildren()) !== 0;
285
    }
286
287
    /**
288
     * @param  Node $node
289
     * @return $this
290
     */
291 9
    public function child(Node $node)
292
    {
293 9
        $this->children[] = $node;
294 9
        return $this;
295
    }
296
297
    /**
298
     * @param  string $attribute
299
     * @param  null   $value
300
     * @return $this
301
     */
302 5
    public function attribute($attribute, $value = null)
303
    {
304 5
        is_array($attribute)
305 5
            ? $this->attributeArray($attribute)
306 5
            : $this->attributes[$attribute] = $value;
307 5
        return $this;
308
    }
309
310
    /**
311
     * @param  array $attributes
312
     * @return $this
313
     */
314 1
    public function attributeArray(array $attributes)
315
    {
316 1
        foreach($attributes as $key => $child) {
317 1
            $this->attribute($key, $child);
318 1
        }
319
320 1
        return $this;
321
    }
322
323
    /**
324
     * @return string
325
     */
326 8
    public function getType()
327
    {
328 8
        return $this->type;
329
    }
330
331
    /**
332
     * @return array
333
     */
334 4
    public function getAttributes()
335
    {
336 4
        return $this->attributes;
337
    }
338
339
    /**
340
     * @return $this
341
     */
342 3
    public function void()
343
    {
344 3
        $this->void = true;
345 3
        return $this;
346
    }
347
348
    /**
349
     * @return bool
350
     */
351 3
    public function isVoid()
352
    {
353 3
        return $this->void;
354
    }
355
}
356