| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Library of array functions for Cake.
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice.
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
14 |
* @package Cake.Utility
|
|
|
15 |
* @since CakePHP(tm) v 1.2.0
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('String', 'Utility');
|
|
|
20 |
App::uses('Hash', 'Utility');
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Class used for manipulation of arrays.
|
|
|
24 |
*
|
|
|
25 |
* @package Cake.Utility
|
|
|
26 |
*/
|
|
|
27 |
class Set {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* This function can be thought of as a hybrid between PHP's array_merge and array_merge_recursive. The difference
|
|
|
31 |
* to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge)
|
|
|
32 |
* but does not do if for keys containing strings (unlike array_merge_recursive).
|
|
|
33 |
*
|
|
|
34 |
* Since this method emulates `array_merge`, it will re-order numeric keys. When combined with out of
|
|
|
35 |
* order numeric keys containing arrays, results can be lossy.
|
|
|
36 |
*
|
|
|
37 |
* Note: This function will work with an unlimited amount of arguments and typecasts non-array
|
|
|
38 |
* parameters into arrays.
|
|
|
39 |
*
|
|
|
40 |
* @param array $data Array to be merged
|
|
|
41 |
* @param array $merge Array to merge with
|
|
|
42 |
* @return array Merged array
|
|
|
43 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::merge
|
|
|
44 |
*/
|
|
|
45 |
public static function merge($data, $merge = null) {
|
|
|
46 |
$args = func_get_args();
|
|
|
47 |
if (empty($args[1]) && count($args) <= 2) {
|
|
|
48 |
return (array)$args[0];
|
|
|
49 |
}
|
|
|
50 |
if (!is_array($args[0])) {
|
|
|
51 |
$args[0] = (array)$args[0];
|
|
|
52 |
}
|
|
|
53 |
return call_user_func_array('Hash::merge', $args);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Filters empty elements out of a route array, excluding '0'.
|
|
|
58 |
*
|
|
|
59 |
* @param array $var Either an array to filter, or value when in callback
|
|
|
60 |
* @return mixed Either filtered array, or true/false when in callback
|
|
|
61 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::filter
|
|
|
62 |
*/
|
|
|
63 |
public static function filter(array $var) {
|
|
|
64 |
return Hash::filter($var);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Pushes the differences in $array2 onto the end of $array
|
|
|
69 |
*
|
|
|
70 |
* @param array $array Original array
|
|
|
71 |
* @param array $array2 Differences to push
|
|
|
72 |
* @return array Combined array
|
|
|
73 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::pushDiff
|
|
|
74 |
*/
|
|
|
75 |
public static function pushDiff($array, $array2) {
|
|
|
76 |
if (empty($array) && !empty($array2)) {
|
|
|
77 |
return $array2;
|
|
|
78 |
}
|
|
|
79 |
if (!empty($array) && !empty($array2)) {
|
|
|
80 |
foreach ($array2 as $key => $value) {
|
|
|
81 |
if (!array_key_exists($key, $array)) {
|
|
|
82 |
$array[$key] = $value;
|
|
|
83 |
} else {
|
|
|
84 |
if (is_array($value)) {
|
|
|
85 |
$array[$key] = Set::pushDiff($array[$key], $array2[$key]);
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
return $array;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Maps the contents of the Set object to an object hierarchy.
|
|
|
95 |
* Maintains numeric keys as arrays of objects
|
|
|
96 |
*
|
|
|
97 |
* @param string $class A class name of the type of object to map to
|
|
|
98 |
* @param string $tmp A temporary class name used as $class if $class is an array
|
|
|
99 |
* @return object Hierarchical object
|
|
|
100 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::map
|
|
|
101 |
*/
|
|
|
102 |
public static function map($class = 'stdClass', $tmp = 'stdClass') {
|
|
|
103 |
if (is_array($class)) {
|
|
|
104 |
$val = $class;
|
|
|
105 |
$class = $tmp;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
if (empty($val)) {
|
|
|
109 |
return null;
|
|
|
110 |
}
|
|
|
111 |
return Set::_map($val, $class);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Maps the given value as an object. If $value is an object,
|
|
|
116 |
* it returns $value. Otherwise it maps $value as an object of
|
|
|
117 |
* type $class, and if primary assign _name_ $key on first array.
|
|
|
118 |
* If $value is not empty, it will be used to set properties of
|
|
|
119 |
* returned object (recursively). If $key is numeric will maintain array
|
|
|
120 |
* structure
|
|
|
121 |
*
|
|
|
122 |
* @param array $array Array to map
|
|
|
123 |
* @param string $class Class name
|
|
|
124 |
* @param boolean $primary whether to assign first array key as the _name_
|
|
|
125 |
* @return mixed Mapped object
|
|
|
126 |
*/
|
|
|
127 |
protected static function _map(&$array, $class, $primary = false) {
|
|
|
128 |
if ($class === true) {
|
|
|
129 |
$out = new stdClass;
|
|
|
130 |
} else {
|
|
|
131 |
$out = new $class;
|
|
|
132 |
}
|
|
|
133 |
if (is_array($array)) {
|
|
|
134 |
$keys = array_keys($array);
|
|
|
135 |
foreach ($array as $key => $value) {
|
|
|
136 |
if ($keys[0] === $key && $class !== true) {
|
|
|
137 |
$primary = true;
|
|
|
138 |
}
|
|
|
139 |
if (is_numeric($key)) {
|
|
|
140 |
if (is_object($out)) {
|
|
|
141 |
$out = get_object_vars($out);
|
|
|
142 |
}
|
|
|
143 |
$out[$key] = Set::_map($value, $class);
|
|
|
144 |
if (is_object($out[$key])) {
|
|
|
145 |
if ($primary !== true && is_array($value) && Set::countDim($value, true) === 2) {
|
|
|
146 |
if (!isset($out[$key]->_name_)) {
|
|
|
147 |
$out[$key]->_name_ = $primary;
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
} elseif (is_array($value)) {
|
|
|
152 |
if ($primary === true) {
|
|
|
153 |
// @codingStandardsIgnoreStart Legacy junk
|
|
|
154 |
if (!isset($out->_name_)) {
|
|
|
155 |
$out->_name_ = $key;
|
|
|
156 |
}
|
|
|
157 |
// @codingStandardsIgnoreEnd
|
|
|
158 |
$primary = false;
|
|
|
159 |
foreach ($value as $key2 => $value2) {
|
|
|
160 |
$out->{$key2} = Set::_map($value2, true);
|
|
|
161 |
}
|
|
|
162 |
} else {
|
|
|
163 |
if (!is_numeric($key)) {
|
|
|
164 |
$out->{$key} = Set::_map($value, true, $key);
|
|
|
165 |
if (is_object($out->{$key}) && !is_numeric($key)) {
|
|
|
166 |
if (!isset($out->{$key}->_name_)) {
|
|
|
167 |
$out->{$key}->_name_ = $key;
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
} else {
|
|
|
171 |
$out->{$key} = Set::_map($value, true);
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
} else {
|
|
|
175 |
$out->{$key} = $value;
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
} else {
|
|
|
179 |
$out = $array;
|
|
|
180 |
}
|
|
|
181 |
return $out;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* Checks to see if all the values in the array are numeric
|
|
|
186 |
*
|
|
|
187 |
* @param array $array The array to check. If null, the value of the current Set object
|
|
|
188 |
* @return boolean true if values are numeric, false otherwise
|
|
|
189 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::numeric
|
|
|
190 |
*/
|
|
|
191 |
public static function numeric($array = null) {
|
|
|
192 |
return Hash::numeric($array);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* Return a value from an array list if the key exists.
|
|
|
197 |
*
|
|
|
198 |
* If a comma separated $list is passed arrays are numeric with the key of the first being 0
|
|
|
199 |
* $list = 'no, yes' would translate to $list = array(0 => 'no', 1 => 'yes');
|
|
|
200 |
*
|
|
|
201 |
* If an array is used, keys can be strings example: array('no' => 0, 'yes' => 1);
|
|
|
202 |
*
|
|
|
203 |
* $list defaults to 0 = no 1 = yes if param is not passed
|
|
|
204 |
*
|
|
|
205 |
* @param string $select Key in $list to return
|
|
|
206 |
* @param array|string $list can be an array or a comma-separated list.
|
|
|
207 |
* @return string the value of the array key or null if no match
|
|
|
208 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::enum
|
|
|
209 |
*/
|
|
|
210 |
public static function enum($select, $list = null) {
|
|
|
211 |
if (empty($list)) {
|
|
|
212 |
$list = array('no', 'yes');
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
$return = null;
|
|
|
216 |
$list = Set::normalize($list, false);
|
|
|
217 |
|
|
|
218 |
if (array_key_exists($select, $list)) {
|
|
|
219 |
$return = $list[$select];
|
|
|
220 |
}
|
|
|
221 |
return $return;
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Returns a series of values extracted from an array, formatted in a format string.
|
|
|
226 |
*
|
|
|
227 |
* @param array $data Source array from which to extract the data
|
|
|
228 |
* @param string $format Format string into which values will be inserted, see sprintf()
|
|
|
229 |
* @param array $keys An array containing one or more Set::extract()-style key paths
|
|
|
230 |
* @return array An array of strings extracted from $keys and formatted with $format
|
|
|
231 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::format
|
|
|
232 |
*/
|
|
|
233 |
public static function format($data, $format, $keys) {
|
|
|
234 |
$extracted = array();
|
|
|
235 |
$count = count($keys);
|
|
|
236 |
|
|
|
237 |
if (!$count) {
|
|
|
238 |
return;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
for ($i = 0; $i < $count; $i++) {
|
|
|
242 |
$extracted[] = Set::extract($data, $keys[$i]);
|
|
|
243 |
}
|
|
|
244 |
$out = array();
|
|
|
245 |
$data = $extracted;
|
|
|
246 |
$count = count($data[0]);
|
|
|
247 |
|
|
|
248 |
if (preg_match_all('/\{([0-9]+)\}/msi', $format, $keys2) && isset($keys2[1])) {
|
|
|
249 |
$keys = $keys2[1];
|
|
|
250 |
$format = preg_split('/\{([0-9]+)\}/msi', $format);
|
|
|
251 |
$count2 = count($format);
|
|
|
252 |
|
|
|
253 |
for ($j = 0; $j < $count; $j++) {
|
|
|
254 |
$formatted = '';
|
|
|
255 |
for ($i = 0; $i <= $count2; $i++) {
|
|
|
256 |
if (isset($format[$i])) {
|
|
|
257 |
$formatted .= $format[$i];
|
|
|
258 |
}
|
|
|
259 |
if (isset($keys[$i]) && isset($data[$keys[$i]][$j])) {
|
|
|
260 |
$formatted .= $data[$keys[$i]][$j];
|
|
|
261 |
}
|
|
|
262 |
}
|
|
|
263 |
$out[] = $formatted;
|
|
|
264 |
}
|
|
|
265 |
} else {
|
|
|
266 |
$count2 = count($data);
|
|
|
267 |
for ($j = 0; $j < $count; $j++) {
|
|
|
268 |
$args = array();
|
|
|
269 |
for ($i = 0; $i < $count2; $i++) {
|
|
|
270 |
if (array_key_exists($j, $data[$i])) {
|
|
|
271 |
$args[] = $data[$i][$j];
|
|
|
272 |
}
|
|
|
273 |
}
|
|
|
274 |
$out[] = vsprintf($format, $args);
|
|
|
275 |
}
|
|
|
276 |
}
|
|
|
277 |
return $out;
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
/**
|
|
|
281 |
* Implements partial support for XPath 2.0. If $path does not contain a '/' the call
|
|
|
282 |
* is delegated to Set::classicExtract(). Also the $path and $data arguments are
|
|
|
283 |
* reversible.
|
|
|
284 |
*
|
|
|
285 |
* #### Currently implemented selectors:
|
|
|
286 |
*
|
|
|
287 |
* - /User/id (similar to the classic {n}.User.id)
|
|
|
288 |
* - /User[2]/name (selects the name of the second User)
|
|
|
289 |
* - /User[id>2] (selects all Users with an id > 2)
|
|
|
290 |
* - /User[id>2][<5] (selects all Users with an id > 2 but < 5)
|
|
|
291 |
* - /Post/Comment[author_name=john]/../name (Selects the name of all Posts that have at least one Comment written by john)
|
|
|
292 |
* - /Posts[name] (Selects all Posts that have a 'name' key)
|
|
|
293 |
* - /Comment/.[1] (Selects the contents of the first comment)
|
|
|
294 |
* - /Comment/.[:last] (Selects the last comment)
|
|
|
295 |
* - /Comment/.[:first] (Selects the first comment)
|
|
|
296 |
* - /Comment[text=/cakephp/i] (Selects the all comments that have a text matching the regex /cakephp/i)
|
|
|
297 |
* - /Comment/@* (Selects the all key names of all comments)
|
|
|
298 |
*
|
|
|
299 |
* #### Other limitations:
|
|
|
300 |
*
|
|
|
301 |
* - Only absolute paths starting with a single '/' are supported right now
|
|
|
302 |
*
|
|
|
303 |
* **Warning**: Even so it has plenty of unit tests the XPath support has not gone through a lot of
|
|
|
304 |
* real-world testing. Please report Bugs as you find them. Suggestions for additional features to
|
|
|
305 |
* implement are also very welcome!
|
|
|
306 |
*
|
|
|
307 |
* @param string $path An absolute XPath 2.0 path
|
|
|
308 |
* @param array $data An array of data to extract from
|
|
|
309 |
* @param array $options Currently only supports 'flatten' which can be disabled for higher XPath-ness
|
|
|
310 |
* @return array An array of matched items
|
|
|
311 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::extract
|
|
|
312 |
*/
|
|
|
313 |
public static function extract($path, $data = null, $options = array()) {
|
|
|
314 |
if (is_string($data)) {
|
|
|
315 |
$tmp = $data;
|
|
|
316 |
$data = $path;
|
|
|
317 |
$path = $tmp;
|
|
|
318 |
}
|
|
|
319 |
if (strpos($path, '/') === false) {
|
|
|
320 |
return Set::classicExtract($data, $path);
|
|
|
321 |
}
|
|
|
322 |
if (empty($data)) {
|
|
|
323 |
return array();
|
|
|
324 |
}
|
|
|
325 |
if ($path === '/') {
|
|
|
326 |
return $data;
|
|
|
327 |
}
|
|
|
328 |
$contexts = $data;
|
|
|
329 |
$options = array_merge(array('flatten' => true), $options);
|
|
|
330 |
if (!isset($contexts[0])) {
|
|
|
331 |
$current = current($data);
|
|
|
332 |
if ((is_array($current) && count($data) < 1) || !is_array($current) || !Set::numeric(array_keys($data))) {
|
|
|
333 |
$contexts = array($data);
|
|
|
334 |
}
|
|
|
335 |
}
|
|
|
336 |
$tokens = array_slice(preg_split('/(?<!=|\\\\)\/(?![a-z-\s]*\])/', $path), 1);
|
|
|
337 |
|
|
|
338 |
do {
|
|
|
339 |
$token = array_shift($tokens);
|
|
|
340 |
$conditions = false;
|
|
|
341 |
if (preg_match_all('/\[([^=]+=\/[^\/]+\/|[^\]]+)\]/', $token, $m)) {
|
|
|
342 |
$conditions = $m[1];
|
|
|
343 |
$token = substr($token, 0, strpos($token, '['));
|
|
|
344 |
}
|
|
|
345 |
$matches = array();
|
|
|
346 |
foreach ($contexts as $key => $context) {
|
|
|
347 |
if (!isset($context['trace'])) {
|
|
|
348 |
$context = array('trace' => array(null), 'item' => $context, 'key' => $key);
|
|
|
349 |
}
|
|
|
350 |
if ($token === '..') {
|
|
|
351 |
if (count($context['trace']) === 1) {
|
|
|
352 |
$context['trace'][] = $context['key'];
|
|
|
353 |
}
|
|
|
354 |
$parent = implode('/', $context['trace']) . '/.';
|
|
|
355 |
$context['item'] = Set::extract($parent, $data);
|
|
|
356 |
$context['key'] = array_pop($context['trace']);
|
|
|
357 |
if (isset($context['trace'][1]) && $context['trace'][1] > 0) {
|
|
|
358 |
$context['item'] = $context['item'][0];
|
|
|
359 |
} elseif (!empty($context['item'][$key])) {
|
|
|
360 |
$context['item'] = $context['item'][$key];
|
|
|
361 |
} else {
|
|
|
362 |
$context['item'] = array_shift($context['item']);
|
|
|
363 |
}
|
|
|
364 |
$matches[] = $context;
|
|
|
365 |
continue;
|
|
|
366 |
}
|
|
|
367 |
if ($token === '@*' && is_array($context['item'])) {
|
|
|
368 |
$matches[] = array(
|
|
|
369 |
'trace' => array_merge($context['trace'], (array)$key),
|
|
|
370 |
'key' => $key,
|
|
|
371 |
'item' => array_keys($context['item']),
|
|
|
372 |
);
|
|
|
373 |
} elseif (is_array($context['item'])
|
|
|
374 |
&& array_key_exists($token, $context['item'])
|
|
|
375 |
&& !(strval($key) === strval($token) && count($tokens) === 1 && $tokens[0] === '.')) {
|
|
|
376 |
$items = $context['item'][$token];
|
|
|
377 |
if (!is_array($items)) {
|
|
|
378 |
$items = array($items);
|
|
|
379 |
} elseif (!isset($items[0])) {
|
|
|
380 |
$current = current($items);
|
|
|
381 |
$currentKey = key($items);
|
|
|
382 |
if (!is_array($current) || (is_array($current) && count($items) <= 1 && !is_numeric($currentKey))) {
|
|
|
383 |
$items = array($items);
|
|
|
384 |
}
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
foreach ($items as $key => $item) {
|
|
|
388 |
$ctext = array($context['key']);
|
|
|
389 |
if (!is_numeric($key)) {
|
|
|
390 |
$ctext[] = $token;
|
|
|
391 |
$tok = array_shift($tokens);
|
|
|
392 |
if (isset($items[$tok])) {
|
|
|
393 |
$ctext[] = $tok;
|
|
|
394 |
$item = $items[$tok];
|
|
|
395 |
$matches[] = array(
|
|
|
396 |
'trace' => array_merge($context['trace'], $ctext),
|
|
|
397 |
'key' => $tok,
|
|
|
398 |
'item' => $item,
|
|
|
399 |
);
|
|
|
400 |
break;
|
|
|
401 |
} elseif ($tok !== null) {
|
|
|
402 |
array_unshift($tokens, $tok);
|
|
|
403 |
}
|
|
|
404 |
} else {
|
|
|
405 |
$key = $token;
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
$matches[] = array(
|
|
|
409 |
'trace' => array_merge($context['trace'], $ctext),
|
|
|
410 |
'key' => $key,
|
|
|
411 |
'item' => $item,
|
|
|
412 |
);
|
|
|
413 |
}
|
|
|
414 |
} elseif ($key === $token || (ctype_digit($token) && $key == $token) || $token === '.') {
|
|
|
415 |
$context['trace'][] = $key;
|
|
|
416 |
$matches[] = array(
|
|
|
417 |
'trace' => $context['trace'],
|
|
|
418 |
'key' => $key,
|
|
|
419 |
'item' => $context['item'],
|
|
|
420 |
);
|
|
|
421 |
}
|
|
|
422 |
}
|
|
|
423 |
if ($conditions) {
|
|
|
424 |
foreach ($conditions as $condition) {
|
|
|
425 |
$filtered = array();
|
|
|
426 |
$length = count($matches);
|
|
|
427 |
foreach ($matches as $i => $match) {
|
|
|
428 |
if (Set::matches(array($condition), $match['item'], $i + 1, $length)) {
|
|
|
429 |
$filtered[$i] = $match;
|
|
|
430 |
}
|
|
|
431 |
}
|
|
|
432 |
$matches = $filtered;
|
|
|
433 |
}
|
|
|
434 |
}
|
|
|
435 |
$contexts = $matches;
|
|
|
436 |
|
|
|
437 |
if (empty($tokens)) {
|
|
|
438 |
break;
|
|
|
439 |
}
|
|
|
440 |
} while (1);
|
|
|
441 |
|
|
|
442 |
$r = array();
|
|
|
443 |
|
|
|
444 |
foreach ($matches as $match) {
|
|
|
445 |
if ((!$options['flatten'] || is_array($match['item'])) && !is_int($match['key'])) {
|
|
|
446 |
$r[] = array($match['key'] => $match['item']);
|
|
|
447 |
} else {
|
|
|
448 |
$r[] = $match['item'];
|
|
|
449 |
}
|
|
|
450 |
}
|
|
|
451 |
return $r;
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
/**
|
|
|
455 |
* This function can be used to see if a single item or a given xpath match certain conditions.
|
|
|
456 |
*
|
|
|
457 |
* @param string|array $conditions An array of condition strings or an XPath expression
|
|
|
458 |
* @param array $data An array of data to execute the match on
|
|
|
459 |
* @param integer $i Optional: The 'nth'-number of the item being matched.
|
|
|
460 |
* @param integer $length
|
|
|
461 |
* @return boolean
|
|
|
462 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::matches
|
|
|
463 |
*/
|
|
|
464 |
public static function matches($conditions, $data = array(), $i = null, $length = null) {
|
|
|
465 |
if (empty($conditions)) {
|
|
|
466 |
return true;
|
|
|
467 |
}
|
|
|
468 |
if (is_string($conditions)) {
|
|
|
469 |
return (bool)Set::extract($conditions, $data);
|
|
|
470 |
}
|
|
|
471 |
foreach ($conditions as $condition) {
|
|
|
472 |
if ($condition === ':last') {
|
|
|
473 |
if ($i != $length) {
|
|
|
474 |
return false;
|
|
|
475 |
}
|
|
|
476 |
continue;
|
|
|
477 |
} elseif ($condition === ':first') {
|
|
|
478 |
if ($i != 1) {
|
|
|
479 |
return false;
|
|
|
480 |
}
|
|
|
481 |
continue;
|
|
|
482 |
}
|
|
|
483 |
if (!preg_match('/(.+?)([><!]?[=]|[><])(.*)/', $condition, $match)) {
|
|
|
484 |
if (ctype_digit($condition)) {
|
|
|
485 |
if ($i != $condition) {
|
|
|
486 |
return false;
|
|
|
487 |
}
|
|
|
488 |
} elseif (preg_match_all('/(?:^[0-9]+|(?<=,)[0-9]+)/', $condition, $matches)) {
|
|
|
489 |
return in_array($i, $matches[0]);
|
|
|
490 |
} elseif (!array_key_exists($condition, $data)) {
|
|
|
491 |
return false;
|
|
|
492 |
}
|
|
|
493 |
continue;
|
|
|
494 |
}
|
|
|
495 |
list(, $key, $op, $expected) = $match;
|
|
|
496 |
if (!(isset($data[$key]) || array_key_exists($key, $data))) {
|
|
|
497 |
return false;
|
|
|
498 |
}
|
|
|
499 |
|
|
|
500 |
$val = $data[$key];
|
|
|
501 |
|
|
|
502 |
if ($op === '=' && $expected && $expected{0} === '/') {
|
|
|
503 |
return preg_match($expected, $val);
|
|
|
504 |
}
|
|
|
505 |
if ($op === '=' && $val != $expected) {
|
|
|
506 |
return false;
|
|
|
507 |
}
|
|
|
508 |
if ($op === '!=' && $val == $expected) {
|
|
|
509 |
return false;
|
|
|
510 |
}
|
|
|
511 |
if ($op === '>' && $val <= $expected) {
|
|
|
512 |
return false;
|
|
|
513 |
}
|
|
|
514 |
if ($op === '<' && $val >= $expected) {
|
|
|
515 |
return false;
|
|
|
516 |
}
|
|
|
517 |
if ($op === '<=' && $val > $expected) {
|
|
|
518 |
return false;
|
|
|
519 |
}
|
|
|
520 |
if ($op === '>=' && $val < $expected) {
|
|
|
521 |
return false;
|
|
|
522 |
}
|
|
|
523 |
}
|
|
|
524 |
return true;
|
|
|
525 |
}
|
|
|
526 |
|
|
|
527 |
/**
|
|
|
528 |
* Gets a value from an array or object that is contained in a given path using an array path syntax, i.e.:
|
|
|
529 |
* "{n}.Person.{[a-z]+}" - Where "{n}" represents a numeric key, "Person" represents a string literal,
|
|
|
530 |
* and "{[a-z]+}" (i.e. any string literal enclosed in brackets besides {n} and {s}) is interpreted as
|
|
|
531 |
* a regular expression.
|
|
|
532 |
*
|
|
|
533 |
* @param array $data Array from where to extract
|
|
|
534 |
* @param string|array $path As an array, or as a dot-separated string.
|
|
|
535 |
* @return array|null Extracted data or null when $data or $path are empty.
|
|
|
536 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::classicExtract
|
|
|
537 |
*/
|
|
|
538 |
public static function classicExtract($data, $path = null) {
|
|
|
539 |
if (empty($path)) {
|
|
|
540 |
return $data;
|
|
|
541 |
}
|
|
|
542 |
if (is_object($data)) {
|
|
|
543 |
if (!($data instanceof ArrayAccess || $data instanceof Traversable)) {
|
|
|
544 |
$data = get_object_vars($data);
|
|
|
545 |
}
|
|
|
546 |
}
|
|
|
547 |
if (empty($data)) {
|
|
|
548 |
return null;
|
|
|
549 |
}
|
|
|
550 |
if (is_string($path) && strpos($path, '{') !== false) {
|
|
|
551 |
$path = String::tokenize($path, '.', '{', '}');
|
|
|
552 |
} elseif (is_string($path)) {
|
|
|
553 |
$path = explode('.', $path);
|
|
|
554 |
}
|
|
|
555 |
$tmp = array();
|
|
|
556 |
|
|
|
557 |
if (empty($path)) {
|
|
|
558 |
return null;
|
|
|
559 |
}
|
|
|
560 |
|
|
|
561 |
foreach ($path as $i => $key) {
|
|
|
562 |
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
|
|
|
563 |
if (isset($data[$key])) {
|
|
|
564 |
$data = $data[$key];
|
|
|
565 |
} else {
|
|
|
566 |
return null;
|
|
|
567 |
}
|
|
|
568 |
} elseif ($key === '{n}') {
|
|
|
569 |
foreach ($data as $j => $val) {
|
|
|
570 |
if (is_int($j)) {
|
|
|
571 |
$tmpPath = array_slice($path, $i + 1);
|
|
|
572 |
if (empty($tmpPath)) {
|
|
|
573 |
$tmp[] = $val;
|
|
|
574 |
} else {
|
|
|
575 |
$tmp[] = Set::classicExtract($val, $tmpPath);
|
|
|
576 |
}
|
|
|
577 |
}
|
|
|
578 |
}
|
|
|
579 |
return $tmp;
|
|
|
580 |
} elseif ($key === '{s}') {
|
|
|
581 |
foreach ($data as $j => $val) {
|
|
|
582 |
if (is_string($j)) {
|
|
|
583 |
$tmpPath = array_slice($path, $i + 1);
|
|
|
584 |
if (empty($tmpPath)) {
|
|
|
585 |
$tmp[] = $val;
|
|
|
586 |
} else {
|
|
|
587 |
$tmp[] = Set::classicExtract($val, $tmpPath);
|
|
|
588 |
}
|
|
|
589 |
}
|
|
|
590 |
}
|
|
|
591 |
return $tmp;
|
|
|
592 |
} elseif (strpos($key, '{') !== false && strpos($key, '}') !== false) {
|
|
|
593 |
$pattern = substr($key, 1, -1);
|
|
|
594 |
|
|
|
595 |
foreach ($data as $j => $val) {
|
|
|
596 |
if (preg_match('/^' . $pattern . '/s', $j) !== 0) {
|
|
|
597 |
$tmpPath = array_slice($path, $i + 1);
|
|
|
598 |
if (empty($tmpPath)) {
|
|
|
599 |
$tmp[$j] = $val;
|
|
|
600 |
} else {
|
|
|
601 |
$tmp[$j] = Set::classicExtract($val, $tmpPath);
|
|
|
602 |
}
|
|
|
603 |
}
|
|
|
604 |
}
|
|
|
605 |
return $tmp;
|
|
|
606 |
} else {
|
|
|
607 |
if (isset($data[$key])) {
|
|
|
608 |
$data = $data[$key];
|
|
|
609 |
} else {
|
|
|
610 |
return null;
|
|
|
611 |
}
|
|
|
612 |
}
|
|
|
613 |
}
|
|
|
614 |
return $data;
|
|
|
615 |
}
|
|
|
616 |
|
|
|
617 |
/**
|
|
|
618 |
* Inserts $data into an array as defined by $path.
|
|
|
619 |
*
|
|
|
620 |
* @param array $list Where to insert into
|
|
|
621 |
* @param string $path A dot-separated string.
|
|
|
622 |
* @param array $data Data to insert
|
|
|
623 |
* @return array
|
|
|
624 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::insert
|
|
|
625 |
*/
|
|
|
626 |
public static function insert($list, $path, $data = null) {
|
|
|
627 |
return Hash::insert($list, $path, $data);
|
|
|
628 |
}
|
|
|
629 |
|
|
|
630 |
/**
|
|
|
631 |
* Removes an element from a Set or array as defined by $path.
|
|
|
632 |
*
|
|
|
633 |
* @param array $list From where to remove
|
|
|
634 |
* @param string $path A dot-separated string.
|
|
|
635 |
* @return array Array with $path removed from its value
|
|
|
636 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::remove
|
|
|
637 |
*/
|
|
|
638 |
public static function remove($list, $path = null) {
|
|
|
639 |
return Hash::remove($list, $path);
|
|
|
640 |
}
|
|
|
641 |
|
|
|
642 |
/**
|
|
|
643 |
* Checks if a particular path is set in an array
|
|
|
644 |
*
|
|
|
645 |
* @param string|array $data Data to check on
|
|
|
646 |
* @param string|array $path A dot-separated string.
|
|
|
647 |
* @return boolean true if path is found, false otherwise
|
|
|
648 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::check
|
|
|
649 |
*/
|
|
|
650 |
public static function check($data, $path = null) {
|
|
|
651 |
if (empty($path)) {
|
|
|
652 |
return $data;
|
|
|
653 |
}
|
|
|
654 |
if (!is_array($path)) {
|
|
|
655 |
$path = explode('.', $path);
|
|
|
656 |
}
|
|
|
657 |
|
|
|
658 |
foreach ($path as $i => $key) {
|
|
|
659 |
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
|
|
|
660 |
$key = intval($key);
|
|
|
661 |
}
|
|
|
662 |
if ($i === count($path) - 1) {
|
|
|
663 |
return (is_array($data) && array_key_exists($key, $data));
|
|
|
664 |
}
|
|
|
665 |
|
|
|
666 |
if (!is_array($data) || !array_key_exists($key, $data)) {
|
|
|
667 |
return false;
|
|
|
668 |
}
|
|
|
669 |
$data =& $data[$key];
|
|
|
670 |
}
|
|
|
671 |
return true;
|
|
|
672 |
}
|
|
|
673 |
|
|
|
674 |
/**
|
|
|
675 |
* Computes the difference between a Set and an array, two Sets, or two arrays
|
|
|
676 |
*
|
|
|
677 |
* @param mixed $val1 First value
|
|
|
678 |
* @param mixed $val2 Second value
|
|
|
679 |
* @return array Returns the key => value pairs that are not common in $val1 and $val2
|
|
|
680 |
* The expression for this function is($val1 - $val2) + ($val2 - ($val1 - $val2))
|
|
|
681 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::diff
|
|
|
682 |
*/
|
|
|
683 |
public static function diff($val1, $val2 = null) {
|
|
|
684 |
if (empty($val1)) {
|
|
|
685 |
return (array)$val2;
|
|
|
686 |
}
|
|
|
687 |
if (empty($val2)) {
|
|
|
688 |
return (array)$val1;
|
|
|
689 |
}
|
|
|
690 |
$intersection = array_intersect_key($val1, $val2);
|
|
|
691 |
while (($key = key($intersection)) !== null) {
|
|
|
692 |
if ($val1[$key] == $val2[$key]) {
|
|
|
693 |
unset($val1[$key]);
|
|
|
694 |
unset($val2[$key]);
|
|
|
695 |
}
|
|
|
696 |
next($intersection);
|
|
|
697 |
}
|
|
|
698 |
|
|
|
699 |
return $val1 + $val2;
|
|
|
700 |
}
|
|
|
701 |
|
|
|
702 |
/**
|
|
|
703 |
* Determines if one Set or array contains the exact keys and values of another.
|
|
|
704 |
*
|
|
|
705 |
* @param array $val1 First value
|
|
|
706 |
* @param array $val2 Second value
|
|
|
707 |
* @return boolean true if $val1 contains $val2, false otherwise
|
|
|
708 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::contains
|
|
|
709 |
*/
|
|
|
710 |
public static function contains($val1, $val2 = null) {
|
|
|
711 |
if (empty($val1) || empty($val2)) {
|
|
|
712 |
return false;
|
|
|
713 |
}
|
|
|
714 |
|
|
|
715 |
foreach ($val2 as $key => $val) {
|
|
|
716 |
if (is_numeric($key)) {
|
|
|
717 |
Set::contains($val, $val1);
|
|
|
718 |
} else {
|
|
|
719 |
if (!isset($val1[$key]) || $val1[$key] != $val) {
|
|
|
720 |
return false;
|
|
|
721 |
}
|
|
|
722 |
}
|
|
|
723 |
}
|
|
|
724 |
return true;
|
|
|
725 |
}
|
|
|
726 |
|
|
|
727 |
/**
|
|
|
728 |
* Counts the dimensions of an array. If $all is set to false (which is the default) it will
|
|
|
729 |
* only consider the dimension of the first element in the array.
|
|
|
730 |
*
|
|
|
731 |
* @param array $array Array to count dimensions on
|
|
|
732 |
* @param boolean $all Set to true to count the dimension considering all elements in array
|
|
|
733 |
* @param integer $count Start the dimension count at this number
|
|
|
734 |
* @return integer The number of dimensions in $array
|
|
|
735 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::countDim
|
|
|
736 |
*/
|
|
|
737 |
public static function countDim($array = null, $all = false, $count = 0) {
|
|
|
738 |
if ($all) {
|
|
|
739 |
$depth = array($count);
|
|
|
740 |
if (is_array($array) && reset($array) !== false) {
|
|
|
741 |
foreach ($array as $value) {
|
|
|
742 |
$depth[] = Set::countDim($value, true, $count + 1);
|
|
|
743 |
}
|
|
|
744 |
}
|
|
|
745 |
$return = max($depth);
|
|
|
746 |
} else {
|
|
|
747 |
if (is_array(reset($array))) {
|
|
|
748 |
$return = Set::countDim(reset($array)) + 1;
|
|
|
749 |
} else {
|
|
|
750 |
$return = 1;
|
|
|
751 |
}
|
|
|
752 |
}
|
|
|
753 |
return $return;
|
|
|
754 |
}
|
|
|
755 |
|
|
|
756 |
/**
|
|
|
757 |
* Normalizes a string or array list.
|
|
|
758 |
*
|
|
|
759 |
* @param mixed $list List to normalize
|
|
|
760 |
* @param boolean $assoc If true, $list will be converted to an associative array
|
|
|
761 |
* @param string $sep If $list is a string, it will be split into an array with $sep
|
|
|
762 |
* @param boolean $trim If true, separated strings will be trimmed
|
|
|
763 |
* @return array
|
|
|
764 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::normalize
|
|
|
765 |
*/
|
|
|
766 |
public static function normalize($list, $assoc = true, $sep = ',', $trim = true) {
|
|
|
767 |
if (is_string($list)) {
|
|
|
768 |
$list = explode($sep, $list);
|
|
|
769 |
if ($trim) {
|
|
|
770 |
foreach ($list as $key => $value) {
|
|
|
771 |
$list[$key] = trim($value);
|
|
|
772 |
}
|
|
|
773 |
}
|
|
|
774 |
if ($assoc) {
|
|
|
775 |
return Hash::normalize($list);
|
|
|
776 |
}
|
|
|
777 |
} elseif (is_array($list)) {
|
|
|
778 |
$list = Hash::normalize($list, $assoc);
|
|
|
779 |
}
|
|
|
780 |
return $list;
|
|
|
781 |
}
|
|
|
782 |
|
|
|
783 |
/**
|
|
|
784 |
* Creates an associative array using a $path1 as the path to build its keys, and optionally
|
|
|
785 |
* $path2 as path to get the values. If $path2 is not specified, all values will be initialized
|
|
|
786 |
* to null (useful for Set::merge). You can optionally group the values by what is obtained when
|
|
|
787 |
* following the path specified in $groupPath.
|
|
|
788 |
*
|
|
|
789 |
* @param array|object $data Array or object from where to extract keys and values
|
|
|
790 |
* @param string|array $path1 As an array, or as a dot-separated string.
|
|
|
791 |
* @param string|array $path2 As an array, or as a dot-separated string.
|
|
|
792 |
* @param string $groupPath As an array, or as a dot-separated string.
|
|
|
793 |
* @return array Combined array
|
|
|
794 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::combine
|
|
|
795 |
*/
|
|
|
796 |
public static function combine($data, $path1 = null, $path2 = null, $groupPath = null) {
|
|
|
797 |
if (empty($data)) {
|
|
|
798 |
return array();
|
|
|
799 |
}
|
|
|
800 |
|
|
|
801 |
if (is_object($data)) {
|
|
|
802 |
if (!($data instanceof ArrayAccess || $data instanceof Traversable)) {
|
|
|
803 |
$data = get_object_vars($data);
|
|
|
804 |
}
|
|
|
805 |
}
|
|
|
806 |
|
|
|
807 |
if (is_array($path1)) {
|
|
|
808 |
$format = array_shift($path1);
|
|
|
809 |
$keys = Set::format($data, $format, $path1);
|
|
|
810 |
} else {
|
|
|
811 |
$keys = Set::extract($data, $path1);
|
|
|
812 |
}
|
|
|
813 |
if (empty($keys)) {
|
|
|
814 |
return array();
|
|
|
815 |
}
|
|
|
816 |
|
|
|
817 |
if (!empty($path2) && is_array($path2)) {
|
|
|
818 |
$format = array_shift($path2);
|
|
|
819 |
$vals = Set::format($data, $format, $path2);
|
|
|
820 |
} elseif (!empty($path2)) {
|
|
|
821 |
$vals = Set::extract($data, $path2);
|
|
|
822 |
} else {
|
|
|
823 |
$count = count($keys);
|
|
|
824 |
for ($i = 0; $i < $count; $i++) {
|
|
|
825 |
$vals[$i] = null;
|
|
|
826 |
}
|
|
|
827 |
}
|
|
|
828 |
|
|
|
829 |
if ($groupPath) {
|
|
|
830 |
$group = Set::extract($data, $groupPath);
|
|
|
831 |
if (!empty($group)) {
|
|
|
832 |
$c = count($keys);
|
|
|
833 |
for ($i = 0; $i < $c; $i++) {
|
|
|
834 |
if (!isset($group[$i])) {
|
|
|
835 |
$group[$i] = 0;
|
|
|
836 |
}
|
|
|
837 |
if (!isset($out[$group[$i]])) {
|
|
|
838 |
$out[$group[$i]] = array();
|
|
|
839 |
}
|
|
|
840 |
$out[$group[$i]][$keys[$i]] = $vals[$i];
|
|
|
841 |
}
|
|
|
842 |
return $out;
|
|
|
843 |
}
|
|
|
844 |
}
|
|
|
845 |
if (empty($vals)) {
|
|
|
846 |
return array();
|
|
|
847 |
}
|
|
|
848 |
return array_combine($keys, $vals);
|
|
|
849 |
}
|
|
|
850 |
|
|
|
851 |
/**
|
|
|
852 |
* Converts an object into an array.
|
|
|
853 |
* @param object $object Object to reverse
|
|
|
854 |
* @return array Array representation of given object
|
|
|
855 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::reverse
|
|
|
856 |
*/
|
|
|
857 |
public static function reverse($object) {
|
|
|
858 |
$out = array();
|
|
|
859 |
if ($object instanceof SimpleXMLElement) {
|
|
|
860 |
return Xml::toArray($object);
|
|
|
861 |
} elseif (is_object($object)) {
|
|
|
862 |
$keys = get_object_vars($object);
|
|
|
863 |
if (isset($keys['_name_'])) {
|
|
|
864 |
$identity = $keys['_name_'];
|
|
|
865 |
unset($keys['_name_']);
|
|
|
866 |
}
|
|
|
867 |
$new = array();
|
|
|
868 |
foreach ($keys as $key => $value) {
|
|
|
869 |
if (is_array($value)) {
|
|
|
870 |
$new[$key] = (array)Set::reverse($value);
|
|
|
871 |
} else {
|
|
|
872 |
// @codingStandardsIgnoreStart Legacy junk
|
|
|
873 |
if (isset($value->_name_)) {
|
|
|
874 |
$new = array_merge($new, Set::reverse($value));
|
|
|
875 |
} else {
|
|
|
876 |
$new[$key] = Set::reverse($value);
|
|
|
877 |
}
|
|
|
878 |
// @codingStandardsIgnoreEnd
|
|
|
879 |
}
|
|
|
880 |
}
|
|
|
881 |
if (isset($identity)) {
|
|
|
882 |
$out[$identity] = $new;
|
|
|
883 |
} else {
|
|
|
884 |
$out = $new;
|
|
|
885 |
}
|
|
|
886 |
} elseif (is_array($object)) {
|
|
|
887 |
foreach ($object as $key => $value) {
|
|
|
888 |
$out[$key] = Set::reverse($value);
|
|
|
889 |
}
|
|
|
890 |
} else {
|
|
|
891 |
$out = $object;
|
|
|
892 |
}
|
|
|
893 |
return $out;
|
|
|
894 |
}
|
|
|
895 |
|
|
|
896 |
/**
|
|
|
897 |
* Collapses a multi-dimensional array into a single dimension, using a delimited array path for
|
|
|
898 |
* each array element's key, i.e. array(array('Foo' => array('Bar' => 'Far'))) becomes
|
|
|
899 |
* array('0.Foo.Bar' => 'Far').
|
|
|
900 |
*
|
|
|
901 |
* @param array $data Array to flatten
|
|
|
902 |
* @param string $separator String used to separate array key elements in a path, defaults to '.'
|
|
|
903 |
* @return array
|
|
|
904 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::flatten
|
|
|
905 |
*/
|
|
|
906 |
public static function flatten($data, $separator = '.') {
|
|
|
907 |
return Hash::flatten($data, $separator);
|
|
|
908 |
}
|
|
|
909 |
|
|
|
910 |
/**
|
|
|
911 |
* Expand/unflattens an string to an array
|
|
|
912 |
*
|
|
|
913 |
* For example, unflattens an array that was collapsed with `Set::flatten()`
|
|
|
914 |
* into a multi-dimensional array. So, `array('0.Foo.Bar' => 'Far')` becomes
|
|
|
915 |
* `array(array('Foo' => array('Bar' => 'Far')))`.
|
|
|
916 |
*
|
|
|
917 |
* @param array $data Flattened array
|
|
|
918 |
* @param string $separator The delimiter used
|
|
|
919 |
* @return array
|
|
|
920 |
*/
|
|
|
921 |
public static function expand($data, $separator = '.') {
|
|
|
922 |
return Hash::expand($data, $separator);
|
|
|
923 |
}
|
|
|
924 |
|
|
|
925 |
/**
|
|
|
926 |
* Flattens an array for sorting
|
|
|
927 |
*
|
|
|
928 |
* @param array $results
|
|
|
929 |
* @param string $key
|
|
|
930 |
* @return array
|
|
|
931 |
*/
|
|
|
932 |
protected static function _flatten($results, $key = null) {
|
|
|
933 |
$stack = array();
|
|
|
934 |
foreach ($results as $k => $r) {
|
|
|
935 |
$id = $k;
|
|
|
936 |
if ($key !== null) {
|
|
|
937 |
$id = $key;
|
|
|
938 |
}
|
|
|
939 |
if (is_array($r) && !empty($r)) {
|
|
|
940 |
$stack = array_merge($stack, Set::_flatten($r, $id));
|
|
|
941 |
} else {
|
|
|
942 |
$stack[] = array('id' => $id, 'value' => $r);
|
|
|
943 |
}
|
|
|
944 |
}
|
|
|
945 |
return $stack;
|
|
|
946 |
}
|
|
|
947 |
|
|
|
948 |
/**
|
|
|
949 |
* Sorts an array by any value, determined by a Set-compatible path
|
|
|
950 |
*
|
|
|
951 |
* @param array $data An array of data to sort
|
|
|
952 |
* @param string $path A Set-compatible path to the array value
|
|
|
953 |
* @param string $dir Direction of sorting - either ascending (ASC), or descending (DESC)
|
|
|
954 |
* @return array Sorted array of data
|
|
|
955 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::sort
|
|
|
956 |
*/
|
|
|
957 |
public static function sort($data, $path, $dir) {
|
|
|
958 |
if (empty($data)) {
|
|
|
959 |
return $data;
|
|
|
960 |
}
|
|
|
961 |
$originalKeys = array_keys($data);
|
|
|
962 |
$numeric = false;
|
|
|
963 |
if (is_numeric(implode('', $originalKeys))) {
|
|
|
964 |
$data = array_values($data);
|
|
|
965 |
$numeric = true;
|
|
|
966 |
}
|
|
|
967 |
$result = Set::_flatten(Set::extract($data, $path));
|
|
|
968 |
list($keys, $values) = array(Set::extract($result, '{n}.id'), Set::extract($result, '{n}.value'));
|
|
|
969 |
|
|
|
970 |
$dir = strtolower($dir);
|
|
|
971 |
if ($dir === 'asc') {
|
|
|
972 |
$dir = SORT_ASC;
|
|
|
973 |
} elseif ($dir === 'desc') {
|
|
|
974 |
$dir = SORT_DESC;
|
|
|
975 |
}
|
|
|
976 |
array_multisort($values, $dir, $keys, $dir);
|
|
|
977 |
$sorted = array();
|
|
|
978 |
$keys = array_unique($keys);
|
|
|
979 |
|
|
|
980 |
foreach ($keys as $k) {
|
|
|
981 |
if ($numeric) {
|
|
|
982 |
$sorted[] = $data[$k];
|
|
|
983 |
} else {
|
|
|
984 |
if (isset($originalKeys[$k])) {
|
|
|
985 |
$sorted[$originalKeys[$k]] = $data[$originalKeys[$k]];
|
|
|
986 |
} else {
|
|
|
987 |
$sorted[$k] = $data[$k];
|
|
|
988 |
}
|
|
|
989 |
}
|
|
|
990 |
}
|
|
|
991 |
return $sorted;
|
|
|
992 |
}
|
|
|
993 |
|
|
|
994 |
/**
|
|
|
995 |
* Allows the application of a callback method to elements of an
|
|
|
996 |
* array extracted by a Set::extract() compatible path.
|
|
|
997 |
*
|
|
|
998 |
* @param mixed $path Set-compatible path to the array value
|
|
|
999 |
* @param array $data An array of data to extract from & then process with the $callback.
|
|
|
1000 |
* @param mixed $callback Callback method to be applied to extracted data.
|
|
|
1001 |
* See http://ca2.php.net/manual/en/language.pseudo-types.php#language.types.callback for examples
|
|
|
1002 |
* of callback formats.
|
|
|
1003 |
* @param array $options Options are:
|
|
|
1004 |
* - type : can be pass, map, or reduce. Map will handoff the given callback
|
|
|
1005 |
* to array_map, reduce will handoff to array_reduce, and pass will
|
|
|
1006 |
* use call_user_func_array().
|
|
|
1007 |
* @return mixed Result of the callback when applied to extracted data
|
|
|
1008 |
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::apply
|
|
|
1009 |
*/
|
|
|
1010 |
public static function apply($path, $data, $callback, $options = array()) {
|
|
|
1011 |
$defaults = array('type' => 'pass');
|
|
|
1012 |
$options = array_merge($defaults, $options);
|
|
|
1013 |
$extracted = Set::extract($path, $data);
|
|
|
1014 |
|
|
|
1015 |
if ($options['type'] === 'map') {
|
|
|
1016 |
return array_map($callback, $extracted);
|
|
|
1017 |
} elseif ($options['type'] === 'reduce') {
|
|
|
1018 |
return array_reduce($extracted, $callback);
|
|
|
1019 |
} elseif ($options['type'] === 'pass') {
|
|
|
1020 |
return call_user_func_array($callback, array($extracted));
|
|
|
1021 |
}
|
|
|
1022 |
return null;
|
|
|
1023 |
}
|
|
|
1024 |
|
|
|
1025 |
/**
|
|
|
1026 |
* Takes in a flat array and returns a nested array
|
|
|
1027 |
*
|
|
|
1028 |
* @param mixed $data
|
|
|
1029 |
* @param array $options Options are:
|
|
|
1030 |
* children - the key name to use in the resultset for children
|
|
|
1031 |
* idPath - the path to a key that identifies each entry
|
|
|
1032 |
* parentPath - the path to a key that identifies the parent of each entry
|
|
|
1033 |
* root - the id of the desired top-most result
|
|
|
1034 |
* @return array of results, nested
|
|
|
1035 |
* @link
|
|
|
1036 |
*/
|
|
|
1037 |
public static function nest($data, $options = array()) {
|
|
|
1038 |
if (!$data) {
|
|
|
1039 |
return $data;
|
|
|
1040 |
}
|
|
|
1041 |
|
|
|
1042 |
$alias = key(current($data));
|
|
|
1043 |
$options += array(
|
|
|
1044 |
'idPath' => "/$alias/id",
|
|
|
1045 |
'parentPath' => "/$alias/parent_id",
|
|
|
1046 |
'children' => 'children',
|
|
|
1047 |
'root' => null
|
|
|
1048 |
);
|
|
|
1049 |
|
|
|
1050 |
$return = $idMap = array();
|
|
|
1051 |
$ids = Set::extract($data, $options['idPath']);
|
|
|
1052 |
$idKeys = explode('/', trim($options['idPath'], '/'));
|
|
|
1053 |
$parentKeys = explode('/', trim($options['parentPath'], '/'));
|
|
|
1054 |
|
|
|
1055 |
foreach ($data as $result) {
|
|
|
1056 |
$result[$options['children']] = array();
|
|
|
1057 |
|
|
|
1058 |
$id = Set::get($result, $idKeys);
|
|
|
1059 |
$parentId = Set::get($result, $parentKeys);
|
|
|
1060 |
|
|
|
1061 |
if (isset($idMap[$id][$options['children']])) {
|
|
|
1062 |
$idMap[$id] = array_merge($result, (array)$idMap[$id]);
|
|
|
1063 |
} else {
|
|
|
1064 |
$idMap[$id] = array_merge($result, array($options['children'] => array()));
|
|
|
1065 |
}
|
|
|
1066 |
if (!$parentId || !in_array($parentId, $ids)) {
|
|
|
1067 |
$return[] =& $idMap[$id];
|
|
|
1068 |
} else {
|
|
|
1069 |
$idMap[$parentId][$options['children']][] =& $idMap[$id];
|
|
|
1070 |
}
|
|
|
1071 |
}
|
|
|
1072 |
|
|
|
1073 |
if ($options['root']) {
|
|
|
1074 |
$root = $options['root'];
|
|
|
1075 |
} else {
|
|
|
1076 |
$root = Set::get($return[0], $parentKeys);
|
|
|
1077 |
}
|
|
|
1078 |
|
|
|
1079 |
foreach ($return as $i => $result) {
|
|
|
1080 |
$id = Set::get($result, $idKeys);
|
|
|
1081 |
$parentId = Set::get($result, $parentKeys);
|
|
|
1082 |
if ($id !== $root && $parentId != $root) {
|
|
|
1083 |
unset($return[$i]);
|
|
|
1084 |
}
|
|
|
1085 |
}
|
|
|
1086 |
|
|
|
1087 |
return array_values($return);
|
|
|
1088 |
}
|
|
|
1089 |
|
|
|
1090 |
/**
|
|
|
1091 |
* Return the value at the specified position
|
|
|
1092 |
*
|
|
|
1093 |
* @param array $input an array
|
|
|
1094 |
* @param string|array $path string or array of array keys
|
|
|
1095 |
* @return the value at the specified position or null if it doesn't exist
|
|
|
1096 |
*/
|
|
|
1097 |
public static function get($input, $path = null) {
|
|
|
1098 |
if (is_string($path)) {
|
|
|
1099 |
if (strpos($path, '/') !== false) {
|
|
|
1100 |
$keys = explode('/', trim($path, '/'));
|
|
|
1101 |
} else {
|
|
|
1102 |
$keys = explode('.', trim($path, '.'));
|
|
|
1103 |
}
|
|
|
1104 |
} else {
|
|
|
1105 |
$keys = $path;
|
|
|
1106 |
}
|
|
|
1107 |
return Hash::get($input, $keys);
|
|
|
1108 |
}
|
|
|
1109 |
|
|
|
1110 |
}
|