@vituum/vite-plugin-latte 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/vendor/composer/autoload_classmap.php +49 -0
- package/vendor/composer/autoload_static.php +49 -0
- package/vendor/composer/installed.json +89 -0
- package/vendor/composer/installed.php +11 -2
- package/vendor/nette/utils/.phpstorm.meta.php +13 -0
- package/vendor/nette/utils/composer.json +51 -0
- package/vendor/nette/utils/license.md +60 -0
- package/vendor/nette/utils/readme.md +56 -0
- package/vendor/nette/utils/src/HtmlStringable.php +22 -0
- package/vendor/nette/utils/src/Iterators/CachingIterator.php +164 -0
- package/vendor/nette/utils/src/Iterators/Mapper.php +34 -0
- package/vendor/nette/utils/src/SmartObject.php +140 -0
- package/vendor/nette/utils/src/StaticClass.php +34 -0
- package/vendor/nette/utils/src/Translator.php +25 -0
- package/vendor/nette/utils/src/Utils/ArrayHash.php +106 -0
- package/vendor/nette/utils/src/Utils/ArrayList.php +136 -0
- package/vendor/nette/utils/src/Utils/Arrays.php +522 -0
- package/vendor/nette/utils/src/Utils/Callback.php +137 -0
- package/vendor/nette/utils/src/Utils/DateTime.php +140 -0
- package/vendor/nette/utils/src/Utils/FileInfo.php +69 -0
- package/vendor/nette/utils/src/Utils/FileSystem.php +326 -0
- package/vendor/nette/utils/src/Utils/Finder.php +510 -0
- package/vendor/nette/utils/src/Utils/Floats.php +107 -0
- package/vendor/nette/utils/src/Utils/Helpers.php +104 -0
- package/vendor/nette/utils/src/Utils/Html.php +839 -0
- package/vendor/nette/utils/src/Utils/Image.php +829 -0
- package/vendor/nette/utils/src/Utils/ImageColor.php +75 -0
- package/vendor/nette/utils/src/Utils/ImageType.php +25 -0
- package/vendor/nette/utils/src/Utils/Iterables.php +159 -0
- package/vendor/nette/utils/src/Utils/Json.php +84 -0
- package/vendor/nette/utils/src/Utils/ObjectHelpers.php +229 -0
- package/vendor/nette/utils/src/Utils/Paginator.php +245 -0
- package/vendor/nette/utils/src/Utils/Random.php +52 -0
- package/vendor/nette/utils/src/Utils/Reflection.php +320 -0
- package/vendor/nette/utils/src/Utils/ReflectionMethod.php +36 -0
- package/vendor/nette/utils/src/Utils/Strings.php +708 -0
- package/vendor/nette/utils/src/Utils/Type.php +267 -0
- package/vendor/nette/utils/src/Utils/Validators.php +416 -0
- package/vendor/nette/utils/src/Utils/exceptions.php +50 -0
- package/vendor/nette/utils/src/compatibility.php +32 -0
- package/vendor/nette/utils/src/exceptions.php +109 -0
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This file is part of the Nette Framework (https://nette.org)
|
|
5
|
+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare(strict_types=1);
|
|
9
|
+
|
|
10
|
+
namespace Nette\Utils;
|
|
11
|
+
|
|
12
|
+
use JetBrains\PhpStorm\Language;
|
|
13
|
+
use Nette;
|
|
14
|
+
use function is_array, is_int, is_object, count;
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Array tools library.
|
|
19
|
+
*/
|
|
20
|
+
class Arrays
|
|
21
|
+
{
|
|
22
|
+
use Nette\StaticClass;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
|
|
26
|
+
* @template T
|
|
27
|
+
* @param array<T> $array
|
|
28
|
+
* @param array-key|array-key[] $key
|
|
29
|
+
* @param ?T $default
|
|
30
|
+
* @return ?T
|
|
31
|
+
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
|
|
32
|
+
*/
|
|
33
|
+
public static function get(array $array, string|int|array $key, mixed $default = null): mixed
|
|
34
|
+
{
|
|
35
|
+
foreach (is_array($key) ? $key : [$key] as $k) {
|
|
36
|
+
if (is_array($array) && array_key_exists($k, $array)) {
|
|
37
|
+
$array = $array[$k];
|
|
38
|
+
} else {
|
|
39
|
+
if (func_num_args() < 3) {
|
|
40
|
+
throw new Nette\InvalidArgumentException("Missing item '$k'.");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return $default;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return $array;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns reference to array item. If the index does not exist, new one is created with value null.
|
|
53
|
+
* @template T
|
|
54
|
+
* @param array<T> $array
|
|
55
|
+
* @param array-key|array-key[] $key
|
|
56
|
+
* @return ?T
|
|
57
|
+
* @throws Nette\InvalidArgumentException if traversed item is not an array
|
|
58
|
+
*/
|
|
59
|
+
public static function &getRef(array &$array, string|int|array $key): mixed
|
|
60
|
+
{
|
|
61
|
+
foreach (is_array($key) ? $key : [$key] as $k) {
|
|
62
|
+
if (is_array($array) || $array === null) {
|
|
63
|
+
$array = &$array[$k];
|
|
64
|
+
} else {
|
|
65
|
+
throw new Nette\InvalidArgumentException('Traversed item is not an array.');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return $array;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Recursively merges two fields. It is useful, for example, for merging tree structures. It behaves as
|
|
75
|
+
* the + operator for array, ie. it adds a key/value pair from the second array to the first one and retains
|
|
76
|
+
* the value from the first array in the case of a key collision.
|
|
77
|
+
* @template T1
|
|
78
|
+
* @template T2
|
|
79
|
+
* @param array<T1> $array1
|
|
80
|
+
* @param array<T2> $array2
|
|
81
|
+
* @return array<T1|T2>
|
|
82
|
+
*/
|
|
83
|
+
public static function mergeTree(array $array1, array $array2): array
|
|
84
|
+
{
|
|
85
|
+
$res = $array1 + $array2;
|
|
86
|
+
foreach (array_intersect_key($array1, $array2) as $k => $v) {
|
|
87
|
+
if (is_array($v) && is_array($array2[$k])) {
|
|
88
|
+
$res[$k] = self::mergeTree($v, $array2[$k]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return $res;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Returns zero-indexed position of given array key. Returns null if key is not found.
|
|
98
|
+
*/
|
|
99
|
+
public static function getKeyOffset(array $array, string|int $key): ?int
|
|
100
|
+
{
|
|
101
|
+
return Helpers::falseToNull(array_search(self::toKey($key), array_keys($array), strict: true));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @deprecated use getKeyOffset()
|
|
107
|
+
*/
|
|
108
|
+
public static function searchKey(array $array, $key): ?int
|
|
109
|
+
{
|
|
110
|
+
return self::getKeyOffset($array, $key);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Tests an array for the presence of value.
|
|
116
|
+
*/
|
|
117
|
+
public static function contains(array $array, mixed $value): bool
|
|
118
|
+
{
|
|
119
|
+
return in_array($value, $array, true);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
|
|
125
|
+
* The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
|
|
126
|
+
* @template T
|
|
127
|
+
* @param array<T> $array
|
|
128
|
+
* @return ?T
|
|
129
|
+
*/
|
|
130
|
+
public static function first(array $array, ?callable $predicate = null, ?callable $else = null): mixed
|
|
131
|
+
{
|
|
132
|
+
$key = self::firstKey($array, $predicate);
|
|
133
|
+
return $key === null
|
|
134
|
+
? ($else ? $else() : null)
|
|
135
|
+
: $array[$key];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Returns the last item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
|
|
141
|
+
* The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
|
|
142
|
+
* @template T
|
|
143
|
+
* @param array<T> $array
|
|
144
|
+
* @return ?T
|
|
145
|
+
*/
|
|
146
|
+
public static function last(array $array, ?callable $predicate = null, ?callable $else = null): mixed
|
|
147
|
+
{
|
|
148
|
+
$key = self::lastKey($array, $predicate);
|
|
149
|
+
return $key === null
|
|
150
|
+
? ($else ? $else() : null)
|
|
151
|
+
: $array[$key];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
|
|
157
|
+
* The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
|
|
158
|
+
*/
|
|
159
|
+
public static function firstKey(array $array, ?callable $predicate = null): int|string|null
|
|
160
|
+
{
|
|
161
|
+
if (!$predicate) {
|
|
162
|
+
return array_key_first($array);
|
|
163
|
+
}
|
|
164
|
+
foreach ($array as $k => $v) {
|
|
165
|
+
if ($predicate($v, $k, $array)) {
|
|
166
|
+
return $k;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Returns the key of last item (matching the specified predicate if given) or null if there is no such item.
|
|
175
|
+
* The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
|
|
176
|
+
*/
|
|
177
|
+
public static function lastKey(array $array, ?callable $predicate = null): int|string|null
|
|
178
|
+
{
|
|
179
|
+
return $predicate
|
|
180
|
+
? self::firstKey(array_reverse($array, preserve_keys: true), $predicate)
|
|
181
|
+
: array_key_last($array);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Inserts the contents of the $inserted array into the $array immediately after the $key.
|
|
187
|
+
* If $key is null (or does not exist), it is inserted at the beginning.
|
|
188
|
+
*/
|
|
189
|
+
public static function insertBefore(array &$array, string|int|null $key, array $inserted): void
|
|
190
|
+
{
|
|
191
|
+
$offset = $key === null ? 0 : (int) self::getKeyOffset($array, $key);
|
|
192
|
+
$array = array_slice($array, 0, $offset, preserve_keys: true)
|
|
193
|
+
+ $inserted
|
|
194
|
+
+ array_slice($array, $offset, count($array), preserve_keys: true);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Inserts the contents of the $inserted array into the $array before the $key.
|
|
200
|
+
* If $key is null (or does not exist), it is inserted at the end.
|
|
201
|
+
*/
|
|
202
|
+
public static function insertAfter(array &$array, string|int|null $key, array $inserted): void
|
|
203
|
+
{
|
|
204
|
+
if ($key === null || ($offset = self::getKeyOffset($array, $key)) === null) {
|
|
205
|
+
$offset = count($array) - 1;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
$array = array_slice($array, 0, $offset + 1, preserve_keys: true)
|
|
209
|
+
+ $inserted
|
|
210
|
+
+ array_slice($array, $offset + 1, count($array), preserve_keys: true);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Renames key in array.
|
|
216
|
+
*/
|
|
217
|
+
public static function renameKey(array &$array, string|int $oldKey, string|int $newKey): bool
|
|
218
|
+
{
|
|
219
|
+
$offset = self::getKeyOffset($array, $oldKey);
|
|
220
|
+
if ($offset === null) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
$val = &$array[$oldKey];
|
|
225
|
+
$keys = array_keys($array);
|
|
226
|
+
$keys[$offset] = $newKey;
|
|
227
|
+
$array = array_combine($keys, $array);
|
|
228
|
+
$array[$newKey] = &$val;
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Returns only those array items, which matches a regular expression $pattern.
|
|
235
|
+
* @param string[] $array
|
|
236
|
+
* @return string[]
|
|
237
|
+
*/
|
|
238
|
+
public static function grep(
|
|
239
|
+
array $array,
|
|
240
|
+
#[Language('RegExp')]
|
|
241
|
+
string $pattern,
|
|
242
|
+
bool|int $invert = false,
|
|
243
|
+
): array
|
|
244
|
+
{
|
|
245
|
+
$flags = $invert ? PREG_GREP_INVERT : 0;
|
|
246
|
+
return Strings::pcre('preg_grep', [$pattern, $array, $flags]);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Transforms multidimensional array to flat array.
|
|
252
|
+
*/
|
|
253
|
+
public static function flatten(array $array, bool $preserveKeys = false): array
|
|
254
|
+
{
|
|
255
|
+
$res = [];
|
|
256
|
+
$cb = $preserveKeys
|
|
257
|
+
? function ($v, $k) use (&$res): void { $res[$k] = $v; }
|
|
258
|
+
: function ($v) use (&$res): void { $res[] = $v; };
|
|
259
|
+
array_walk_recursive($array, $cb);
|
|
260
|
+
return $res;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
|
|
266
|
+
* @return ($value is list ? true : false)
|
|
267
|
+
*/
|
|
268
|
+
public static function isList(mixed $value): bool
|
|
269
|
+
{
|
|
270
|
+
return is_array($value) && (PHP_VERSION_ID < 80100
|
|
271
|
+
? !$value || array_keys($value) === range(0, count($value) - 1)
|
|
272
|
+
: array_is_list($value)
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
|
|
279
|
+
* @param string|string[] $path
|
|
280
|
+
*/
|
|
281
|
+
public static function associate(array $array, $path): array|\stdClass
|
|
282
|
+
{
|
|
283
|
+
$parts = is_array($path)
|
|
284
|
+
? $path
|
|
285
|
+
: preg_split('#(\[\]|->|=|\|)#', $path, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
|
286
|
+
|
|
287
|
+
if (!$parts || $parts === ['->'] || $parts[0] === '=' || $parts[0] === '|') {
|
|
288
|
+
throw new Nette\InvalidArgumentException("Invalid path '$path'.");
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
$res = $parts[0] === '->' ? new \stdClass : [];
|
|
292
|
+
|
|
293
|
+
foreach ($array as $rowOrig) {
|
|
294
|
+
$row = (array) $rowOrig;
|
|
295
|
+
$x = &$res;
|
|
296
|
+
|
|
297
|
+
for ($i = 0; $i < count($parts); $i++) {
|
|
298
|
+
$part = $parts[$i];
|
|
299
|
+
if ($part === '[]') {
|
|
300
|
+
$x = &$x[];
|
|
301
|
+
|
|
302
|
+
} elseif ($part === '=') {
|
|
303
|
+
if (isset($parts[++$i])) {
|
|
304
|
+
$x = $row[$parts[$i]];
|
|
305
|
+
$row = null;
|
|
306
|
+
}
|
|
307
|
+
} elseif ($part === '->') {
|
|
308
|
+
if (isset($parts[++$i])) {
|
|
309
|
+
if ($x === null) {
|
|
310
|
+
$x = new \stdClass;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
$x = &$x->{$row[$parts[$i]]};
|
|
314
|
+
} else {
|
|
315
|
+
$row = is_object($rowOrig) ? $rowOrig : (object) $row;
|
|
316
|
+
}
|
|
317
|
+
} elseif ($part !== '|') {
|
|
318
|
+
$x = &$x[(string) $row[$part]];
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if ($x === null) {
|
|
323
|
+
$x = $row;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return $res;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
|
|
333
|
+
*/
|
|
334
|
+
public static function normalize(array $array, mixed $filling = null): array
|
|
335
|
+
{
|
|
336
|
+
$res = [];
|
|
337
|
+
foreach ($array as $k => $v) {
|
|
338
|
+
$res[is_int($k) ? $v : $k] = is_int($k) ? $filling : $v;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return $res;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Returns and removes the value of an item from an array. If it does not exist, it throws an exception,
|
|
347
|
+
* or returns $default, if provided.
|
|
348
|
+
* @template T
|
|
349
|
+
* @param array<T> $array
|
|
350
|
+
* @param ?T $default
|
|
351
|
+
* @return ?T
|
|
352
|
+
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
|
|
353
|
+
*/
|
|
354
|
+
public static function pick(array &$array, string|int $key, mixed $default = null): mixed
|
|
355
|
+
{
|
|
356
|
+
if (array_key_exists($key, $array)) {
|
|
357
|
+
$value = $array[$key];
|
|
358
|
+
unset($array[$key]);
|
|
359
|
+
return $value;
|
|
360
|
+
|
|
361
|
+
} elseif (func_num_args() < 3) {
|
|
362
|
+
throw new Nette\InvalidArgumentException("Missing item '$key'.");
|
|
363
|
+
|
|
364
|
+
} else {
|
|
365
|
+
return $default;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Tests whether at least one element in the array passes the test implemented by the provided function,
|
|
372
|
+
* which has the signature `function ($value, $key, array $array): bool`.
|
|
373
|
+
* @template K
|
|
374
|
+
* @template V
|
|
375
|
+
* @param iterable<K, V> $array
|
|
376
|
+
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): bool $predicate
|
|
377
|
+
*/
|
|
378
|
+
public static function some(iterable $array, callable $predicate): bool
|
|
379
|
+
{
|
|
380
|
+
foreach ($array as $k => $v) {
|
|
381
|
+
if ($predicate($v, $k, $array)) {
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Tests whether all elements in the array pass the test implemented by the provided function,
|
|
392
|
+
* which has the signature `function ($value, $key, array $array): bool`.
|
|
393
|
+
* @template K
|
|
394
|
+
* @template V
|
|
395
|
+
* @param iterable<K, V> $array
|
|
396
|
+
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): bool $predicate
|
|
397
|
+
*/
|
|
398
|
+
public static function every(iterable $array, callable $predicate): bool
|
|
399
|
+
{
|
|
400
|
+
foreach ($array as $k => $v) {
|
|
401
|
+
if (!$predicate($v, $k, $array)) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return true;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Returns a new array containing all key-value pairs matching the given $predicate.
|
|
412
|
+
* The callback has the signature `function (mixed $value, int|string $key, array $array): bool`.
|
|
413
|
+
* @template K of array-key
|
|
414
|
+
* @template V
|
|
415
|
+
* @param array<K, V> $array
|
|
416
|
+
* @param callable(V, K, array<K, V>): bool $predicate
|
|
417
|
+
* @return array<K, V>
|
|
418
|
+
*/
|
|
419
|
+
public static function filter(array $array, callable $predicate): array
|
|
420
|
+
{
|
|
421
|
+
$res = [];
|
|
422
|
+
foreach ($array as $k => $v) {
|
|
423
|
+
if ($predicate($v, $k, $array)) {
|
|
424
|
+
$res[$k] = $v;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return $res;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Returns an array containing the original keys and results of applying the given transform function to each element.
|
|
433
|
+
* The function has signature `function ($value, $key, array $array): mixed`.
|
|
434
|
+
* @template K of array-key
|
|
435
|
+
* @template V
|
|
436
|
+
* @template R
|
|
437
|
+
* @param iterable<K, V> $array
|
|
438
|
+
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): R $transformer
|
|
439
|
+
* @return array<K, R>
|
|
440
|
+
*/
|
|
441
|
+
public static function map(iterable $array, callable $transformer): array
|
|
442
|
+
{
|
|
443
|
+
$res = [];
|
|
444
|
+
foreach ($array as $k => $v) {
|
|
445
|
+
$res[$k] = $transformer($v, $k, $array);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return $res;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Invokes all callbacks and returns array of results.
|
|
454
|
+
* @param callable[] $callbacks
|
|
455
|
+
*/
|
|
456
|
+
public static function invoke(iterable $callbacks, ...$args): array
|
|
457
|
+
{
|
|
458
|
+
$res = [];
|
|
459
|
+
foreach ($callbacks as $k => $cb) {
|
|
460
|
+
$res[$k] = $cb(...$args);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return $res;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Invokes method on every object in an array and returns array of results.
|
|
469
|
+
* @param object[] $objects
|
|
470
|
+
*/
|
|
471
|
+
public static function invokeMethod(iterable $objects, string $method, ...$args): array
|
|
472
|
+
{
|
|
473
|
+
$res = [];
|
|
474
|
+
foreach ($objects as $k => $obj) {
|
|
475
|
+
$res[$k] = $obj->$method(...$args);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return $res;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Copies the elements of the $array array to the $object object and then returns it.
|
|
484
|
+
* @template T of object
|
|
485
|
+
* @param T $object
|
|
486
|
+
* @return T
|
|
487
|
+
*/
|
|
488
|
+
public static function toObject(iterable $array, object $object): object
|
|
489
|
+
{
|
|
490
|
+
foreach ($array as $k => $v) {
|
|
491
|
+
$object->$k = $v;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return $object;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Converts value to array key.
|
|
500
|
+
*/
|
|
501
|
+
public static function toKey(mixed $value): int|string
|
|
502
|
+
{
|
|
503
|
+
return key([$value => null]);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Returns copy of the $array where every item is converted to string
|
|
509
|
+
* and prefixed by $prefix and suffixed by $suffix.
|
|
510
|
+
* @param string[] $array
|
|
511
|
+
* @return string[]
|
|
512
|
+
*/
|
|
513
|
+
public static function wrap(array $array, string $prefix = '', string $suffix = ''): array
|
|
514
|
+
{
|
|
515
|
+
$res = [];
|
|
516
|
+
foreach ($array as $k => $v) {
|
|
517
|
+
$res[$k] = $prefix . $v . $suffix;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
return $res;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This file is part of the Nette Framework (https://nette.org)
|
|
5
|
+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare(strict_types=1);
|
|
9
|
+
|
|
10
|
+
namespace Nette\Utils;
|
|
11
|
+
|
|
12
|
+
use Nette;
|
|
13
|
+
use function is_array, is_object, is_string;
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* PHP callable tools.
|
|
18
|
+
*/
|
|
19
|
+
final class Callback
|
|
20
|
+
{
|
|
21
|
+
use Nette\StaticClass;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Invokes internal PHP function with own error handler.
|
|
25
|
+
*/
|
|
26
|
+
public static function invokeSafe(string $function, array $args, callable $onError): mixed
|
|
27
|
+
{
|
|
28
|
+
$prev = set_error_handler(function ($severity, $message, $file) use ($onError, &$prev, $function): ?bool {
|
|
29
|
+
if ($file === __FILE__) {
|
|
30
|
+
$msg = ini_get('html_errors')
|
|
31
|
+
? Html::htmlToText($message)
|
|
32
|
+
: $message;
|
|
33
|
+
$msg = preg_replace("#^$function\\(.*?\\): #", '', $msg);
|
|
34
|
+
if ($onError($msg, $severity) !== false) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return $prev ? $prev(...func_get_args()) : false;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
return $function(...$args);
|
|
44
|
+
} finally {
|
|
45
|
+
restore_error_handler();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Checks that $callable is valid PHP callback. Otherwise throws exception. If the $syntax is set to true, only verifies
|
|
52
|
+
* that $callable has a valid structure to be used as a callback, but does not verify if the class or method actually exists.
|
|
53
|
+
* @return callable
|
|
54
|
+
* @throws Nette\InvalidArgumentException
|
|
55
|
+
*/
|
|
56
|
+
public static function check(mixed $callable, bool $syntax = false)
|
|
57
|
+
{
|
|
58
|
+
if (!is_callable($callable, $syntax)) {
|
|
59
|
+
throw new Nette\InvalidArgumentException(
|
|
60
|
+
$syntax
|
|
61
|
+
? 'Given value is not a callable type.'
|
|
62
|
+
: sprintf("Callback '%s' is not callable.", self::toString($callable)),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return $callable;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Converts PHP callback to textual form. Class or method may not exists.
|
|
72
|
+
*/
|
|
73
|
+
public static function toString(mixed $callable): string
|
|
74
|
+
{
|
|
75
|
+
if ($callable instanceof \Closure) {
|
|
76
|
+
$inner = self::unwrap($callable);
|
|
77
|
+
return '{closure' . ($inner instanceof \Closure ? '}' : ' ' . self::toString($inner) . '}');
|
|
78
|
+
} else {
|
|
79
|
+
is_callable(is_object($callable) ? [$callable, '__invoke'] : $callable, true, $textual);
|
|
80
|
+
return $textual;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Returns reflection for method or function used in PHP callback.
|
|
87
|
+
* @param callable $callable type check is escalated to ReflectionException
|
|
88
|
+
* @throws \ReflectionException if callback is not valid
|
|
89
|
+
*/
|
|
90
|
+
public static function toReflection($callable): \ReflectionMethod|\ReflectionFunction
|
|
91
|
+
{
|
|
92
|
+
if ($callable instanceof \Closure) {
|
|
93
|
+
$callable = self::unwrap($callable);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (is_string($callable) && str_contains($callable, '::')) {
|
|
97
|
+
return new ReflectionMethod($callable);
|
|
98
|
+
} elseif (is_array($callable)) {
|
|
99
|
+
return new ReflectionMethod($callable[0], $callable[1]);
|
|
100
|
+
} elseif (is_object($callable) && !$callable instanceof \Closure) {
|
|
101
|
+
return new ReflectionMethod($callable, '__invoke');
|
|
102
|
+
} else {
|
|
103
|
+
return new \ReflectionFunction($callable);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Checks whether PHP callback is function or static method.
|
|
110
|
+
*/
|
|
111
|
+
public static function isStatic(callable $callable): bool
|
|
112
|
+
{
|
|
113
|
+
return is_string(is_array($callable) ? $callable[0] : $callable);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Unwraps closure created by Closure::fromCallable().
|
|
119
|
+
*/
|
|
120
|
+
public static function unwrap(\Closure $closure): callable|array
|
|
121
|
+
{
|
|
122
|
+
$r = new \ReflectionFunction($closure);
|
|
123
|
+
$class = $r->getClosureScopeClass()?->name;
|
|
124
|
+
if (str_ends_with($r->name, '}')) {
|
|
125
|
+
return $closure;
|
|
126
|
+
|
|
127
|
+
} elseif (($obj = $r->getClosureThis()) && $obj::class === $class) {
|
|
128
|
+
return [$obj, $r->name];
|
|
129
|
+
|
|
130
|
+
} elseif ($class) {
|
|
131
|
+
return [$class, $r->name];
|
|
132
|
+
|
|
133
|
+
} else {
|
|
134
|
+
return $r->name;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|