|
7 | 7 | use Lkrms\Http\Uri; |
8 | 8 | use Lkrms\Support\Catalog\RegularExpression as Regex; |
9 | 9 | use Lkrms\Support\DateFormatter; |
10 | | -use ArrayAccess; |
11 | | -use ArrayIterator; |
12 | | -use Closure; |
13 | 10 | use DateInterval; |
14 | 11 | use DateTimeImmutable; |
15 | 12 | use DateTimeInterface; |
16 | 13 | use DateTimeZone; |
17 | | -use Iterator; |
18 | | -use IteratorIterator; |
19 | 14 | use LogicException; |
20 | 15 | use Stringable; |
21 | 16 |
|
@@ -275,38 +270,16 @@ public static function coalesce(...$values) |
275 | 270 | } |
276 | 271 |
|
277 | 272 | /** |
278 | | - * If an iterable isn't already an array, make it one |
279 | | - * |
280 | 273 | * @template TKey of array-key |
281 | 274 | * @template TValue |
282 | | - * |
283 | 275 | * @param iterable<TKey,TValue> $iterable |
284 | 276 | * @return array<TKey,TValue> |
| 277 | + * @deprecated Use {@see Get::array()} instead |
| 278 | + * @codeCoverageIgnore |
285 | 279 | */ |
286 | 280 | public static function iterableToArray(iterable $iterable, bool $preserveKeys = false): array |
287 | 281 | { |
288 | | - return is_array($iterable) ? $iterable : iterator_to_array($iterable, $preserveKeys); |
289 | | - } |
290 | | - |
291 | | - /** |
292 | | - * If an iterable isn't already an Iterator, enclose it in one |
293 | | - * |
294 | | - * @template TKey of array-key |
295 | | - * @template TValue |
296 | | - * |
297 | | - * @param iterable<TKey,TValue> $iterable |
298 | | - * @return Iterator<TKey,TValue> |
299 | | - */ |
300 | | - public static function iterableToIterator(iterable $iterable): Iterator |
301 | | - { |
302 | | - if ($iterable instanceof Iterator) { |
303 | | - return $iterable; |
304 | | - } |
305 | | - if (is_array($iterable)) { |
306 | | - return new ArrayIterator($iterable); |
307 | | - } |
308 | | - |
309 | | - return new IteratorIterator($iterable); |
| 282 | + return Get::array($iterable, $preserveKeys); |
310 | 283 | } |
311 | 284 |
|
312 | 285 | /** |
@@ -385,123 +358,16 @@ public static function classToNamespace(string $class): string |
385 | 358 | } |
386 | 359 |
|
387 | 360 | /** |
388 | | - * Create a map from a list |
389 | | - * |
390 | | - * For example, to map from each array's `id` to the array itself: |
391 | | - * |
392 | | - * ```php |
393 | | - * $list = [ |
394 | | - * ['id' => 32, 'name' => 'Greta'], |
395 | | - * ['id' => 71, 'name' => 'Terry'], |
396 | | - * ]; |
397 | | - * |
398 | | - * $map = Convert::listToMap($list, 'id'); |
399 | | - * |
400 | | - * print_r($map); |
401 | | - * ``` |
402 | | - * |
403 | | - * ``` |
404 | | - * Array |
405 | | - * ( |
406 | | - * [32] => Array |
407 | | - * ( |
408 | | - * [id] => 32 |
409 | | - * [name] => Greta |
410 | | - * ) |
411 | | - * |
412 | | - * [71] => Array |
413 | | - * ( |
414 | | - * [id] => 71 |
415 | | - * [name] => Terry |
416 | | - * ) |
417 | | - * |
418 | | - * ) |
419 | | - * ``` |
420 | | - * |
421 | 361 | * @template T of mixed[]|object |
422 | | - * |
423 | 362 | * @param array<T> $list |
424 | | - * @param int|string|(Closure(T): (int|string)) $key Either the index or |
425 | | - * property name to use when retrieving keys from arrays or objects in |
426 | | - * `$list`, or a closure that returns a key for each item in `$list`. |
| 363 | + * @param int|string $key |
427 | 364 | * @return array<T> |
| 365 | + * @deprecated Use {@see Arr::toMap()} instead |
| 366 | + * @codeCoverageIgnore |
428 | 367 | */ |
429 | 368 | public static function listToMap(array $list, $key): array |
430 | 369 | { |
431 | | - return array_combine( |
432 | | - array_map(self::_keyToClosure($key), $list), |
433 | | - $list |
434 | | - ); |
435 | | - } |
436 | | - |
437 | | - /** |
438 | | - * Get the first item in $list where the value at $key is $value |
439 | | - * |
440 | | - * @template T0 of mixed[]|object |
441 | | - * @template T1 |
442 | | - * |
443 | | - * @param iterable<array-key,T0> $list |
444 | | - * @param int|string|(Closure(T0): T1) $key Either the index or property |
445 | | - * name to use when retrieving values from arrays or objects in `$list`, or |
446 | | - * a closure that returns a value for each item in `$list`. |
447 | | - * @param T1 $value |
448 | | - * @return T0|false `false` if no item was found in `$list` with `$value` at |
449 | | - * `$key`. |
450 | | - */ |
451 | | - public static function iterableToItem(iterable $list, $key, $value, bool $strict = false) |
452 | | - { |
453 | | - $list = self::iterableToIterator($list); |
454 | | - $closure = self::_keyToClosure($key); |
455 | | - |
456 | | - while ($list->valid()) { |
457 | | - $item = $list->current(); |
458 | | - $list->next(); |
459 | | - if (($strict && ($closure($item) === $value)) || |
460 | | - (!$strict && ($closure($item) == $value))) { |
461 | | - return $item; |
462 | | - } |
463 | | - } |
464 | | - |
465 | | - return false; |
466 | | - } |
467 | | - |
468 | | - /** |
469 | | - * @param int|string|Closure $key |
470 | | - */ |
471 | | - private static function _keyToClosure($key): Closure |
472 | | - { |
473 | | - return $key instanceof Closure |
474 | | - ? $key |
475 | | - : fn($item) => self::valueAtKey($item, $key); |
476 | | - } |
477 | | - |
478 | | - /** |
479 | | - * Get the value at $key in $item, where $item is an array or object |
480 | | - * |
481 | | - * @param mixed[]|ArrayAccess|object $item |
482 | | - * @param int|string $key |
483 | | - * @return mixed |
484 | | - */ |
485 | | - public static function valueAtKey($item, $key) |
486 | | - { |
487 | | - return is_array($item) || $item instanceof ArrayAccess |
488 | | - ? $item[$key] |
489 | | - : $item->$key; |
490 | | - } |
491 | | - |
492 | | - /** |
493 | | - * Convert a scalar to a string |
494 | | - * |
495 | | - * @param mixed $value |
496 | | - * @return string|false Returns `false` if `$value` is not a scalar |
497 | | - */ |
498 | | - public static function scalarToString($value) |
499 | | - { |
500 | | - if (is_scalar($value)) { |
501 | | - return (string) $value; |
502 | | - } else { |
503 | | - return false; |
504 | | - } |
| 370 | + return Arr::toMap($list, $key); |
505 | 371 | } |
506 | 372 |
|
507 | 373 | /** |
|
0 commit comments