1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\htmltag\Attributes; |
4
|
|
|
|
5
|
|
|
use drupol\htmltag\AbstractBaseHtmlTagObject; |
6
|
|
|
use drupol\htmltag\Attribute\AttributeFactoryInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Attributes. |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractAttributes extends AbstractBaseHtmlTagObject implements AttributesInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The attribute factory. |
15
|
|
|
* |
16
|
|
|
* @var \drupol\htmltag\Attribute\AttributeFactoryInterface |
17
|
|
|
*/ |
18
|
|
|
private $attributeFactory; |
19
|
|
|
/** |
20
|
|
|
* Stores the attribute data. |
21
|
|
|
* |
22
|
|
|
* @var \drupol\htmltag\Attribute\AttributeInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $storage = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Attributes constructor. |
28
|
|
|
* |
29
|
|
|
* @param \drupol\htmltag\Attribute\AttributeFactoryInterface $attributeFactory |
30
|
|
|
* The attribute factory |
31
|
|
|
* @param mixed[] $data |
32
|
|
|
* The input attributes |
33
|
|
|
*/ |
34
|
41 |
|
public function __construct(AttributeFactoryInterface $attributeFactory, array $data = []) |
35
|
|
|
{ |
36
|
41 |
|
$this->attributeFactory = $attributeFactory; |
37
|
41 |
|
$this->import($data); |
38
|
41 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function __toString() |
44
|
|
|
{ |
45
|
1 |
|
return $this->render(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
16 |
|
public function append($key, ...$values) |
52
|
|
|
{ |
53
|
|
|
$this->storage += [ |
54
|
16 |
|
$key => $this->attributeFactory->getInstance($key), |
55
|
|
|
]; |
56
|
|
|
|
57
|
16 |
|
$this->storage[$key]->append($values); |
58
|
|
|
|
59
|
16 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
3 |
|
public function contains($key, ...$values) |
66
|
|
|
{ |
67
|
3 |
|
return $this->exists($key) && $this->storage[$key]->contains($values); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function count() |
74
|
|
|
{ |
75
|
1 |
|
return $this->getStorage()->count(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
2 |
|
public function delete(...$keys) |
82
|
|
|
{ |
83
|
2 |
|
foreach ($this->ensureStrings($this->ensureFlatArray($keys)) as $key) { |
84
|
2 |
|
unset($this->storage[$key]); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
3 |
|
public function exists($key, ...$values) |
94
|
|
|
{ |
95
|
3 |
|
if (!isset($this->storage[$key])) { |
96
|
3 |
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
return [] === $values ? |
100
|
3 |
|
true : |
101
|
3 |
|
$this->contains($key, $values); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
1 |
|
public function getIterator() |
108
|
|
|
{ |
109
|
1 |
|
return $this->getStorage(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
30 |
|
public function getStorage() |
116
|
|
|
{ |
117
|
30 |
|
return new \ArrayIterator(\array_values($this->preprocess($this->storage))); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
3 |
|
public function getValuesAsArray() |
124
|
|
|
{ |
125
|
3 |
|
$values = []; |
126
|
|
|
|
127
|
3 |
|
foreach ($this->getStorage() as $attribute) { |
128
|
3 |
|
$values[$attribute->getName()] = $attribute->getValuesAsArray(); |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
return $values; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
41 |
|
public function import($data) |
138
|
|
|
{ |
139
|
41 |
|
foreach ($data as $key => $value) { |
140
|
8 |
|
$this->storage[$key] = $this->attributeFactory->getInstance($key, $value); |
141
|
|
|
} |
142
|
|
|
|
143
|
41 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
1 |
|
public function merge(array ...$dataset) |
150
|
|
|
{ |
151
|
1 |
|
foreach ($dataset as $data) { |
152
|
1 |
|
foreach ($data as $key => $value) { |
153
|
1 |
|
$this->append($key, $value); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
2 |
|
public function offsetExists($key) |
164
|
|
|
{ |
165
|
2 |
|
return isset($this->storage[$key]); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
4 |
|
public function offsetGet($key) |
172
|
|
|
{ |
173
|
|
|
$this->storage += [ |
174
|
4 |
|
$key => $this->attributeFactory->getInstance($key), |
175
|
|
|
]; |
176
|
|
|
|
177
|
4 |
|
return $this->storage[$key]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
1 |
|
public function offsetSet($key, $value = null) |
184
|
|
|
{ |
185
|
1 |
|
$this->storage[$key] = $this->attributeFactory->getInstance($key, $value); |
186
|
1 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
1 |
|
public function offsetUnset($key) |
192
|
|
|
{ |
193
|
1 |
|
unset($this->storage[$key]); |
194
|
1 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
30 |
|
public function preprocess(array $values, array $context = []) |
200
|
|
|
{ |
201
|
30 |
|
return $values; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
2 |
|
public function remove($key, ...$values) |
208
|
|
|
{ |
209
|
2 |
|
if (isset($this->storage[$key])) { |
210
|
1 |
|
$this->storage[$key]->remove($values); |
211
|
|
|
} |
212
|
|
|
|
213
|
2 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritdoc} |
218
|
|
|
*/ |
219
|
24 |
|
public function render() |
220
|
|
|
{ |
221
|
24 |
|
$output = ''; |
222
|
|
|
|
223
|
24 |
|
foreach ($this->getStorage() as $attribute) { |
224
|
19 |
|
$output .= ' ' . $attribute; |
225
|
|
|
} |
226
|
|
|
|
227
|
24 |
|
return $output; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
*/ |
233
|
1 |
|
public function replace($key, $value, ...$replacements) |
234
|
|
|
{ |
235
|
1 |
|
if (!$this->contains($key, $value)) { |
236
|
1 |
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
$this->storage[$key]->replace($value, $replacements); |
240
|
|
|
|
241
|
1 |
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* {@inheritdoc} |
246
|
|
|
*/ |
247
|
1 |
|
public function serialize() |
248
|
|
|
{ |
249
|
1 |
|
return \serialize([ |
250
|
1 |
|
'storage' => $this->getValuesAsArray(), |
251
|
|
|
]); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* {@inheritdoc} |
256
|
|
|
*/ |
257
|
2 |
|
public function set($key, ...$value) |
258
|
|
|
{ |
259
|
2 |
|
$this->storage[$key] = $this->attributeFactory->getInstance($key, $value); |
260
|
|
|
|
261
|
2 |
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritdoc} |
266
|
|
|
*/ |
267
|
1 |
|
public function unserialize($serialized) |
268
|
|
|
{ |
269
|
1 |
|
$unserialize = \unserialize($serialized); |
270
|
1 |
|
$attributeFactory = $this->attributeFactory; |
271
|
|
|
|
272
|
1 |
|
$this->storage = \array_map( |
273
|
|
|
function ($key, $values) use ($attributeFactory) { |
274
|
1 |
|
return $attributeFactory::build($key, $values); |
275
|
1 |
|
}, |
276
|
1 |
|
\array_keys($unserialize['storage']), |
277
|
1 |
|
\array_values($unserialize['storage']) |
278
|
|
|
); |
279
|
1 |
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* {@inheritdoc} |
283
|
|
|
*/ |
284
|
1 |
|
public function without(...$keys) |
285
|
|
|
{ |
286
|
1 |
|
$attributes = clone $this; |
287
|
|
|
|
288
|
1 |
|
return $attributes->delete($keys); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|