1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\htmltag\Tag; |
4
|
|
|
|
5
|
|
|
use drupol\htmltag\AbstractBaseHtmlTagObject; |
6
|
|
|
use drupol\htmltag\Attributes\AttributesInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Tag. |
10
|
|
|
*/ |
11
|
|
|
class Tag extends AbstractBaseHtmlTagObject implements TagInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The tag name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $tag; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The tag attributes. |
22
|
|
|
* |
23
|
|
|
* @var \drupol\htmltag\Attributes\AttributesInterface |
24
|
|
|
*/ |
25
|
|
|
private $attributes; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The tag content. |
29
|
|
|
* |
30
|
|
|
* @var mixed[]|null |
31
|
|
|
*/ |
32
|
|
|
private $content; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Tag constructor. |
36
|
|
|
* |
37
|
|
|
* @param \drupol\htmltag\Attributes\AttributesInterface $attributes |
38
|
|
|
* The attributes object. |
39
|
|
|
* @param string $name |
40
|
|
|
* The tag name. |
41
|
|
|
* @param mixed $content |
42
|
|
|
* The content. |
43
|
|
|
*/ |
44
|
10 |
|
public function __construct(AttributesInterface $attributes, $name, $content = false) |
45
|
|
|
{ |
46
|
10 |
|
$this->tag = $name; |
47
|
10 |
|
$this->attributes = $attributes; |
48
|
10 |
|
$this->content($content); |
49
|
10 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $name |
53
|
|
|
* @param array $arguments |
54
|
|
|
* |
55
|
|
|
* @return \drupol\htmltag\Tag\TagInterface |
56
|
|
|
*/ |
57
|
1 |
|
public static function __callStatic($name, array $arguments = []) |
58
|
|
|
{ |
59
|
1 |
|
return new static($arguments[0], $name); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
1 |
|
public function __toString() |
66
|
|
|
{ |
67
|
1 |
|
return $this->render(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
3 |
|
public function attr($name = null, ...$value) |
74
|
|
|
{ |
75
|
3 |
|
if (null === $name) { |
76
|
1 |
|
return $this->attributes->render(); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
if ([] === $value) { |
80
|
2 |
|
return $this->attributes[$name]; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $this->attributes[$name]->set($value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
10 |
|
public function content(...$content) |
90
|
|
|
{ |
91
|
10 |
|
if ([] !== $content) { |
92
|
10 |
|
if (\reset($content) === false) { |
93
|
9 |
|
$content = null; |
94
|
|
|
} |
95
|
|
|
|
96
|
10 |
|
$this->content = $content; |
97
|
|
|
} |
98
|
|
|
|
99
|
10 |
|
return $this->renderContent(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
8 |
|
public function render() |
106
|
|
|
{ |
107
|
8 |
|
return null === $this->content ? |
108
|
5 |
|
sprintf('<%s%s/>', $this->tag, $this->attributes->render()): |
109
|
8 |
|
sprintf('<%s%s>%s</%s>', $this->tag, $this->attributes->render(), $this->renderContent(), $this->tag); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Render the tag content. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
10 |
|
private function renderContent() |
118
|
|
|
{ |
119
|
10 |
|
return \implode( |
120
|
10 |
|
'', |
121
|
10 |
|
$this->preprocess( |
122
|
10 |
|
\array_map( |
123
|
|
|
function ($content_item) { |
124
|
10 |
|
if (is_object($content_item)) { |
125
|
1 |
|
if (!$content_item instanceof TagInterface && |
126
|
1 |
|
\method_exists($content_item, '__toString')) { |
127
|
1 |
|
$content_item = (string) $content_item; |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
10 |
|
$content_item = $this->escape($content_item); |
131
|
|
|
} |
132
|
|
|
|
133
|
10 |
|
return $content_item; |
134
|
10 |
|
}, |
135
|
10 |
|
$this->ensureFlatArray($this->ensureArray($this->content)) |
136
|
|
|
) |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function serialize() |
145
|
|
|
{ |
146
|
1 |
|
return \serialize([ |
147
|
1 |
|
'tag' => $this->tag, |
148
|
1 |
|
'attributes' => $this->attributes->getValuesAsArray(), |
149
|
1 |
|
'content' => $this->renderContent(), |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
1 |
|
public function unserialize($serialized) |
157
|
|
|
{ |
158
|
1 |
|
$unserialize = \unserialize($serialized); |
159
|
|
|
|
160
|
1 |
|
$this->tag = $unserialize['tag']; |
161
|
1 |
|
$this->attributes = $this->attributes->import($unserialize['attributes']); |
162
|
1 |
|
$this->content = $unserialize['content']; |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Alter the values of an object. |
167
|
|
|
* |
168
|
|
|
* @param callable ...$closures |
169
|
|
|
* A closure. |
170
|
|
|
* |
171
|
|
|
* @return object |
172
|
|
|
* The object. |
173
|
|
|
*/ |
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
1 |
|
public function alter(callable ...$closures) |
178
|
|
|
{ |
179
|
1 |
|
foreach ($closures as $closure) { |
180
|
1 |
|
$this->content = $closure( |
181
|
1 |
|
$this->ensureFlatArray($this->ensureArray($this->content)) |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
10 |
|
protected function escape($string) |
192
|
|
|
{ |
193
|
10 |
|
return \htmlspecialchars( |
194
|
10 |
|
$string, |
195
|
10 |
|
ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_HTML5, |
196
|
10 |
|
'UTF-8' |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|