Completed
Push — master ( 83739a...a13ee4 )
by Colin
14s queued 11s
created

HtmlElement::__toString()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 8.9777
c 0
b 0
f 0
cc 6
nc 8
nop 0
crap 6
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 2469
    public function __construct(string $tagName, array $attributes = [], $contents = '', bool $selfClosing = false)
48
    {
49 2469
        $this->tagName = $tagName;
50 2469
        $this->attributes = $attributes;
51 2469
        $this->selfClosing = $selfClosing;
52
53 2469
        $this->setContents($contents ?? '');
54 2469
    }
55
56
    /**
57
     * @return string
58
     */
59 102
    public function getTagName(): string
60
    {
61 102
        return $this->tagName;
62
    }
63
64
    /**
65
     * @return string[]
66
     */
67 60
    public function getAllAttributes(): array
68
    {
69 60
        return $this->attributes;
70
    }
71
72
    /**
73
     * @param string $key
74
     *
75
     * @return string|null
76
     */
77 69
    public function getAttribute(string $key): ?string
78
    {
79 69
        if (!isset($this->attributes[$key])) {
80 24
            return null;
81
        }
82
83 63
        return $this->attributes[$key];
84
    }
85
86
    /**
87
     * @param string $key
88
     * @param string $value
89
     *
90
     * @return $this
91
     */
92 21
    public function setAttribute(string $key, string $value): self
93
    {
94 21
        $this->attributes[$key] = $value;
95
96 21
        return $this;
97
    }
98
99
    /**
100
     * @param bool $asString
101
     *
102
     * @return HtmlElement|HtmlElement[]|string
103
     */
104 93
    public function getContents(bool $asString = true)
105
    {
106 93
        if (!$asString) {
107 15
            return $this->contents;
108
        }
109
110 87
        return $this->getContentsAsString();
111
    }
112
113
    /**
114
     * Sets the inner contents of the tag (must be pre-escaped if needed)
115
     *
116
     * @param HtmlElement|HtmlElement[]|string $contents
117
     *
118
     * @return $this
119
     */
120 2469
    public function setContents($contents): self
121
    {
122 2469
        $this->contents = $contents ?? '';
123
124 2469
        return $this;
125
    }
126
127
    /**
128
     * @return string
129
     */
130 2343
    public function __toString(): string
131
    {
132 2343
        $result = '<' . $this->tagName;
133
134 2343
        foreach ($this->attributes as $key => $value) {
135 612
            $result .= ' ' . $key . '="' . Xml::escape($value) . '"';
136
        }
137
138 2343
        if ($this->contents !== '') {
139 2304
            $result .= '>' . $this->getContentsAsString() . '</' . $this->tagName . '>';
140 261
        } elseif ($this->selfClosing && $this->tagName === 'input') {
141 9
            $result .= '>';
142 252
        } elseif ($this->selfClosing) {
143 198
            $result .= ' />';
144
        } else {
145 57
            $result .= '></' . $this->tagName . '>';
146
        }
147
148 2343
        return $result;
149
    }
150
151 2388
    private function getContentsAsString(): string
152
    {
153 2388
        if (\is_string($this->contents)) {
154 2376
            return $this->contents;
155
        }
156
157 255
        if (\is_array($this->contents)) {
158 3
            return \implode('', $this->contents);
159
        }
160
161 252
        return (string) $this->contents;
162
    }
163
}
164