Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 150 Revision 399
Zeile 28... Zeile 28...
28
	 * @param  array-key|array-key[]  $key
28
	 * @param  array-key|array-key[]  $key
29
	 * @param  ?T  $default
29
	 * @param  ?T  $default
30
	 * @return ?T
30
	 * @return ?T
31
	 * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
31
	 * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
32
	 */
32
	 */
33
	public static function get(array $array, $key, $default = null)
33
	public static function get(array $array, string|int|array $key, mixed $default = null): mixed
34
	{
34
	{
35
		foreach (is_array($key) ? $key : [$key] as $k) {
35
		foreach (is_array($key) ? $key : [$key] as $k) {
36
			if (is_array($array) && array_key_exists($k, $array)) {
36
			if (is_array($array) && array_key_exists($k, $array)) {
37
				$array = $array[$k];
37
				$array = $array[$k];
38
			} else {
38
			} else {
Zeile 54... Zeile 54...
54
	 * @param  array<T>  $array
54
	 * @param  array<T>  $array
55
	 * @param  array-key|array-key[]  $key
55
	 * @param  array-key|array-key[]  $key
56
	 * @return ?T
56
	 * @return ?T
57
	 * @throws Nette\InvalidArgumentException if traversed item is not an array
57
	 * @throws Nette\InvalidArgumentException if traversed item is not an array
58
	 */
58
	 */
59
	public static function &getRef(array &$array, $key)
59
	public static function &getRef(array &$array, string|int|array $key): mixed
60
	{
60
	{
61
		foreach (is_array($key) ? $key : [$key] as $k) {
61
		foreach (is_array($key) ? $key : [$key] as $k) {
62
			if (is_array($array) || $array === null) {
62
			if (is_array($array) || $array === null) {
63
				$array = &$array[$k];
63
				$array = &$array[$k];
64
			} else {
64
			} else {
Zeile 93... Zeile 93...
93
	}
93
	}
Zeile 94... Zeile 94...
94
 
94
 
95
 
95
 
96
	/**
-
 
97
	 * Returns zero-indexed position of given array key. Returns null if key is not found.
-
 
98
	 * @param  array-key  $key
96
	/**
99
	 * @return int|null offset if it is found, null otherwise
97
	 * Returns zero-indexed position of given array key. Returns null if key is not found.
100
	 */
98
	 */
101
	public static function getKeyOffset(array $array, $key): ?int
99
	public static function getKeyOffset(array $array, string|int $key): ?int
102
	{
100
	{
Zeile 113... Zeile 111...
113
	}
111
	}
Zeile 114... Zeile 112...
114
 
112
 
115
 
113
 
116
	/**
-
 
117
	 * Tests an array for the presence of value.
114
	/**
118
	 * @param  mixed  $value
115
	 * Tests an array for the presence of value.
119
	 */
116
	 */
120
	public static function contains(array $array, $value): bool
117
	public static function contains(array $array, mixed $value): bool
121
	{
118
	{
Zeile 127... Zeile 124...
127
	 * Returns the first item from the array or null if array is empty.
124
	 * Returns the first item from the array or null if array is empty.
128
	 * @template T
125
	 * @template T
129
	 * @param  array<T>  $array
126
	 * @param  array<T>  $array
130
	 * @return ?T
127
	 * @return ?T
131
	 */
128
	 */
132
	public static function first(array $array)
129
	public static function first(array $array): mixed
133
	{
130
	{
134
		return count($array) ? reset($array) : null;
131
		return count($array) ? reset($array) : null;
135
	}
132
	}
Zeile 139... Zeile 136...
139
	 * Returns the last item from the array or null if array is empty.
136
	 * Returns the last item from the array or null if array is empty.
140
	 * @template T
137
	 * @template T
141
	 * @param  array<T>  $array
138
	 * @param  array<T>  $array
142
	 * @return ?T
139
	 * @return ?T
143
	 */
140
	 */
144
	public static function last(array $array)
141
	public static function last(array $array): mixed
145
	{
142
	{
146
		return count($array) ? end($array) : null;
143
		return count($array) ? end($array) : null;
147
	}
144
	}
Zeile 148... Zeile 145...
148
 
145
 
149
 
146
 
150
	/**
147
	/**
151
	 * Inserts the contents of the $inserted array into the $array immediately after the $key.
-
 
152
	 * If $key is null (or does not exist), it is inserted at the beginning.
148
	 * Inserts the contents of the $inserted array into the $array immediately after the $key.
153
	 * @param  array-key|null  $key
149
	 * If $key is null (or does not exist), it is inserted at the beginning.
154
	 */
150
	 */
155
	public static function insertBefore(array &$array, $key, array $inserted): void
151
	public static function insertBefore(array &$array, string|int|null $key, array $inserted): void
156
	{
152
	{
157
		$offset = $key === null ? 0 : (int) self::getKeyOffset($array, $key);
153
		$offset = $key === null ? 0 : (int) self::getKeyOffset($array, $key);
158
		$array = array_slice($array, 0, $offset, true)
154
		$array = array_slice($array, 0, $offset, true)
Zeile 162... Zeile 158...
162
 
158
 
163
 
159
 
164
	/**
160
	/**
165
	 * Inserts the contents of the $inserted array into the $array before the $key.
-
 
166
	 * If $key is null (or does not exist), it is inserted at the end.
161
	 * Inserts the contents of the $inserted array into the $array before the $key.
167
	 * @param  array-key|null  $key
162
	 * If $key is null (or does not exist), it is inserted at the end.
168
	 */
163
	 */
169
	public static function insertAfter(array &$array, $key, array $inserted): void
164
	public static function insertAfter(array &$array, string|int|null $key, array $inserted): void
170
	{
165
	{
171
		if ($key === null || ($offset = self::getKeyOffset($array, $key)) === null) {
166
		if ($key === null || ($offset = self::getKeyOffset($array, $key)) === null) {
Zeile 178... Zeile 173...
178
	}
173
	}
Zeile 179... Zeile 174...
179
 
174
 
180
 
175
 
181
	/**
-
 
182
	 * Renames key in array.
-
 
183
	 * @param  array-key  $oldKey
176
	/**
184
	 * @param  array-key  $newKey
177
	 * Renames key in array.
185
	 */
178
	 */
186
	public static function renameKey(array &$array, $oldKey, $newKey): bool
179
	public static function renameKey(array &$array, string|int $oldKey, string|int $newKey): bool
187
	{
180
	{
188
		$offset = self::getKeyOffset($array, $oldKey);
181
		$offset = self::getKeyOffset($array, $oldKey);
189
		if ($offset === null) {
182
		if ($offset === null) {
Zeile 206... Zeile 199...
206
	 */
199
	 */
207
	public static function grep(
200
	public static function grep(
208
		array $array,
201
		array $array,
209
		#[Language('RegExp')]
202
		#[Language('RegExp')]
210
		string $pattern,
203
		string $pattern,
211
		int $flags = 0
204
		bool|int $invert = false,
212
	): array
205
	): array
213
	{
206
	{
-
 
207
		$flags = $invert ? PREG_GREP_INVERT : 0;
214
		return Strings::pcre('preg_grep', [$pattern, $array, $flags]);
208
		return Strings::pcre('preg_grep', [$pattern, $array, $flags]);
215
	}
209
	}
Zeile 216... Zeile 210...
216
 
210
 
Zeile 229... Zeile 223...
229
	}
223
	}
Zeile 230... Zeile 224...
230
 
224
 
231
 
225
 
232
	/**
-
 
233
	 * Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
226
	/**
234
	 * @param  mixed  $value
227
	 * Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
235
	 */
228
	 */
236
	public static function isList($value): bool
229
	public static function isList(mixed $value): bool
237
	{
230
	{
238
		return is_array($value) && (PHP_VERSION_ID < 80100
231
		return is_array($value) && (PHP_VERSION_ID < 80100
239
			? !$value || array_keys($value) === range(0, count($value) - 1)
232
			? !$value || array_keys($value) === range(0, count($value) - 1)
Zeile 243... Zeile 236...
243
 
236
 
244
 
237
 
245
	/**
238
	/**
246
	 * Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
-
 
247
	 * @param  string|string[]  $path
239
	 * Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
248
	 * @return array|\stdClass
240
	 * @param  string|string[]  $path
249
	 */
241
	 */
250
	public static function associate(array $array, $path)
242
	public static function associate(array $array, $path): array|\stdClass
251
	{
243
	{
252
		$parts = is_array($path)
244
		$parts = is_array($path)
Zeile 297... Zeile 289...
297
	}
289
	}
Zeile 298... Zeile 290...
298
 
290
 
299
 
291
 
300
	/**
-
 
301
	 * Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
292
	/**
302
	 * @param  mixed  $filling
293
	 * Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
303
	 */
294
	 */
304
	public static function normalize(array $array, $filling = null): array
295
	public static function normalize(array $array, mixed $filling = null): array
305
	{
296
	{
306
		$res = [];
297
		$res = [];
307
		foreach ($array as $k => $v) {
298
		foreach ($array as $k => $v) {
Zeile 315... Zeile 306...
315
	/**
306
	/**
316
	 * Returns and removes the value of an item from an array. If it does not exist, it throws an exception,
307
	 * Returns and removes the value of an item from an array. If it does not exist, it throws an exception,
317
	 * or returns $default, if provided.
308
	 * or returns $default, if provided.
318
	 * @template T
309
	 * @template T
319
	 * @param  array<T>  $array
310
	 * @param  array<T>  $array
320
	 * @param  array-key  $key
-
 
321
	 * @param  ?T  $default
311
	 * @param  ?T  $default
322
	 * @return ?T
312
	 * @return ?T
323
	 * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
313
	 * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
324
	 */
314
	 */
325
	public static function pick(array &$array, $key, $default = null)
315
	public static function pick(array &$array, string|int $key, mixed $default = null): mixed
326
	{
316
	{
327
		if (array_key_exists($key, $array)) {
317
		if (array_key_exists($key, $array)) {
328
			$value = $array[$key];
318
			$value = $array[$key];
329
			unset($array[$key]);
319
			unset($array[$key]);
330
			return $value;
320
			return $value;
Zeile 419... Zeile 409...
419
	 * Copies the elements of the $array array to the $object object and then returns it.
409
	 * Copies the elements of the $array array to the $object object and then returns it.
420
	 * @template T of object
410
	 * @template T of object
421
	 * @param  T  $object
411
	 * @param  T  $object
422
	 * @return T
412
	 * @return T
423
	 */
413
	 */
424
	public static function toObject(iterable $array, $object)
414
	public static function toObject(iterable $array, object $object): object
425
	{
415
	{
426
		foreach ($array as $k => $v) {
416
		foreach ($array as $k => $v) {
427
			$object->$k = $v;
417
			$object->$k = $v;
428
		}
418
		}
Zeile 431... Zeile 421...
431
	}
421
	}
Zeile 432... Zeile 422...
432
 
422
 
433
 
423
 
434
	/**
-
 
435
	 * Converts value to array key.
-
 
436
	 * @param  mixed  $value
424
	/**
437
	 * @return array-key
425
	 * Converts value to array key.
438
	 */
426
	 */
439
	public static function toKey($value)
427
	public static function toKey(mixed $value): int|string
440
	{
428
	{