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