1: <?php
2: /**
3: * TbArray class file.
4: * @author Christoffer Niska <[email protected]>
5: * @copyright Copyright © Christoffer Niska 2013-
6: * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
7: * @package bootstrap.helpers
8: */
9:
10: /**
11: * Array helper class.
12: */
13: class TbArray
14: {
15: /**
16: * Returns a specific value from the given array (or the default value if not set).
17: * @param string $key the item key.
18: * @param array $array the array to get from.
19: * @param mixed $defaultValue the default value.
20: * @return mixed the value.
21: */
22: public static function getValue($key, array $array, $defaultValue = null)
23: {
24: return isset($array[$key]) ? $array[$key] : $defaultValue;
25: }
26:
27: /**
28: * Removes and returns a specific value from the given array (or the default value if not set).
29: * @param string $key the item key.
30: * @param array $array the array to pop the item from.
31: * @param mixed $defaultValue the default value.
32: * @return mixed the value.
33: */
34: public static function popValue($key, array &$array, $defaultValue = null)
35: {
36: $value = self::getValue($key, $array, $defaultValue);
37: unset($array[$key]);
38: return $value;
39: }
40:
41: /**
42: * Sets the default value for a specific key in the given array.
43: * @param string $key the item key.
44: * @param mixed $value the default value.
45: * @param array $array the array.
46: */
47: public static function defaultValue($key, $value, array &$array)
48: {
49: if (!isset($array[$key])) {
50: $array[$key] = $value;
51: }
52: }
53:
54: /**
55: * Sets a set of default values for the given array.
56: * @param array $array the array to set values for.
57: * @param array $values the default values.
58: */
59: public static function defaultValues(array $values, array &$array)
60: {
61: foreach ($values as $name => $value) {
62: self::defaultValue($name, $value, $array);
63: }
64: }
65:
66: /**
67: * Removes a specific value from the given array.
68: * @param string $key the item key.
69: */
70: public static function removeValue($key, array &$array)
71: {
72: unset($array[$key]);
73: }
74:
75: /**
76: * Removes a set of items from the given array.
77: * @param array $keys the keys to remove.
78: * @param array $array the array to remove from.
79: */
80: public static function removeValues(array $keys, array &$array)
81: {
82: $array = array_diff_key($array, array_flip($keys));
83: }
84:
85: /**
86: * Copies the given values from one array to another.
87: * @param array $keys the keys to copy.
88: * @param array $from the array to copy from.
89: * @param array $to the array to copy to.
90: * @param boolean $force whether to allow overriding of existing values.
91: * @return array the options.
92: */
93: public static function copyValues(array $keys, array $from, array $to, $force = false)
94: {
95: foreach ($keys as $key) {
96: if (isset($from[$key])) {
97: if ($force || !isset($to[$key])) {
98: $to[$key] = self::getValue($key, $from);
99: }
100: }
101: }
102: return $to;
103: }
104:
105: /**
106: * Moves the given values from one array to another.
107: * @param array $keys the keys to move.
108: * @param array $from the array to move from.
109: * @param array $to the array to move to.
110: * @param boolean $force whether to allow overriding of existing values.
111: * @return array the options.
112: */
113: public static function moveValues(array $keys, array &$from, array $to, $force = false)
114: {
115: foreach ($keys as $key) {
116: if (isset($from[$key])) {
117: $value = self::popValue($key, $from);
118: if ($force || !isset($to[$key])) {
119: $to[$key] = $value;
120: unset($from[$key]);
121: }
122: }
123: }
124: return $to;
125: }
126:
127: /**
128: * Merges two arrays.
129: * @param array $to array to be merged to.
130: * @param array $from array to be merged from.
131: * @return array the merged array.
132: */
133: public static function merge(array $to, array $from)
134: {
135: $args = func_get_args();
136: $res = array_shift($args);
137: while (!empty($args)) {
138: $next = array_shift($args);
139: foreach ($next as $k => $v) {
140: if (is_integer($k)) {
141: isset($res[$k]) ? $res[] = $v : $res[$k] = $v;
142: } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
143: $res[$k] = self::merge($res[$k], $v);
144: } else {
145: $res[$k] = $v;
146: }
147: }
148: }
149: return $res;
150: }
151: }