/
ArrayFacade.php
229 lines (207 loc) · 5.95 KB
/
ArrayFacade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php declare(strict_types=1);
namespace Shopware\Core\Framework\Script\Facade;
use Shopware\Core\Framework\Log\Package;
/**
* The ArrayFacade acts as a wrapper around an array and allows easier manipulation of arrays inside scripts.
* An array facade can also be accessed like a "normal" array inside twig.
* Examples:
* {% raw %}
* ```twig
* {% do array.push('test') %}
*
* {% do array.foo = 'bar' }
*
* {% do array.has('foo') }
*
* {% if array.foo === 'bar' %}
*
* {% foreach array as key => value %}
* ```
* {% endraw %}
*
* @script-service miscellaneous
*
* @implements \ArrayAccess<array-key, string|int|float|array|object|bool|null>
* @implements \IteratorAggregate<array-key, string|int|float|array|object|bool|null>
*/
#[Package('core')]
class ArrayFacade implements \IteratorAggregate, \ArrayAccess, \Countable
{
private readonly ?\Closure $closure;
/**
* @param array<string|int, mixed> $items
*/
public function __construct(
private array $items,
?\Closure $closure = null
) {
$this->closure = $closure;
}
/**
* `set()` adds a new element to the array using the given key.
*
* @param string|int $key The array key.
* @param mixed $value The value that should be added.
*
* @example payload-cases/payload-cases.twig 5 3 Add a new element with key `test` and value 1.
*/
public function set(string|int $key, $value): void
{
$this->items[$key] = $value;
$this->update();
}
/**
* `push()` adds a new value to the end of the array.
*
* @param mixed $value The value that should be added.
*/
public function push($value): void
{
$this->items[] = $value;
$this->update();
}
/**
* `removeBy()` removes the value at the given index from the array.
*
* @param string|int $index The index that should be removed.
*/
public function removeBy(string|int $index): void
{
unset($this->items[$index]);
$this->update();
}
/**
* `remove()` removes the given value from the array. It does nothing if the provided value does not exist in the array.
*
* @param mixed $value The value that should be removed.
*/
public function remove($value): void
{
$index = \array_search($value, $this->items, true);
if ($index !== false) {
$this->removeBy($index);
$this->update();
}
}
/**
* `reset()` removes all entries from the array.
*/
public function reset(): void
{
foreach (\array_keys($this->items) as $key) {
unset($this->items[$key]);
}
$this->update();
}
/**
* `merge()` recursively merges the array with the given array.
*
* @param array<string|int, mixed>|ArrayFacade $array The array that should be merged with this array. Either a plain `array` or another `ArrayFacade`.
*
* @example payload-cases/payload-cases.twig 13 3 Merge two arrays.
*/
public function merge(array|ArrayFacade $array): void
{
if ($array instanceof ArrayFacade) {
$array = $array->items;
}
$this->items = \array_merge_recursive($this->items, $array);
$this->update();
}
/**
* `replace()` recursively replaces elements from the given array into this array.
*
* @param array<string|int, mixed>|ArrayFacade $array The array from which the elements should be replaced into this array. Either a plain `array` or another `ArrayFacade`.
*
* @example payload-cases/payload-cases.twig 17 3 Replace elements in the product payload array.
*/
public function replace(array|ArrayFacade $array): void
{
if ($array instanceof ArrayFacade) {
$array = $array->items;
}
$this->items = \array_replace_recursive($this->items, $array);
$this->update();
}
/**
* @internal should not be used directly, use the default twig array syntax instead
*
* @param string|int $offset
*
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)/* :bool */
{
return \array_key_exists($offset, $this->items);
}
/**
* @internal should not be used directly, use the default twig array syntax instead
*
* @param string|int $offset
*
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)/* :mixed */
{
return $this->items[$offset];
}
/**
* @internal should not be used directly, use the default twig array syntax instead
*
* @param string|int $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
{
if ($offset === null) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
$this->update();
}
/**
* @internal should not be used directly, use the default twig array syntax instead
*
* @param string|int $offset
*/
public function offsetUnset($offset): void
{
$this->removeBy($offset);
}
/**
* @internal should not be used directly, loop over an array facade directly inside twig instead
*/
public function getIterator(): \Generator
{
yield from $this->items;
}
/**
* `count()` returns the count of elements inside this array.
*
* @return int Returns the count of elements.
*/
public function count(): int
{
return \count($this->items);
}
/**
* `all()` function returns all elements of this array.
*
* @return array<string|int, mixed> Returns all elements of this array.
*/
public function all(): array
{
return $this->items;
}
private function update(): void
{
if (!$this->closure) {
return;
}
$closure = $this->closure;
$closure($this->items);
}
}