Completed
Push — master ( 9eb60a...7a0a03 )
by Colin
36:39 queued 35:14
created

HtmlElement   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 1
dl 0
loc 128
ccs 40
cts 42
cp 0.9524
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getTagName() 0 4 1
A getAllAttributes() 0 4 1
A getAttribute() 0 8 2
A setAttribute() 0 6 1
A getContents() 0 8 2
A setContents() 0 6 1
B __toString() 0 20 6
A getContentsAsString() 0 12 3
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 147
    public function __construct(string $tagName, array $attributes = [], $contents = '', bool $selfClosing = false)
46
    {
47 147
        $this->tagName = $tagName;
48 147
        $this->attributes = $attributes;
49 147
        $this->selfClosing = $selfClosing;
50
51 147
        $this->setContents($contents ?? '');
52 147
    }
53
54 96
    public function getTagName(): string
55
    {
56 96
        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 57
    public function getAttribute(string $key): ?string
68
    {
69 57
        if (!isset($this->attributes[$key])) {
70 21
            return null;
71
        }
72
73 51
        return $this->attributes[$key];
74
    }
75
76 12
    public function setAttribute(string $key, string $value): self
77
    {
78 12
        $this->attributes[$key] = $value;
79
80 12
        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 147
    public function setContents($contents): self
105
    {
106 147
        $this->contents = $contents ?? '';
107
108 147
        return $this;
109
    }
110
111 33
    public function __toString(): string
112
    {
113 33
        $result = '<' . $this->tagName;
114
115 33
        foreach ($this->attributes as $key => $value) {
116 21
            $result .= ' ' . $key . '="' . Xml::escape($value) . '"';
117
        }
118
119 33
        if ($this->contents !== '') {
120 24
            $result .= '>' . $this->getContentsAsString() . '</' . $this->tagName . '>';
121 12
        } elseif ($this->selfClosing && $this->tagName === 'input') {
122
            $result .= '>';
123 12
        } elseif ($this->selfClosing) {
124 9
            $result .= ' />';
125
        } else {
126 6
            $result .= '></' . $this->tagName . '>';
127
        }
128
129 33
        return $result;
130
    }
131
132 108
    private function getContentsAsString(): string
133
    {
134 108
        if (\is_string($this->contents)) {
135 105
            return $this->contents;
136
        }
137
138 3
        if (\is_array($this->contents)) {
139 3
            return \implode('', $this->contents);
140
        }
141
142
        return (string) $this->contents;
143
    }
144
}
145