PageRenderTime 75ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/nette-dev/ArrayTools.php

https://github.com/bazo/Mokuji
PHP | 157 lines | 65 code | 27 blank | 65 comment | 9 complexity | bd27caff6c9fd70e33ac6dabb7c884b6 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette
  10. */
  11. /**
  12. * Array tools library.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette
  16. */
  17. final class ArrayTools
  18. {
  19. /**
  20. * Static class - cannot be instantiated.
  21. */
  22. final public function __construct()
  23. {
  24. throw new LogicException("Cannot instantiate static class " . get_class($this));
  25. }
  26. /**
  27. * Returns array item or $default if item is not set.
  28. * Example: $val = ArrayTools::get($arr, 'i', 123);
  29. * @param mixed array
  30. * @param mixed key
  31. * @param mixed default value
  32. * @return mixed
  33. */
  34. public static function get(array $arr, $key, $default = NULL)
  35. {
  36. foreach (is_array($key) ? $key : array($key) as $k) {
  37. if (is_array($arr) && array_key_exists($k, $arr)) {
  38. $arr = $arr[$k];
  39. } else {
  40. return $default;
  41. }
  42. }
  43. return $arr;
  44. }
  45. /**
  46. * Returns reference to array item or $default if item is not set.
  47. * @param mixed array
  48. * @param mixed key
  49. * @return mixed
  50. */
  51. public static function & getRef(& $arr, $key)
  52. {
  53. foreach (is_array($key) ? $key : array($key) as $k) {
  54. if (is_array($arr) || $arr === NULL) {
  55. $arr = & $arr[$k];
  56. } else {
  57. throw new InvalidArgumentException('Traversed item is not an array.');
  58. }
  59. }
  60. return $arr;
  61. }
  62. /**
  63. * Recursively appends elements of remaining keys from the second array to the first.
  64. * @param array
  65. * @param array
  66. * @return array
  67. */
  68. public static function mergeTree($arr1, $arr2)
  69. {
  70. $res = $arr1 + $arr2;
  71. foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
  72. if (is_array($v) && is_array($arr2[$k])) {
  73. $res[$k] = self::mergeTree($v, $arr2[$k]);
  74. }
  75. }
  76. return $res;
  77. }
  78. /**
  79. * Searches the array for a given key and returns the offset if successful.
  80. * @param array input array
  81. * @param mixed key
  82. * @return int offset if it is found, FALSE otherwise
  83. */
  84. public static function searchKey($arr, $key)
  85. {
  86. $foo = array($key => NULL);
  87. return array_search(key($foo), array_keys($arr), TRUE);
  88. }
  89. /**
  90. * Inserts new array before item specified by key.
  91. * @param array input array
  92. * @param mixed key
  93. * @param array inserted array
  94. * @return void
  95. */
  96. public static function insertBefore(array &$arr, $key, array $inserted)
  97. {
  98. $offset = self::searchKey($arr, $key);
  99. $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
  100. }
  101. /**
  102. * Inserts new array after item specified by key.
  103. * @param array input array
  104. * @param mixed key
  105. * @param array inserted array
  106. * @return void
  107. */
  108. public static function insertAfter(array &$arr, $key, array $inserted)
  109. {
  110. $offset = self::searchKey($arr, $key);
  111. $offset = $offset === FALSE ? count($arr) : $offset + 1;
  112. $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
  113. }
  114. /**
  115. * Renames key in array.
  116. * @param array
  117. * @param mixed old key
  118. * @param mixed new key
  119. * @return void
  120. */
  121. public static function renameKey(array &$arr, $oldKey, $newKey)
  122. {
  123. $offset = self::searchKey($arr, $oldKey);
  124. if ($offset !== FALSE) {
  125. $keys = array_keys($arr);
  126. $keys[$offset] = $newKey;
  127. $arr = array_combine($keys, $arr);
  128. }
  129. }
  130. }