1 | <?php |
||
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 |
|
63 | |||
64 | /** |
||
65 | * @return string[] |
||
66 | */ |
||
67 | 57 | public function getAllAttributes(): array |
|
71 | |||
72 | /** |
||
73 | * @param string $key |
||
74 | * |
||
75 | * @return string|null |
||
76 | */ |
||
77 | 63 | public function getAttribute(string $key): ?string |
|
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 |
|
98 | |||
99 | /** |
||
100 | * @param bool $asString |
||
101 | * |
||
102 | * @return HtmlElement|HtmlElement[]|string |
||
103 | */ |
||
104 | 2031 | public function getContents(bool $asString = true) |
|
112 | |||
113 | /** |
||
114 | 249 | * 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 | public function setContents($contents): self |
||
126 | 2100 | ||
127 | /** |
||
128 | 2100 | * @return string |
|
129 | */ |
||
130 | public function __toString(): string |
||
148 | |||
149 | private function getContentsAsString(): string |
||
161 | } |
||
162 |