Completed
Pull Request — master (#2)
by Adam
01:53
created

Node   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Importance

Changes 0
Metric Value
wmc 21
lcom 4
cbo 3
dl 0
loc 173
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A __call() 0 14 4
A content() 0 5 1
A getContent() 0 4 1
A getFinalContent() 0 4 2
A getChildren() 0 4 1
A hasChildren() 0 4 1
A child() 0 5 1
A attribute() 0 5 2
A attributeArray() 0 8 2
A getType() 0 4 1
A getAttributes() 0 4 1
A void() 0 5 1
A isVoid() 0 4 1
1
<?php
2
3
namespace BestServedCold\HTMLBuilder\Html;
4
5
use BestServedCold\HTMLBuilder\Html\Node\Children,
6
    BestServedCold\HTMLBuilder\Html\Node\Attribute,
7
    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
0 ignored issues
show
Documentation introduced by
The doc-type $string could not be parsed: Unknown type name "$string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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
    public function __construct($type, $children = [], $void = false)
226
    {
227
        $this->type = $type;
228
        $this->void = $void;
229
        empty($children) ?: Children::run($this, $children);
230
    }
231
    
232
    /**
233
     * @param  string $name
234
     * @param  array  $arguments
235
     * @return Node
236
     * @throws \Exception
237
     */
238
    public function __call($name, $arguments)
239
    {
240
        if (! Attribute::isAttribute($name)) {
241
            throw new \Exception('Attribute [' . $name . '] is not valid.');
242
        }
243
244
        if (! Attribute::isElementAttribute($name, $this->getType())) {
245
            throw new \Exception(
246
                'Attribute [' . $name . '] is not a valid for element type [' . $this->getType() . '].'
247
            );
248
        }
249
250
        return $this->attribute($name, reset($arguments) ?: null);
251
    }
252
253
    /**
254
     * @param  array ...$content
255
     * @return $this
256
     */
257
    public function content(...$content)
258
    {
259
        $this->content = array_merge($this->content, $content);
260
        return $this;
261
    }
262
263
    /**
264
     * @return string|null
265
     */
266
    public function getContent()
267
    {
268
        return $this->content;
269
    }
270
271
    /**
272
     * @return mixed|null
273
     */
274
    public function getFinalContent()
275
    {
276
        return empty($this->content) ? null : array_pop($this->content);
277
    }
278
279
    /**
280
     * @return array
281
     */
282
    public function getChildren()
283
    {
284
        return $this->children;
285
    }
286
287
    /**
288
     * @return bool
289
     */
290
    public function hasChildren()
291
    {
292
        return count($this->getChildren()) !== 0;
293
    }
294
295
    /**
296
     * @param  Node $node
297
     * @return $this
298
     */
299
    public function child(Node $node)
300
    {
301
        $this->children[] = $node;
302
        return $this;
303
    }
304
305
    /**
306
     * @param  string $attribute
307
     * @param  null   $value
308
     * @return $this
309
     */
310
    public function attribute($attribute, $value = null)
311
    {
312
        is_array($attribute) ? $this->attributeArray($attribute) : $this->attributes[$attribute] = $value;
313
        return $this;
314
    }
315
316
    /**
317
     * @param  array $attributes
318
     * @return $this
319
     */
320
    public function attributeArray(array $attributes)
321
    {
322
        foreach($attributes as $key => $child) {
323
            $this->attribute($key, $child);
324
        }
325
326
        return $this;
327
    }
328
329
    /**
330
     * @return string
331
     */
332
    public function getType()
333
    {
334
        return $this->type;
335
    }
336
337
    /**
338
     * @return array
339
     */
340
    public function getAttributes()
341
    {
342
        return $this->attributes;
343
    }
344
345
    /**
346
     * @return $this
347
     */
348
    public function void()
349
    {
350
        $this->void = true;
351
        return $this;
352
    }
353
354
    /**
355
     * @return bool
356
     */
357
    public function isVoid()
358
    {
359
        return $this->void;
360
    }
361
}
362