RecursiveArrayAccess.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\Data\Util;
  11. use Symfony\Component\Intl\Exception\OutOfBoundsException;
  12. /**
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. *
  15. * @internal
  16. */
  17. class RecursiveArrayAccess
  18. {
  19. public static function get($array, array $indices)
  20. {
  21. foreach ($indices as $index) {
  22. // Use array_key_exists() for arrays, isset() otherwise
  23. if (is_array($array)) {
  24. if (array_key_exists($index, $array)) {
  25. $array = $array[$index];
  26. continue;
  27. }
  28. } elseif ($array instanceof \ArrayAccess) {
  29. if (isset($array[$index])) {
  30. $array = $array[$index];
  31. continue;
  32. }
  33. }
  34. throw new OutOfBoundsException(sprintf(
  35. 'The index %s does not exist.',
  36. $index
  37. ));
  38. }
  39. return $array;
  40. }
  41. private function __construct()
  42. {
  43. }
  44. }