| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Utility; |
| 4: | |
| 5: | use Salient\Contract\Core\Jsonable; |
| 6: | use ArrayAccess; |
| 7: | use OutOfRangeException; |
| 8: | use Stringable; |
| 9: | use ValueError; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | final class Arr extends AbstractUtility |
| 17: | { |
| 18: | public const SORT_REGULAR = \SORT_REGULAR; |
| 19: | public const SORT_NUMERIC = \SORT_NUMERIC; |
| 20: | public const SORT_STRING = \SORT_STRING; |
| 21: | public const SORT_LOCALE_STRING = \SORT_LOCALE_STRING; |
| 22: | public const SORT_NATURAL = \SORT_NATURAL; |
| 23: | public const SORT_FLAG_CASE = \SORT_FLAG_CASE; |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | public static function pluck(iterable $array, string $value, $key = null): array |
| 37: | { |
| 38: | if ($key === false) { |
| 39: | $key = null; |
| 40: | } |
| 41: | foreach ($array as $itemKey => $item) { |
| 42: | $itemValue = self::get($item, $value, null); |
| 43: | if ($key === null) { |
| 44: | $plucked[] = $itemValue; |
| 45: | continue; |
| 46: | } elseif ($key === true) { |
| 47: | $itemKey = self::getKey($itemKey, $i); |
| 48: | } else { |
| 49: | $itemKey = self::get($item, $key); |
| 50: | } |
| 51: | $plucked[$itemKey] = $itemValue; |
| 52: | } |
| 53: | return $plucked ?? []; |
| 54: | } |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | public static function get(array $array, string $key, $default = null) |
| 66: | { |
| 67: | foreach (explode('.', $key) as $part) { |
| 68: | if (is_array($array) && array_key_exists($part, $array)) { |
| 69: | $array = $array[$part]; |
| 70: | continue; |
| 71: | } |
| 72: | if (func_num_args() < 3) { |
| 73: | throw new OutOfRangeException(sprintf('Value not found: %s', $key)); |
| 74: | } |
| 75: | return $default; |
| 76: | } |
| 77: | return $array; |
| 78: | } |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | public static function has(array $array, string $key): bool |
| 86: | { |
| 87: | foreach (explode('.', $key) as $part) { |
| 88: | if (!is_array($array) || !array_key_exists($part, $array)) { |
| 89: | return false; |
| 90: | } |
| 91: | $array = $array[$part]; |
| 92: | } |
| 93: | return true; |
| 94: | } |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | public static function first(array $array) |
| 105: | { |
| 106: | return $array ? reset($array) : null; |
| 107: | } |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | public static function last(array $array) |
| 118: | { |
| 119: | return $array ? end($array) : null; |
| 120: | } |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | public static function keyOf(array $array, $value, bool $strict = true) |
| 134: | { |
| 135: | $key = array_search($value, $array, $strict); |
| 136: | if ($key === false) { |
| 137: | throw new OutOfRangeException('Value not found in array'); |
| 138: | } |
| 139: | |
| 140: | return $key; |
| 141: | } |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | public static function search(array $array, $value, bool $strict = true) |
| 154: | { |
| 155: | $key = array_search($value, $array, $strict); |
| 156: | return $key === false |
| 157: | ? null |
| 158: | : $key; |
| 159: | } |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | public static function combine(array $keys, array $values): array |
| 172: | { |
| 173: | $array = @array_combine($keys, $values); |
| 174: | if ($array === false) { |
| 175: | $error = error_get_last(); |
| 176: | throw new ValueError($error['message'] ?? 'array_combine() failed'); |
| 177: | } |
| 178: | return $array; |
| 179: | } |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | public static function shift(array $array, &$shifted = null): array |
| 193: | { |
| 194: | $shifted = array_shift($array); |
| 195: | return $array; |
| 196: | } |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | public static function unshift(array $array, ...$values): array |
| 209: | { |
| 210: | array_unshift($array, ...$values); |
| 211: | return $array; |
| 212: | } |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | public static function pop(array $array, &$popped = null): array |
| 226: | { |
| 227: | $popped = array_pop($array); |
| 228: | return $array; |
| 229: | } |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | public static function push(array $array, ...$values): array |
| 242: | { |
| 243: | array_push($array, ...$values); |
| 244: | return $array; |
| 245: | } |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | public static function extend(array $array, ...$values): array |
| 259: | { |
| 260: | return array_merge($array, array_diff($values, $array)); |
| 261: | } |
| 262: | |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | public static function set(array $array, $key, $value): array |
| 275: | { |
| 276: | $array[$key] = $value; |
| 277: | return $array; |
| 278: | } |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 290: | |
| 291: | public static function setIf(array $array, $key, $value): array |
| 292: | { |
| 293: | if (!array_key_exists($key, $array)) { |
| 294: | $array[$key] = $value; |
| 295: | } |
| 296: | return $array; |
| 297: | } |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | |
| 304: | |
| 305: | |
| 306: | |
| 307: | |
| 308: | |
| 309: | public static function unset(array $array, ...$keys): array |
| 310: | { |
| 311: | foreach ($keys as $key) { |
| 312: | unset($array[$key]); |
| 313: | } |
| 314: | return $array; |
| 315: | } |
| 316: | |
| 317: | |
| 318: | |
| 319: | |
| 320: | |
| 321: | |
| 322: | |
| 323: | |
| 324: | |
| 325: | |
| 326: | |
| 327: | public static function sort( |
| 328: | array $array, |
| 329: | bool $preserveKeys = false, |
| 330: | $callbackOrFlags = \SORT_REGULAR |
| 331: | ): array { |
| 332: | if (is_callable($callbackOrFlags)) { |
| 333: | if ($preserveKeys) { |
| 334: | uasort($array, $callbackOrFlags); |
| 335: | return $array; |
| 336: | } |
| 337: | usort($array, $callbackOrFlags); |
| 338: | return $array; |
| 339: | } |
| 340: | if ($preserveKeys) { |
| 341: | asort($array, $callbackOrFlags); |
| 342: | return $array; |
| 343: | } |
| 344: | sort($array, $callbackOrFlags); |
| 345: | return $array; |
| 346: | } |
| 347: | |
| 348: | |
| 349: | |
| 350: | |
| 351: | |
| 352: | |
| 353: | |
| 354: | |
| 355: | |
| 356: | |
| 357: | |
| 358: | public static function sortDesc( |
| 359: | array $array, |
| 360: | bool $preserveKeys = false, |
| 361: | int $flags = \SORT_REGULAR |
| 362: | ): array { |
| 363: | if ($preserveKeys) { |
| 364: | arsort($array, $flags); |
| 365: | return $array; |
| 366: | } |
| 367: | rsort($array, $flags); |
| 368: | return $array; |
| 369: | } |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: | |
| 379: | |
| 380: | |
| 381: | public static function sortByKey( |
| 382: | array $array, |
| 383: | $callbackOrFlags = \SORT_REGULAR |
| 384: | ): array { |
| 385: | if (is_callable($callbackOrFlags)) { |
| 386: | uksort($array, $callbackOrFlags); |
| 387: | return $array; |
| 388: | } |
| 389: | ksort($array, $callbackOrFlags); |
| 390: | return $array; |
| 391: | } |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: | |
| 402: | |
| 403: | public static function sortByKeyDesc( |
| 404: | array $array, |
| 405: | int $flags = \SORT_REGULAR |
| 406: | ): array { |
| 407: | krsort($array, $flags); |
| 408: | return $array; |
| 409: | } |
| 410: | |
| 411: | |
| 412: | |
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | |
| 419: | |
| 420: | |
| 421: | |
| 422: | |
| 423: | |
| 424: | |
| 425: | |
| 426: | |
| 427: | public static function unique( |
| 428: | iterable $array, |
| 429: | bool $preserveKeys = false, |
| 430: | bool $strict = true |
| 431: | ): array { |
| 432: | $unique = []; |
| 433: | foreach ($array as $key => $value) { |
| 434: | if (in_array($value, $unique, $strict)) { |
| 435: | continue; |
| 436: | } |
| 437: | if ($preserveKeys) { |
| 438: | $unique[self::getKey($key, $i)] = $value; |
| 439: | } else { |
| 440: | $unique[] = $value; |
| 441: | } |
| 442: | } |
| 443: | return $unique; |
| 444: | } |
| 445: | |
| 446: | |
| 447: | |
| 448: | |
| 449: | |
| 450: | |
| 451: | |
| 452: | |
| 453: | public static function isList($value, bool $orEmpty = false): bool |
| 454: | { |
| 455: | if (!is_array($value)) { |
| 456: | return false; |
| 457: | } |
| 458: | if (!$value) { |
| 459: | return $orEmpty; |
| 460: | } |
| 461: | $i = 0; |
| 462: | foreach ($value as $key => $value) { |
| 463: | if ($i++ !== $key) { |
| 464: | return false; |
| 465: | } |
| 466: | } |
| 467: | return true; |
| 468: | } |
| 469: | |
| 470: | |
| 471: | |
| 472: | |
| 473: | |
| 474: | |
| 475: | |
| 476: | public static function hasNumericKeys($value, bool $orEmpty = false): bool |
| 477: | { |
| 478: | if (!is_array($value)) { |
| 479: | return false; |
| 480: | } |
| 481: | if (!$value) { |
| 482: | return $orEmpty; |
| 483: | } |
| 484: | foreach ($value as $key => $value) { |
| 485: | if (!is_int($key)) { |
| 486: | return false; |
| 487: | } |
| 488: | } |
| 489: | return true; |
| 490: | } |
| 491: | |
| 492: | |
| 493: | |
| 494: | |
| 495: | |
| 496: | |
| 497: | |
| 498: | public static function ofArrayKey($value, bool $orEmpty = false): bool |
| 499: | { |
| 500: | return self::ofInt($value, $orEmpty) || self::ofString($value); |
| 501: | } |
| 502: | |
| 503: | |
| 504: | |
| 505: | |
| 506: | |
| 507: | |
| 508: | |
| 509: | public static function ofInt($value, bool $orEmpty = false): bool |
| 510: | { |
| 511: | return self::doIsArrayOf('is_int', $value, $orEmpty); |
| 512: | } |
| 513: | |
| 514: | |
| 515: | |
| 516: | |
| 517: | |
| 518: | |
| 519: | |
| 520: | public static function ofString($value, bool $orEmpty = false): bool |
| 521: | { |
| 522: | return self::doIsArrayOf('is_string', $value, $orEmpty); |
| 523: | } |
| 524: | |
| 525: | |
| 526: | |
| 527: | |
| 528: | |
| 529: | |
| 530: | |
| 531: | |
| 532: | |
| 533: | |
| 534: | public static function of($value, string $class, bool $orEmpty = false): bool |
| 535: | { |
| 536: | return self::doIsArrayOf('is_a', $value, $orEmpty, $class); |
| 537: | } |
| 538: | |
| 539: | |
| 540: | |
| 541: | |
| 542: | |
| 543: | |
| 544: | private static function doIsArrayOf(string $func, $value, bool $orEmpty, ...$args): bool |
| 545: | { |
| 546: | if (!is_array($value)) { |
| 547: | return false; |
| 548: | } |
| 549: | if (!$value) { |
| 550: | return $orEmpty; |
| 551: | } |
| 552: | foreach ($value as $item) { |
| 553: | if (!$func($item, ...$args)) { |
| 554: | return false; |
| 555: | } |
| 556: | } |
| 557: | return true; |
| 558: | } |
| 559: | |
| 560: | |
| 561: | |
| 562: | |
| 563: | |
| 564: | |
| 565: | |
| 566: | |
| 567: | public static function sameValues(array $array1, array $array2, array ...$arrays): bool |
| 568: | { |
| 569: | $last = null; |
| 570: | foreach ([$array1, $array2, ...$arrays] as $array) { |
| 571: | usort($array, fn($a, $b) => gettype($a) <=> gettype($b) ?: $a <=> $b); |
| 572: | if ($last !== null && $last !== $array) { |
| 573: | return false; |
| 574: | } |
| 575: | $last = $array; |
| 576: | } |
| 577: | return true; |
| 578: | } |
| 579: | |
| 580: | |
| 581: | |
| 582: | |
| 583: | |
| 584: | |
| 585: | |
| 586: | |
| 587: | public static function same(array $array1, array $array2, array ...$arrays): bool |
| 588: | { |
| 589: | $last = null; |
| 590: | foreach ([$array1, $array2, ...$arrays] as $array) { |
| 591: | ksort($array, \SORT_STRING); |
| 592: | if ($last !== null && $last !== $array) { |
| 593: | return false; |
| 594: | } |
| 595: | $last = $array; |
| 596: | } |
| 597: | return true; |
| 598: | } |
| 599: | |
| 600: | |
| 601: | |
| 602: | |
| 603: | |
| 604: | |
| 605: | |
| 606: | |
| 607: | |
| 608: | |
| 609: | public static function whereNotNull(iterable $array): array |
| 610: | { |
| 611: | foreach ($array as $key => $value) { |
| 612: | if ($value === null) { |
| 613: | continue; |
| 614: | } |
| 615: | $filtered[self::getKey($key, $i)] = $value; |
| 616: | } |
| 617: | return $filtered ?? []; |
| 618: | } |
| 619: | |
| 620: | |
| 621: | |
| 622: | |
| 623: | |
| 624: | |
| 625: | |
| 626: | |
| 627: | |
| 628: | |
| 629: | public static function whereNotEmpty(iterable $array): array |
| 630: | { |
| 631: | foreach ($array as $key => $value) { |
| 632: | if ($value === null || (string) $value === '') { |
| 633: | continue; |
| 634: | } |
| 635: | $filtered[self::getKey($key, $i)] = $value; |
| 636: | } |
| 637: | return $filtered ?? []; |
| 638: | } |
| 639: | |
| 640: | |
| 641: | |
| 642: | |
| 643: | |
| 644: | |
| 645: | |
| 646: | |
| 647: | |
| 648: | public static function implode( |
| 649: | string $separator, |
| 650: | iterable $array, |
| 651: | ?string $characters = null |
| 652: | ): string { |
| 653: | foreach ($array as $value) { |
| 654: | $value = (string) $value; |
| 655: | if ($characters !== '') { |
| 656: | $value = $characters === null |
| 657: | ? trim($value) |
| 658: | : trim($value, $characters); |
| 659: | } |
| 660: | if ($value === '') { |
| 661: | continue; |
| 662: | } |
| 663: | $filtered[] = $value; |
| 664: | } |
| 665: | return implode($separator, $filtered ?? []); |
| 666: | } |
| 667: | |
| 668: | |
| 669: | |
| 670: | |
| 671: | |
| 672: | |
| 673: | |
| 674: | |
| 675: | |
| 676: | |
| 677: | |
| 678: | |
| 679: | public static function trim( |
| 680: | iterable $array, |
| 681: | ?string $characters = null, |
| 682: | bool $removeEmpty = true, |
| 683: | bool $nullEmpty = false |
| 684: | ): array { |
| 685: | foreach ($array as $key => $value) { |
| 686: | $value = (string) $value; |
| 687: | if ($characters !== '') { |
| 688: | $value = $characters === null |
| 689: | ? trim($value) |
| 690: | : trim($value, $characters); |
| 691: | } |
| 692: | if ($removeEmpty) { |
| 693: | if ($value !== '') { |
| 694: | $trimmed[] = $value; |
| 695: | } |
| 696: | continue; |
| 697: | } |
| 698: | $trimmed[self::getKey($key, $i)] = $nullEmpty && $value === '' |
| 699: | ? null |
| 700: | : $value; |
| 701: | } |
| 702: | return $trimmed ?? []; |
| 703: | } |
| 704: | |
| 705: | |
| 706: | |
| 707: | |
| 708: | |
| 709: | |
| 710: | |
| 711: | |
| 712: | |
| 713: | |
| 714: | public static function lower(iterable $array): array |
| 715: | { |
| 716: | foreach ($array as $key => $value) { |
| 717: | $lower[self::getKey($key, $i)] = Str::lower((string) $value); |
| 718: | } |
| 719: | return $lower ?? []; |
| 720: | } |
| 721: | |
| 722: | |
| 723: | |
| 724: | |
| 725: | |
| 726: | |
| 727: | |
| 728: | |
| 729: | |
| 730: | |
| 731: | public static function upper(iterable $array): array |
| 732: | { |
| 733: | foreach ($array as $key => $value) { |
| 734: | $upper[self::getKey($key, $i)] = Str::upper((string) $value); |
| 735: | } |
| 736: | return $upper ?? []; |
| 737: | } |
| 738: | |
| 739: | |
| 740: | |
| 741: | |
| 742: | |
| 743: | |
| 744: | |
| 745: | |
| 746: | |
| 747: | |
| 748: | public static function snakeCase(iterable $array): array |
| 749: | { |
| 750: | foreach ($array as $key => $value) { |
| 751: | $snakeCase[self::getKey($key, $i)] = Str::snake((string) $value); |
| 752: | } |
| 753: | return $snakeCase ?? []; |
| 754: | } |
| 755: | |
| 756: | |
| 757: | |
| 758: | |
| 759: | |
| 760: | |
| 761: | |
| 762: | |
| 763: | |
| 764: | |
| 765: | |
| 766: | |
| 767: | |
| 768: | |
| 769: | |
| 770: | |
| 771: | public static function toScalars(iterable $array, $null = null): array |
| 772: | { |
| 773: | foreach ($array as $key => $value) { |
| 774: | if ($value === null) { |
| 775: | $value = $null; |
| 776: | } elseif (!is_scalar($value)) { |
| 777: | if (Test::isStringable($value)) { |
| 778: | $value = (string) $value; |
| 779: | } elseif ($value instanceof Jsonable) { |
| 780: | $value = $value->toJson(Json::ENCODE_FLAGS); |
| 781: | } else { |
| 782: | $value = Json::encode($value); |
| 783: | } |
| 784: | } |
| 785: | $scalars[self::getKey($key, $i)] = $value; |
| 786: | } |
| 787: | return $scalars ?? []; |
| 788: | } |
| 789: | |
| 790: | |
| 791: | |
| 792: | |
| 793: | |
| 794: | |
| 795: | |
| 796: | |
| 797: | |
| 798: | |
| 799: | |
| 800: | |
| 801: | |
| 802: | |
| 803: | |
| 804: | public static function toStrings(iterable $array, ?string $null = null): array |
| 805: | { |
| 806: | foreach ($array as $key => $value) { |
| 807: | if ($value === null) { |
| 808: | $value = $null; |
| 809: | } elseif (is_scalar($value) || Test::isStringable($value)) { |
| 810: | $value = (string) $value; |
| 811: | } elseif ($value instanceof Jsonable) { |
| 812: | $value = $value->toJson(Json::ENCODE_FLAGS); |
| 813: | } else { |
| 814: | $value = Json::encode($value); |
| 815: | } |
| 816: | $strings[self::getKey($key, $i)] = $value; |
| 817: | } |
| 818: | return $strings ?? []; |
| 819: | } |
| 820: | |
| 821: | |
| 822: | |
| 823: | |
| 824: | |
| 825: | |
| 826: | |
| 827: | |
| 828: | |
| 829: | |
| 830: | public static function offsetOfKey(array $array, $key): int |
| 831: | { |
| 832: | $offset = array_flip(array_keys($array))[$key] ?? null; |
| 833: | if ($offset === null) { |
| 834: | throw new OutOfRangeException(sprintf('Array key not found: %s', $key)); |
| 835: | } |
| 836: | return $offset; |
| 837: | } |
| 838: | |
| 839: | |
| 840: | |
| 841: | |
| 842: | |
| 843: | |
| 844: | |
| 845: | |
| 846: | |
| 847: | |
| 848: | |
| 849: | |
| 850: | |
| 851: | public static function rename(array $array, $key, $newKey): array |
| 852: | { |
| 853: | if (!array_key_exists($key, $array)) { |
| 854: | throw new OutOfRangeException(sprintf('Array key not found: %s', $key)); |
| 855: | } |
| 856: | if ($key === $newKey) { |
| 857: | return $array; |
| 858: | } |
| 859: | return self::spliceByKey($array, $key, 1, [$newKey => $array[$key]]); |
| 860: | } |
| 861: | |
| 862: | |
| 863: | |
| 864: | |
| 865: | |
| 866: | |
| 867: | |
| 868: | |
| 869: | |
| 870: | |
| 871: | |
| 872: | |
| 873: | |
| 874: | public static function splice( |
| 875: | array $array, |
| 876: | int $offset, |
| 877: | ?int $length = null, |
| 878: | $replacement = [], |
| 879: | ?array &$replaced = null |
| 880: | ): array { |
| 881: | |
| 882: | if ($length === null) { |
| 883: | $length = count($array); |
| 884: | } |
| 885: | $replaced = array_splice($array, $offset, $length, $replacement); |
| 886: | return $array; |
| 887: | } |
| 888: | |
| 889: | |
| 890: | |
| 891: | |
| 892: | |
| 893: | |
| 894: | |
| 895: | |
| 896: | |
| 897: | |
| 898: | |
| 899: | |
| 900: | |
| 901: | |
| 902: | |
| 903: | public static function spliceByKey( |
| 904: | array $array, |
| 905: | $key, |
| 906: | ?int $length = null, |
| 907: | array $replacement = [], |
| 908: | ?array &$replaced = null |
| 909: | ): array { |
| 910: | $keys = array_keys($array); |
| 911: | $offset = array_flip($keys)[$key] ?? null; |
| 912: | if ($offset === null) { |
| 913: | throw new OutOfRangeException(sprintf('Array key not found: %s', $key)); |
| 914: | } |
| 915: | |
| 916: | if ($length === null) { |
| 917: | $length = count($array); |
| 918: | } |
| 919: | $replaced = self::combine( |
| 920: | array_splice($keys, $offset, $length, array_keys($replacement)), |
| 921: | array_splice($array, $offset, $length, $replacement), |
| 922: | ); |
| 923: | return self::combine($keys, $array); |
| 924: | } |
| 925: | |
| 926: | |
| 927: | |
| 928: | |
| 929: | |
| 930: | |
| 931: | |
| 932: | |
| 933: | |
| 934: | |
| 935: | |
| 936: | public static function toIndex(iterable $array, $value = true): array |
| 937: | { |
| 938: | if (is_array($array)) { |
| 939: | return array_fill_keys($array, $value); |
| 940: | } |
| 941: | foreach ($array as $key) { |
| 942: | $index[$key] = $value; |
| 943: | } |
| 944: | return $index ?? []; |
| 945: | } |
| 946: | |
| 947: | |
| 948: | |
| 949: | |
| 950: | |
| 951: | |
| 952: | |
| 953: | |
| 954: | |
| 955: | |
| 956: | public static function toMap(iterable $array, $key): array |
| 957: | { |
| 958: | foreach ($array as $item) { |
| 959: | $map[ |
| 960: | is_array($item) || $item instanceof ArrayAccess |
| 961: | ? $item[$key] |
| 962: | : $item->$key |
| 963: | ] = $item; |
| 964: | } |
| 965: | return $map ?? []; |
| 966: | } |
| 967: | |
| 968: | |
| 969: | |
| 970: | |
| 971: | |
| 972: | |
| 973: | |
| 974: | |
| 975: | |
| 976: | |
| 977: | |
| 978: | |
| 979: | |
| 980: | |
| 981: | |
| 982: | |
| 983: | |
| 984: | |
| 985: | public static function with(iterable $array, callable $callback, $value) |
| 986: | { |
| 987: | foreach ($array as $arrKey => $arrValue) { |
| 988: | $value = $callback($value, $arrValue, $arrKey); |
| 989: | } |
| 990: | return $value; |
| 991: | } |
| 992: | |
| 993: | |
| 994: | |
| 995: | |
| 996: | |
| 997: | |
| 998: | |
| 999: | |
| 1000: | |
| 1001: | public static function flatten( |
| 1002: | iterable $array, |
| 1003: | int $limit = -1, |
| 1004: | bool $preserveKeys = false |
| 1005: | ): array { |
| 1006: | do { |
| 1007: | $flattened = []; |
| 1008: | $i = null; |
| 1009: | $fromIterable = false; |
| 1010: | foreach ($array as $key => $value) { |
| 1011: | if (!is_iterable($value) || !$limit) { |
| 1012: | if ($preserveKeys) { |
| 1013: | $flattened[self::getKey($key, $i)] = $value; |
| 1014: | } else { |
| 1015: | $flattened[] = $value; |
| 1016: | } |
| 1017: | continue; |
| 1018: | } |
| 1019: | $fromIterable = true; |
| 1020: | foreach ($value as $key => $value) { |
| 1021: | if ($preserveKeys && ($limit === 1 || !is_iterable($value))) { |
| 1022: | $flattened[self::getKey($key, $i)] = $value; |
| 1023: | } else { |
| 1024: | $flattened[] = $value; |
| 1025: | } |
| 1026: | } |
| 1027: | } |
| 1028: | $limit--; |
| 1029: | } while ($fromIterable && $limit && ($array = $flattened)); |
| 1030: | |
| 1031: | return $flattened; |
| 1032: | } |
| 1033: | |
| 1034: | |
| 1035: | |
| 1036: | |
| 1037: | |
| 1038: | |
| 1039: | |
| 1040: | |
| 1041: | |
| 1042: | public static function wrap($value): array |
| 1043: | { |
| 1044: | if ($value === null) { |
| 1045: | return []; |
| 1046: | } |
| 1047: | return is_array($value) ? $value : [$value]; |
| 1048: | } |
| 1049: | |
| 1050: | |
| 1051: | |
| 1052: | |
| 1053: | |
| 1054: | |
| 1055: | |
| 1056: | |
| 1057: | |
| 1058: | public static function wrapList($value): array |
| 1059: | { |
| 1060: | if ($value === null) { |
| 1061: | return []; |
| 1062: | } |
| 1063: | |
| 1064: | return self::isList($value, true) ? $value : [$value]; |
| 1065: | } |
| 1066: | |
| 1067: | |
| 1068: | |
| 1069: | |
| 1070: | |
| 1071: | |
| 1072: | |
| 1073: | |
| 1074: | |
| 1075: | public static function unwrap($value, int $limit = -1) |
| 1076: | { |
| 1077: | while ( |
| 1078: | $limit |
| 1079: | && is_array($value) |
| 1080: | && count($value) === 1 |
| 1081: | && array_key_first($value) === 0 |
| 1082: | ) { |
| 1083: | $value = $value[0]; |
| 1084: | $limit--; |
| 1085: | } |
| 1086: | return $value; |
| 1087: | } |
| 1088: | |
| 1089: | |
| 1090: | |
| 1091: | |
| 1092: | |
| 1093: | |
| 1094: | |
| 1095: | private static function getKey($key, ?int &$i) |
| 1096: | { |
| 1097: | if (is_int($key)) { |
| 1098: | if ($i === null || $key > $i) { |
| 1099: | $i = $key; |
| 1100: | } |
| 1101: | return $key; |
| 1102: | } |
| 1103: | if (is_string($key)) { |
| 1104: | return $key; |
| 1105: | } |
| 1106: | if ($i === null) { |
| 1107: | return $i = 0; |
| 1108: | } |
| 1109: | return ++$i; |
| 1110: | } |
| 1111: | } |
| 1112: | |