1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Utils;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: class Arrays
19: {
20:
21: 22: 23:
24: final public function __construct()
25: {
26: throw new Nette\StaticClassException;
27: }
28:
29:
30: 31: 32: 33: 34: 35: 36: 37:
38: public static function get(array $arr, $key, $default = NULL)
39: {
40: foreach (is_array($key) ? $key : array($key) as $k) {
41: if (is_array($arr) && array_key_exists($k, $arr)) {
42: $arr = $arr[$k];
43: } else {
44: if (func_num_args() < 3) {
45: throw new Nette\InvalidArgumentException("Missing item '$k'.");
46: }
47: return $default;
48: }
49: }
50: return $arr;
51: }
52:
53:
54: 55: 56: 57: 58: 59: 60:
61: public static function & getRef(& $arr, $key)
62: {
63: foreach (is_array($key) ? $key : array($key) as $k) {
64: if (is_array($arr) || $arr === NULL) {
65: $arr = & $arr[$k];
66: } else {
67: throw new Nette\InvalidArgumentException('Traversed item is not an array.');
68: }
69: }
70: return $arr;
71: }
72:
73:
74: 75: 76: 77:
78: public static function mergeTree($arr1, $arr2)
79: {
80: $res = $arr1 + $arr2;
81: foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
82: if (is_array($v) && is_array($arr2[$k])) {
83: $res[$k] = self::mergeTree($v, $arr2[$k]);
84: }
85: }
86: return $res;
87: }
88:
89:
90: 91: 92: 93:
94: public static function searchKey($arr, $key)
95: {
96: $foo = array($key => NULL);
97: return array_search(key($foo), array_keys($arr), TRUE);
98: }
99:
100:
101: 102: 103: 104:
105: public static function insertBefore(array & $arr, $key, array $inserted)
106: {
107: $offset = self::searchKey($arr, $key);
108: $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
109: }
110:
111:
112: 113: 114: 115:
116: public static function insertAfter(array & $arr, $key, array $inserted)
117: {
118: $offset = self::searchKey($arr, $key);
119: $offset = $offset === FALSE ? count($arr) : $offset + 1;
120: $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
121: }
122:
123:
124: 125: 126: 127:
128: public static function renameKey(array & $arr, $oldKey, $newKey)
129: {
130: $offset = self::searchKey($arr, $oldKey);
131: if ($offset !== FALSE) {
132: $keys = array_keys($arr);
133: $keys[$offset] = $newKey;
134: $arr = array_combine($keys, $arr);
135: }
136: }
137:
138:
139: 140: 141: 142:
143: public static function grep(array $arr, $pattern, $flags = 0)
144: {
145: set_error_handler(function($severity, $message) use ($pattern) {
146: restore_error_handler();
147: throw new RegexpException("$message in pattern: $pattern");
148: });
149: $res = preg_grep($pattern, $arr, $flags);
150: restore_error_handler();
151: if (preg_last_error()) {
152: throw new RegexpException(NULL, preg_last_error(), $pattern);
153: }
154: return $res;
155: }
156:
157:
158: 159: 160: 161:
162: public static function flatten(array $arr)
163: {
164: $res = array();
165: array_walk_recursive($arr, function($a) use (& $res) { $res[] = $a; });
166: return $res;
167: }
168:
169: }
170: