Source for file ArrayTools.php

Documentation is available at ArrayTools.php

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