1: <?php
2: /**
3: * This file contains the array utility class.
4: *
5: * @package Core
6: * @subpackage Util
7: * @version SVN Revision $Rev:$
8: *
9: * @author Murat Purc <murat@purc.de>
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * Array helper class.
20: *
21: * @package Core
22: * @subpackage Util
23: */
24: class cArray {
25:
26: /**
27: * Strip whitespaces (or other characters) from the beginning and end of
28: * each item in array.
29: * Similar to trim() function.
30: *
31: * @param array $arr Array of strings that will be trimmed.
32: * @param string $charlist Optionally, the stripped characters can also be
33: * specified using the charlist parameter. Simply list all characters
34: * that you want to be stripped. With .. you can specify a range of
35: * characters.
36: * @return array Array of trimmed strings.
37: */
38: public static function trim(array $arr, $charlist = NULL) {
39: foreach ($arr as $key => $value) {
40: $arr[$key] = trim($value, $charlist);
41: }
42:
43: return $arr;
44: }
45:
46: /**
47: * Search for given value in given array and return key of its first
48: * occurance.
49: *
50: * If value wasn't found at all false will be returned. If given array
51: * contains subarrays, these will be searched too. If value is found in
52: * subarray the returned key is that of the subarray.
53: *
54: * Usually the values are tested for equality with the given $search. If the
55: * flag $partial is not false values are tested to contain $search.
56: * Otherwise, if $strict equals true values are tested for identity with
57: * $search. Otherwise (which is the default) values are tested for equality.
58: *
59: * Be carefull when searching by equality in arrays containing values that
60: * are no strings! The same is true for searching by equality for values
61: * that are no strings. PHPs behaviour is quite weird concerning comparision
62: * of different data types. E.g. '0' equals '0.0', 'foo' equals 0, 'foo'
63: * equals 0.0, NULL equals '' and false equals '0'! When dealing with
64: * nonstrings consider to use the strict mode!
65: *
66: * Another caveat is when searching for an empty string when using the
67: * partial mode. This would lead to an error and is considered a bug!
68: *
69: * @param array $arr array to search
70: * @param mixed $search value to search for
71: * @param bool $partial if values are tested to contain $search
72: * @param bool $strict if values are tested for identity
73: * @return mixed key of the array containing the searched value or false
74: * @todo There should be only one flag for $partial and $strict in order to
75: * avoid ambiguities (imagine $partial=true & $strict=true).
76: */
77: public static function searchRecursive(array $arr, $search, $partial = false, $strict = false) {
78: foreach ($arr as $key => $value) {
79: if (is_array($value)) {
80: $ret = self::searchRecursive($value, $search, $partial, $strict);
81: if ($ret !== false) {
82: return $ret;
83: }
84: } else {
85: if ($partial !== false) {
86: // search partial
87: $found = false !== strpos($value, $search);
88: } else if ($strict == true) {
89: // search by identity
90: $found = $value === $search;
91: } else {
92: // search by equality
93: $found = $value == $search;
94: }
95: if ($found) {
96: return $key;
97: }
98: }
99: }
100:
101: return false;
102: }
103:
104: /**
105: * Sorts an array by changing the locale temporary to passed value.
106: *
107: * @param array $arr The array to sort
108: * @param string $locale The locale to change before sorting
109: * @return array Sorted array
110: */
111: public static function sortWithLocale(array $arr, $locale) {
112: $oldlocale = setlocale(LC_COLLATE, 0);
113: setlocale(LC_COLLATE, $locale);
114:
115: uasort($arr, 'strcoll');
116:
117: setlocale(LC_COLLATE, $oldlocale);
118:
119: return $arr;
120: }
121:
122: /**
123: * Very cool algorithm for sorting multi-dimensional arrays.
124: *
125: * Found at http://us2.php.net/manual/en/function.array-multisort.php
126: *
127: * Syntax:
128: * <pre>
129: * $new_array = cArray::csort($array [, 'col1' [, SORT_FLAG [,
130: * SORT_FLAG]]]...);
131: * </pre>
132: *
133: * Explanation:
134: * - $array is the array you want to sort
135: * - 'col1' is the name of the column you want to sort
136: * - SORT_FLAGS are: SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC,
137: * SORT_STRING
138: *
139: * You can repeat the 'col', FLAG, FLAG as often as you want. The highest
140: * prioritiy is given to the first - so the array is sorted by the last
141: * given column first, then the one before ...
142: *
143: * Example:
144: * <pre>
145: * $array = cArray::csort($array, 'town', 'age', SORT_DESC, 'name');
146: * </pre>
147: *
148: * @return array
149: */
150: public static function csort() {
151: $args = func_get_args();
152: $marray = array_shift($args);
153: $msortline = "return(array_multisort(";
154: $i = 0;
155: foreach ($args as $arg) {
156: $i++;
157: if (is_string($arg)) {
158: foreach ($marray as $row) {
159: $a = strtoupper($row[$arg]);
160: $sortarr[$i][] = $a;
161: }
162: } else {
163: $sortarr[$i] = $arg;
164: }
165: $msortline .= "\$sortarr[" . $i . "],";
166: }
167: $msortline .= "\$marray));";
168: @eval($msortline);
169: return $marray;
170: }
171:
172: /**
173: * Ensures that the passed array has the key, sets it by using the value.
174: *
175: * @param array $aArray
176: * @param string $sKey
177: * @param mixed $mDefault
178: * @return boolean
179: */
180: public static function initializeKey(&$aArray, $sKey, $mDefault = '') {
181: if (!is_array($aArray)) {
182: if (isset($aArray)) {
183: return false;
184: }
185: $aArray = array();
186: }
187:
188: if (!array_key_exists($sKey, $aArray)) {
189: $aArray[$sKey] = $mDefault;
190: }
191: }
192: }
193: