HtmlElement::setAttribute()   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
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark;
16
17
use League\CommonMark\Util\Xml;
18
19
class HtmlElement
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $tagName;
25
26
    /**
27
     * @var string[]
28
     */
29
    protected $attributes = [];
30
31
    /**
32
     * @var HtmlElement|HtmlElement[]|string
33
     */
34
    protected $contents;
35
36
    /**
37
     * @var bool
38
     */
39
    protected $selfClosing = false;
40
41
    /**
42
     * @param string                                $tagName     Name of the HTML tag
43
     * @param string[]                              $attributes  Array of attributes (values should be unescaped)
44
     * @param HtmlElement|HtmlElement[]|string|null $contents    Inner contents, pre-escaped if needed
45
     * @param bool                                  $selfClosing Whether the tag is self-closing
46
     */
47 2100
    public function __construct(string $tagName, array $attributes = [], $contents = '', bool $selfClosing = false)
48
    {
49 2100
        $this->tagName = $tagName;
50 2100
        $this->attributes = $attributes;
51 2100
        $this->selfClosing = $selfClosing;
52
53 2100
        $this->setContents($contents ?? '');
54 2100
    }
55
56
    /**
57
     * @return string
58
     */
59 93
    public function getTagName(): string
60
    {
61 93
        return $this->tagName;
62
    }
63
64
    /**
65
     * @return string[]
66
     */
67 57
    public function getAllAttributes(): array
68
    {
69 57
        return $this->attributes;
70
    }
71
72
    /**
73
     * @param string $key
74
     *
75
     * @return string|null
76
     */
77 63
    public function getAttribute(string $key): ?string
78
    {
79 63
        if (!isset($this->attributes[$key])) {
80 21
            return null;
81
        }
82
83 57
        return $this->attributes[$key];
84
    }
85
86
    /**
87
     * @param string $key
88
     * @param string $value
89
     *
90
     * @return $this
91
     */
92 6
    public function setAttribute(string $key, string $value): self
93
    {
94 6
        $this->attributes[$key] = $value;
95
96 6
        return $this;
97
    }
98
99
    /**
100
     * @param bool $asString
101
     *
102
     * @return HtmlElement|HtmlElement[]|string
103
     */
104 2031
    public function getContents(bool $asString = true)
105
    {
106 2031
        if (!$asString || \is_string($this->contents)) {
107 2019
            return $this->contents;
108
        }
109
110 252
        if (\is_array($this->contents)) {
111 3
            return \implode('', $this->contents);
112
        }
113
114 249
        return (string) $this->contents;
115
    }
116
117
    /**
118
     * Sets the inner contents of the tag (must be pre-escaped if needed)
119
     *
120
     * @param HtmlElement|HtmlElement[]|string $contents
121
     *
122
     * @return $this
123
     */
124 2100
    public function setContents($contents): self
125
    {
126 2100
        $this->contents = $contents ?? '';
127
128 2100
        return $this;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 1983
    public function __toString(): string
135
    {
136 1983
        $result = '<' . $this->tagName;
137
138 1983
        foreach ($this->attributes as $key => $value) {
139 444
            $result .= ' ' . $key . '="' . Xml::escape($value) . '"';
140
        }
141
142 1983
        if ($this->contents !== '') {
143 1944
            $result .= '>' . $this->getContents() . '</' . $this->tagName . '>';
144 228
        } elseif ($this->selfClosing) {
145 192
            $result .= ' />';
146
        } else {
147 39
            $result .= '></' . $this->tagName . '>';
148
        }
149
150 1983
        return $result;
151
    }
152
}
153