/libs/Nette/Utils/ArrayTools.php

https://github.com/GunioRobot/IconStore · PHP · 176 lines · 74 code · 32 blank · 70 comment · 9 complexity · 72e5dbc52872dfefa5f342d255c96077 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Nette Framework.
  4. *
  5. * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license", and/or
  8. * GPL license. For more information please see http://nette.org
  9. */
  10. namespace Nette;
  11. use Nette;
  12. /**
  13. * Array tools library.
  14. *
  15. * @author David Grudl
  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. /**
  131. * Return array entries that match the pattern.
  132. * @param array
  133. * @param string
  134. * @param int
  135. * @return array
  136. */
  137. public static function grep(array $arr, $pattern, $flags = 0)
  138. {
  139. Debug::tryError();
  140. $res = preg_grep($pattern, $arr, $flags);
  141. String::catchPregError($pattern);
  142. return $res;
  143. }
  144. }