Passed
Push — main ( 10f25b...c1f98d )
by Sammy
09:23 queued 02:27
created

Element::is_void()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace HexMakina\Marker;
4
5
class Element
6
{
7
    const VOID_ELEMENTS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
8
9
    protected $tagName = '';
10
    protected $attributeList = [];
11
    protected $classList = [];
12
    protected $innerContent = '';
13
14
    public function is_void()
15
    {
16
        return in_array($this->tagName, self::VOID_ELEMENTS);
17
    }
18
19
    public function __construct($tagName, $innerContent = null, $attributeList = [])
20
    {
21
        $this->tagName = $tagName;
22
        $this->attributeList = $attributeList;
23
        $this->innerContent = $innerContent ?? '';
24
    }
25
26
    private function format_attributes()
27
    {
28
        $ret = '';
29
30
        foreach ($this->attributeList as $k => $v) {
31
            if (!is_null($v) && $v !== '' && !is_array($v)) {
32
                $ret .=  is_int($k) ? " $v" : sprintf(' %s="%s"', $k, $v);
33
            }
34
        }
35
36
        return $ret;
37
    }
38
39
    public function __toString()
40
    {
41
        $flattributes = $this->format_attributes();
42
43
        $ret = '';
44
        if ($this->is_void()) {
45
            $ret = sprintf('<%s%s/>', $this->tagName, $flattributes);
46
        } else {
47
            $ret = sprintf(
48
                '<%s%s>%s</%s>',
49
                $this->tagName,
50
                $flattributes,
51
                $this->innerContent,
52
                $this->tagName
53
            );
54
        }
55
56
        return $ret;
57
    }
58
}
59