Passed
Push — latest ( d59ff4...6fb795 )
by Colin
08:12
created

HtmlElement::__toString()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

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