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