Completed
Push — master ( 7a0a03...73b89e )
by Colin
02:28
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\Util;
16
17
class HtmlElement
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $tagName;
23
24
    /**
25
     * @var string[]
26
     */
27
    protected $attributes = [];
28
29
    /**
30
     * @var HtmlElement|HtmlElement[]|string
31
     */
32
    protected $contents;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $selfClosing = false;
38
39
    /**
40
     * @param string                                $tagName     Name of the HTML tag
41
     * @param string[]                              $attributes  Array of attributes (values should be unescaped)
42
     * @param HtmlElement|HtmlElement[]|string|null $contents    Inner contents, pre-escaped if needed
43
     * @param bool                                  $selfClosing Whether the tag is self-closing
44
     */
45 2544
    public function __construct(string $tagName, array $attributes = [], $contents = '', bool $selfClosing = false)
46
    {
47 2544
        $this->tagName = $tagName;
48 2544
        $this->attributes = $attributes;
49 2544
        $this->selfClosing = $selfClosing;
50
51 2544
        $this->setContents($contents ?? '');
52 2544
    }
53
54 102
    public function getTagName(): string
55
    {
56 102
        return $this->tagName;
57
    }
58
59
    /**
60
     * @return string[]
61
     */
62 60
    public function getAllAttributes(): array
63
    {
64 60
        return $this->attributes;
65
    }
66
67 69
    public function getAttribute(string $key): ?string
68
    {
69 69
        if (!isset($this->attributes[$key])) {
70 24
            return null;
71
        }
72
73 63
        return $this->attributes[$key];
74
    }
75
76 21
    public function setAttribute(string $key, string $value): self
77
    {
78 21
        $this->attributes[$key] = $value;
79
80 21
        return $this;
81
    }
82
83
    /**
84
     * @param bool $asString
85
     *
86
     * @return HtmlElement|HtmlElement[]|string
87
     */
88 93
    public function getContents(bool $asString = true)
89
    {
90 93
        if (!$asString) {
91 15
            return $this->contents;
92
        }
93
94 87
        return $this->getContentsAsString();
95
    }
96
97
    /**
98
     * Sets the inner contents of the tag (must be pre-escaped if needed)
99
     *
100
     * @param HtmlElement|HtmlElement[]|string $contents
101
     *
102
     * @return $this
103
     */
104 2544
    public function setContents($contents): self
105
    {
106 2544
        $this->contents = $contents ?? '';
107
108 2544
        return $this;
109
    }
110
111 2418
    public function __toString(): string
112
    {
113 2418
        $result = '<' . $this->tagName;
114
115 2418
        foreach ($this->attributes as $key => $value) {
116 681
            $result .= ' ' . $key . '="' . Xml::escape($value) . '"';
117
        }
118
119 2418
        if ($this->contents !== '') {
120 2379
            $result .= '>' . $this->getContentsAsString() . '</' . $this->tagName . '>';
121 261
        } elseif ($this->selfClosing && $this->tagName === 'input') {
122 9
            $result .= '>';
123 252
        } elseif ($this->selfClosing) {
124 198
            $result .= ' />';
125
        } else {
126 57
            $result .= '></' . $this->tagName . '>';
127
        }
128
129 2418
        return $result;
130
    }
131
132 2463
    private function getContentsAsString(): string
133
    {
134 2463
        if (\is_string($this->contents)) {
135 2451
            return $this->contents;
136
        }
137
138 255
        if (\is_array($this->contents)) {
139 3
            return \implode('', $this->contents);
140
        }
141
142 252
        return (string) $this->contents;
143
    }
144
}
145