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