1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\htmltag\Attribute; |
4
|
|
|
|
5
|
|
|
use drupol\htmltag\AbstractBaseHtmlTagObject; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Attribute. |
9
|
|
|
*/ |
10
|
|
|
class Attribute extends AbstractBaseHtmlTagObject implements AttributeInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Store the attribute name. |
14
|
|
|
* |
15
|
|
|
* @var string|null |
16
|
|
|
*/ |
17
|
|
|
private $name; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Store the attribute value. |
21
|
|
|
* |
22
|
|
|
* @var array|null |
23
|
|
|
*/ |
24
|
|
|
private $values; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Attribute constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $name |
30
|
|
|
* The attribute name. |
31
|
|
|
* @param string|string[]|mixed[] ...$values |
32
|
|
|
* The attribute values. |
33
|
|
|
*/ |
34
|
42 |
|
public function __construct($name, ...$values) |
35
|
|
|
{ |
36
|
42 |
|
$this->name = \trim($this->sanitize($name)); |
37
|
42 |
|
$this->values = array_filter($values); |
38
|
42 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function offsetExists($offset) |
44
|
|
|
{ |
45
|
1 |
|
return $this->contains($offset); |
46
|
|
|
} |
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
1 |
|
public function offsetGet($offset) |
51
|
|
|
{ |
52
|
1 |
|
throw new \BadMethodCallException('Unsupported method.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
1 |
|
public function offsetSet($offset, $value) |
59
|
|
|
{ |
60
|
1 |
|
$this->append($value); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
1 |
|
public function offsetUnset($offset) |
67
|
|
|
{ |
68
|
1 |
|
$this->remove($offset); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
8 |
|
public function set(...$value) |
75
|
|
|
{ |
76
|
8 |
|
$this->values = $value; |
77
|
|
|
|
78
|
8 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
34 |
|
public function getName() |
85
|
|
|
{ |
86
|
34 |
|
return (string) $this->name; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
34 |
|
public function getValuesAsArray() |
93
|
|
|
{ |
94
|
34 |
|
return array_values( |
95
|
34 |
|
$this->preprocess( |
96
|
34 |
|
$this->normalizeValue($this->values), |
97
|
34 |
|
$this->getName() |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
1 |
|
public function getValuesAsString() |
106
|
|
|
{ |
107
|
1 |
|
return $this->sanitize( |
108
|
1 |
|
\implode( |
109
|
1 |
|
' ', |
110
|
1 |
|
array_filter( |
111
|
1 |
|
$this->getValuesAsArray() |
112
|
|
|
) |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
29 |
|
public function render() |
121
|
|
|
{ |
122
|
29 |
|
$values = $this->getValuesAsArray(); |
123
|
|
|
|
124
|
29 |
|
return empty($values) ? |
125
|
13 |
|
$this->getName() : |
126
|
29 |
|
$this->getName() . '="' . $this->sanitize(\implode(' ', array_filter($values))) . '"'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sanitize a value. |
131
|
|
|
* |
132
|
|
|
* @param string|null $value |
133
|
|
|
* The value to sanitize |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
* The value sanitized. |
137
|
|
|
*/ |
138
|
42 |
|
protected function sanitize($value) |
139
|
|
|
{ |
140
|
42 |
|
return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
1 |
|
public function __toString() |
147
|
|
|
{ |
148
|
1 |
|
return $this->render(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
1 |
|
public function isBoolean() |
155
|
|
|
{ |
156
|
1 |
|
return [] === $this->getValuesAsArray(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
29 |
|
public function append(...$value) |
163
|
|
|
{ |
164
|
29 |
|
$this->values[] = $value; |
165
|
|
|
|
166
|
29 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
5 |
|
public function remove(...$value) |
173
|
|
|
{ |
174
|
5 |
|
return $this->set( |
175
|
5 |
|
\array_diff( |
176
|
5 |
|
$this->normalizeValue($this->values), |
177
|
5 |
|
$this->normalizeValue($value) |
178
|
|
|
) |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
2 |
|
public function replace($original, ...$replacement) |
186
|
|
|
{ |
187
|
2 |
|
$count_start = \count($this->normalizeValue($this->values)); |
188
|
2 |
|
$this->remove($original); |
189
|
2 |
|
$count_end = \count($this->normalizeValue($this->values)); |
190
|
|
|
|
191
|
2 |
|
if ($count_end !== $count_start) { |
192
|
2 |
|
$this->append($replacement); |
193
|
|
|
} |
194
|
|
|
|
195
|
2 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
5 |
|
public function contains(...$substring) |
202
|
|
|
{ |
203
|
5 |
|
$values = $this->normalizeValue($this->values); |
204
|
|
|
|
205
|
5 |
|
return !in_array( |
206
|
5 |
|
false, |
207
|
5 |
|
\array_map( |
208
|
|
|
function ($substring_item) use ($values) { |
209
|
5 |
|
return \in_array($substring_item, $values, true); |
210
|
5 |
|
}, |
211
|
5 |
|
$this->normalizeValue($substring) |
212
|
|
|
), |
213
|
5 |
|
true |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
2 |
|
public function setBoolean($boolean = true) |
221
|
|
|
{ |
222
|
2 |
|
return true === $boolean ? |
223
|
2 |
|
$this->set(null) : |
224
|
2 |
|
$this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
1 |
|
public function delete() |
231
|
|
|
{ |
232
|
1 |
|
$this->name = null; |
233
|
1 |
|
$this->values = null; |
234
|
|
|
|
235
|
1 |
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritdoc} |
240
|
|
|
*/ |
241
|
1 |
|
public function alter(callable ...$closures) |
242
|
|
|
{ |
243
|
1 |
|
$name = $this->getName(); |
244
|
|
|
|
245
|
1 |
|
foreach ($closures as $closure) { |
246
|
1 |
|
$this->values = $closure( |
247
|
1 |
|
$this->normalizeValue($this->values), |
248
|
1 |
|
$name |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
1 |
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* {@inheritdoc} |
257
|
|
|
*/ |
258
|
1 |
|
public function serialize() |
259
|
|
|
{ |
260
|
1 |
|
return \serialize([ |
261
|
1 |
|
'name' => $this->getName(), |
262
|
1 |
|
'values' => $this->getValuesAsArray(), |
263
|
|
|
]); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
1 |
|
public function unserialize($serialized) |
270
|
|
|
{ |
271
|
1 |
|
$unserialized = \unserialize($serialized); |
272
|
|
|
|
273
|
1 |
|
$this->name = $unserialized['name']; |
274
|
1 |
|
$this->values = $unserialized['values']; |
275
|
1 |
|
} |
276
|
|
|
} |
277
|
|
|
|