Tag::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JoaoRobertoPB\Htmlable;
4
5
use JoaoRobertoPB\Htmlable\Contracts\Htmlable;
6
use JoaoRobertoPB\Htmlable\Traits\HasAttributes;
7
use JoaoRobertoPB\Htmlable\Contracts\HtmlAttributes;
8
9
/**
10
* Class Tag.
11
*/
12
class Tag implements Htmlable, HtmlAttributes
13
{
14
    use HasAttributes;
15
16
    /**
17
    * @var string
18
    */
19
    private $tag;
20
21
    /**
22
    * @var array
23
    */
24
    protected $contents;
25
26
    /**
27
    * @param string $tagName
28
    * @param array $attributes
29
    */
30 33
    public function __construct($tagName, $attributes = [])
31
    {
32 33
        $this->tag = trim($tagName);
33 33
        $this->setAttributes($attributes);
34 33
        $this->contents = [];
35 33
    }
36
37
    /**
38
    * @param string $tagName
39
    * @param array $attributes
40
    */
41 6
    public static function make($tagName, $attributes = [])
42
    {
43 6
        return new self($tagName, $attributes);
44
    }
45
46
    /**
47
    * @return void
48
    */
49 33
    private function openTag()
50
    {
51 33
        echo ($this->attributesIsEmpty()) ?
52 27
            "<{$this->tag}>" :
53 18
            "<{$this->tag} {$this->attributesToString()}>";
54 33
    }
55
56
    /**
57
    * @return void
58
    */
59 27
    private function closeTag()
60
    {
61 27
        echo "</{$this->tag}>";
62 27
    }
63
64
    /**
65
    * @param  String    $contents
66
    * @return void
67
    */
68 21
    public function add($contents)
69
    {
70 21
        array_push($this->contents, $contents);
71
72 21
        return $this;
73
    }
74
75
    /**
76
    * @return void
77
    */
78 33
    public function render()
79
    {
80 33
        $this->openTag();
81
82 33
        foreach ($this->contents as $child) {
83 21
            if ($child instanceof Htmlable) {
84 6
                $child->render();
85 4
            } else {
86 19
                echo $child;
87
            }
88 22
        }
89
90 33
        if (! $this->isSelfClosingTag()) {
91 27
            $this->closeTag();
92 18
        }
93 33
    }
94
95
    /**
96
    * @return bool
97
    */
98 33
    protected function isSelfClosingTag()
99
    {
100 33
        return in_array(strtolower($this->tag), [
101 33
            'area', 'base', 'br', 'col', 'embed', 'hr',
102 22
            'img', 'input', 'keygen', 'link', 'menuitem',
103 22
            'meta', 'param', 'source', 'track', 'wbr',
104 22
        ]);
105
    }
106
107
    /**
108
    * Returns the element html as a string
109
    * @return  string
110
    */
111 33
    public function toHtml()
112
    {
113 33
        ob_start();
114 33
        $this->render();
115 33
        $html = ob_get_contents();
116 33
        ob_end_clean();
117
118 33
        return $html;
119
    }
120
121
    /**
122
    * @return string
123
    */
124
    public function __toString()
125
    {
126
        return $this->toHtml();
127
    }
128
}
129