2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Library of array functions for Cake.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-04 07:27:20 +00:00
|
|
|
App::uses('String', 'Utility');
|
2012-03-21 02:01:57 +00:00
|
|
|
App::uses('Hash', 'Utility');
|
2010-12-03 23:07:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Class used for manipulation of arrays.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Will be removed in 3.0. Use Hash instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-09-30 04:01:47 +00:00
|
|
|
class Set {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* This function can be thought of as a hybrid between PHP's array_merge and array_merge_recursive. The difference
|
|
|
|
* to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge)
|
2010-01-25 20:37:55 +00:00
|
|
|
* but does not do if for keys containing strings (unlike array_merge_recursive).
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* Since this method emulates `array_merge`, it will re-order numeric keys. When combined with out of
|
2012-02-18 21:01:07 +00:00
|
|
|
* order numeric keys containing arrays, results can be lossy.
|
|
|
|
*
|
2012-03-12 13:02:29 +00:00
|
|
|
* Note: This function will work with an unlimited amount of arguments and typecasts non-array
|
2012-02-18 21:01:07 +00:00
|
|
|
* parameters into arrays.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-11 02:16:14 +00:00
|
|
|
* @param array $data Array to be merged
|
|
|
|
* @param array $merge Array to merge with
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Merged array
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::merge
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-04-11 02:16:14 +00:00
|
|
|
public static function merge($data, $merge = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
2012-11-19 19:03:03 +00:00
|
|
|
if (empty($args[1]) && count($args) <= 2) {
|
2012-03-31 17:38:09 +00:00
|
|
|
return (array)$args[0];
|
|
|
|
}
|
|
|
|
if (!is_array($args[0])) {
|
|
|
|
$args[0] = (array)$args[0];
|
|
|
|
}
|
2012-03-21 02:01:57 +00:00
|
|
|
return call_user_func_array('Hash::merge', $args);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Filters empty elements out of a route array, excluding '0'.
|
|
|
|
*
|
2011-07-29 03:24:31 +00:00
|
|
|
* @param array $var Either an array to filter, or value when in callback
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed Either filtered array, or true/false when in callback
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::filter
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-10 03:34:36 +00:00
|
|
|
public static function filter(array $var) {
|
2012-03-21 02:01:57 +00:00
|
|
|
return Hash::filter($var);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Pushes the differences in $array2 onto the end of $array
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $array Original array
|
|
|
|
* @param array $array2 Differences to push
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Combined array
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::pushDiff
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function pushDiff($array, $array2) {
|
2008-08-24 22:08:13 +00:00
|
|
|
if (empty($array) && !empty($array2)) {
|
|
|
|
return $array2;
|
|
|
|
}
|
|
|
|
if (!empty($array) && !empty($array2)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($array2 as $key => $value) {
|
|
|
|
if (!array_key_exists($key, $array)) {
|
|
|
|
$array[$key] = $value;
|
|
|
|
} else {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$array[$key] = Set::pushDiff($array[$key], $array2[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-31 23:50:32 +00:00
|
|
|
return $array;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Maps the contents of the Set object to an object hierarchy.
|
|
|
|
* Maintains numeric keys as arrays of objects
|
|
|
|
*
|
|
|
|
* @param string $class A class name of the type of object to map to
|
|
|
|
* @param string $tmp A temporary class name used as $class if $class is an array
|
|
|
|
* @return object Hierarchical object
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::map
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function map($class = 'stdClass', $tmp = 'stdClass') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($class)) {
|
|
|
|
$val = $class;
|
|
|
|
$class = $tmp;
|
|
|
|
}
|
|
|
|
|
2008-09-14 17:17:49 +00:00
|
|
|
if (empty($val)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2011-08-20 05:39:30 +00:00
|
|
|
return Set::_map($val, $class);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps the given value as an object. If $value is an object,
|
|
|
|
* it returns $value. Otherwise it maps $value as an object of
|
|
|
|
* type $class, and if primary assign _name_ $key on first array.
|
|
|
|
* If $value is not empty, it will be used to set properties of
|
|
|
|
* returned object (recursively). If $key is numeric will maintain array
|
|
|
|
* structure
|
|
|
|
*
|
2014-06-04 18:58:55 +00:00
|
|
|
* @param array &$array Array to map
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $class Class name
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $primary whether to assign first array key as the _name_
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed Mapped object
|
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected static function _map(&$array, $class, $primary = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($class === true) {
|
|
|
|
$out = new stdClass;
|
|
|
|
} else {
|
|
|
|
$out = new $class;
|
|
|
|
}
|
|
|
|
if (is_array($array)) {
|
|
|
|
$keys = array_keys($array);
|
|
|
|
foreach ($array as $key => $value) {
|
2008-07-31 23:50:32 +00:00
|
|
|
if ($keys[0] === $key && $class !== true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$primary = true;
|
|
|
|
}
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
if (is_object($out)) {
|
|
|
|
$out = get_object_vars($out);
|
|
|
|
}
|
2011-08-20 05:39:30 +00:00
|
|
|
$out[$key] = Set::_map($value, $class);
|
2008-08-28 18:16:40 +00:00
|
|
|
if (is_object($out[$key])) {
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($primary !== true && is_array($value) && Set::countDim($value, true) === 2) {
|
2008-10-23 00:10:44 +00:00
|
|
|
if (!isset($out[$key]->_name_)) {
|
2008-09-26 21:11:51 +00:00
|
|
|
$out[$key]->_name_ = $primary;
|
|
|
|
}
|
2008-08-28 18:16:40 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-28 18:16:40 +00:00
|
|
|
} elseif (is_array($value)) {
|
|
|
|
if ($primary === true) {
|
2012-03-05 03:49:38 +00:00
|
|
|
// @codingStandardsIgnoreStart Legacy junk
|
2008-10-23 00:10:44 +00:00
|
|
|
if (!isset($out->_name_)) {
|
2008-09-26 21:11:51 +00:00
|
|
|
$out->_name_ = $key;
|
|
|
|
}
|
2012-03-05 03:49:38 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2008-08-28 18:16:40 +00:00
|
|
|
$primary = false;
|
2008-10-23 00:10:44 +00:00
|
|
|
foreach ($value as $key2 => $value2) {
|
2011-08-20 05:39:30 +00:00
|
|
|
$out->{$key2} = Set::_map($value2, true);
|
2008-08-28 18:16:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!is_numeric($key)) {
|
2011-08-20 05:39:30 +00:00
|
|
|
$out->{$key} = Set::_map($value, true, $key);
|
2008-08-28 18:16:40 +00:00
|
|
|
if (is_object($out->{$key}) && !is_numeric($key)) {
|
2008-10-23 00:10:44 +00:00
|
|
|
if (!isset($out->{$key}->_name_)) {
|
2008-09-26 21:11:51 +00:00
|
|
|
$out->{$key}->_name_ = $key;
|
|
|
|
}
|
2008-08-28 18:16:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-08-20 05:39:30 +00:00
|
|
|
$out->{$key} = Set::_map($value, true);
|
2008-08-28 18:16:40 +00:00
|
|
|
}
|
2008-08-22 19:17:48 +00:00
|
|
|
}
|
2008-08-28 18:16:40 +00:00
|
|
|
} else {
|
|
|
|
$out->{$key} = $value;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$out = $array;
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if all the values in the array are numeric
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param array $array The array to check. If null, the value of the current Set object
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if values are numeric, false otherwise
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::numeric
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function numeric($array = null) {
|
2012-03-21 02:01:57 +00:00
|
|
|
return Hash::numeric($array);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return a value from an array list if the key exists.
|
|
|
|
*
|
|
|
|
* If a comma separated $list is passed arrays are numeric with the key of the first being 0
|
|
|
|
* $list = 'no, yes' would translate to $list = array(0 => 'no', 1 => 'yes');
|
|
|
|
*
|
|
|
|
* If an array is used, keys can be strings example: array('no' => 0, 'yes' => 1);
|
|
|
|
*
|
|
|
|
* $list defaults to 0 = no 1 = yes if param is not passed
|
|
|
|
*
|
2012-11-22 13:28:09 +00:00
|
|
|
* @param string $select Key in $list to return
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array|string $list can be an array or a comma-separated list.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string the value of the array key or null if no match
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::enum
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function enum($select, $list = null) {
|
2008-07-31 23:50:32 +00:00
|
|
|
if (empty($list)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$list = array('no', 'yes');
|
|
|
|
}
|
|
|
|
|
|
|
|
$return = null;
|
|
|
|
$list = Set::normalize($list, false);
|
|
|
|
|
|
|
|
if (array_key_exists($select, $list)) {
|
|
|
|
$return = $list[$select];
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a series of values extracted from an array, formatted in a format string.
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* @param array $data Source array from which to extract the data
|
|
|
|
* @param string $format Format string into which values will be inserted, see sprintf()
|
|
|
|
* @param array $keys An array containing one or more Set::extract()-style key paths
|
|
|
|
* @return array An array of strings extracted from $keys and formatted with $format
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::format
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function format($data, $format, $keys) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$extracted = array();
|
|
|
|
$count = count($keys);
|
|
|
|
|
|
|
|
if (!$count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$extracted[] = Set::extract($data, $keys[$i]);
|
|
|
|
}
|
|
|
|
$out = array();
|
|
|
|
$data = $extracted;
|
|
|
|
$count = count($data[0]);
|
|
|
|
|
|
|
|
if (preg_match_all('/\{([0-9]+)\}/msi', $format, $keys2) && isset($keys2[1])) {
|
|
|
|
$keys = $keys2[1];
|
|
|
|
$format = preg_split('/\{([0-9]+)\}/msi', $format);
|
|
|
|
$count2 = count($format);
|
|
|
|
|
|
|
|
for ($j = 0; $j < $count; $j++) {
|
|
|
|
$formatted = '';
|
|
|
|
for ($i = 0; $i <= $count2; $i++) {
|
|
|
|
if (isset($format[$i])) {
|
|
|
|
$formatted .= $format[$i];
|
|
|
|
}
|
|
|
|
if (isset($keys[$i]) && isset($data[$keys[$i]][$j])) {
|
|
|
|
$formatted .= $data[$keys[$i]][$j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$out[] = $formatted;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$count2 = count($data);
|
|
|
|
for ($j = 0; $j < $count; $j++) {
|
|
|
|
$args = array();
|
|
|
|
for ($i = 0; $i < $count2; $i++) {
|
2011-10-07 14:11:50 +00:00
|
|
|
if (array_key_exists($j, $data[$i])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args[] = $data[$i][$j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$out[] = vsprintf($format, $args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-03-02 16:18:59 +00:00
|
|
|
* Implements partial support for XPath 2.0. If $path does not contain a '/' the call
|
2012-03-12 13:02:29 +00:00
|
|
|
* is delegated to Set::classicExtract(). Also the $path and $data arguments are
|
2012-03-02 16:18:59 +00:00
|
|
|
* reversible.
|
2009-10-06 01:27:34 +00:00
|
|
|
*
|
|
|
|
* #### Currently implemented selectors:
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* - /User/id (similar to the classic {n}.User.id)
|
|
|
|
* - /User[2]/name (selects the name of the second User)
|
|
|
|
* - /User[id>2] (selects all Users with an id > 2)
|
|
|
|
* - /User[id>2][<5] (selects all Users with an id > 2 but < 5)
|
|
|
|
* - /Post/Comment[author_name=john]/../name (Selects the name of all Posts that have at least one Comment written by john)
|
2008-09-15 18:05:26 +00:00
|
|
|
* - /Posts[name] (Selects all Posts that have a 'name' key)
|
2008-05-30 11:40:08 +00:00
|
|
|
* - /Comment/.[1] (Selects the contents of the first comment)
|
|
|
|
* - /Comment/.[:last] (Selects the last comment)
|
|
|
|
* - /Comment/.[:first] (Selects the first comment)
|
|
|
|
* - /Comment[text=/cakephp/i] (Selects the all comments that have a text matching the regex /cakephp/i)
|
|
|
|
* - /Comment/@* (Selects the all key names of all comments)
|
|
|
|
*
|
2009-10-06 01:27:34 +00:00
|
|
|
* #### Other limitations:
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* - Only absolute paths starting with a single '/' are supported right now
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* **Warning**: Even so it has plenty of unit tests the XPath support has not gone through a lot of
|
|
|
|
* real-world testing. Please report Bugs as you find them. Suggestions for additional features to
|
2009-10-06 01:27:34 +00:00
|
|
|
* implement are also very welcome!
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $path An absolute XPath 2.0 path
|
2009-09-25 18:31:05 +00:00
|
|
|
* @param array $data An array of data to extract from
|
|
|
|
* @param array $options Currently only supports 'flatten' which can be disabled for higher XPath-ness
|
2014-03-08 10:26:03 +00:00
|
|
|
* @return mixed An array of matched items or the content of a single selected item or null in any of these cases: $path or $data are null, no items found.
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::extract
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function extract($path, $data = null, $options = array()) {
|
2009-03-14 19:56:28 +00:00
|
|
|
if (is_string($data)) {
|
|
|
|
$tmp = $data;
|
|
|
|
$data = $path;
|
|
|
|
$path = $tmp;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-03-14 19:56:28 +00:00
|
|
|
if (strpos($path, '/') === false) {
|
|
|
|
return Set::classicExtract($data, $path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-03-15 19:39:35 +00:00
|
|
|
if (empty($data)) {
|
|
|
|
return array();
|
|
|
|
}
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($path === '/') {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
$contexts = $data;
|
2014-04-07 23:25:14 +00:00
|
|
|
$options += array('flatten' => true);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($contexts[0])) {
|
2009-08-22 03:18:27 +00:00
|
|
|
$current = current($data);
|
2010-10-06 11:19:54 +00:00
|
|
|
if ((is_array($current) && count($data) < 1) || !is_array($current) || !Set::numeric(array_keys($data))) {
|
2009-08-22 03:18:27 +00:00
|
|
|
$contexts = array($data);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-30 07:55:46 +00:00
|
|
|
$tokens = array_slice(preg_split('/(?<!=|\\\\)\/(?![a-z-\s]*\])/', $path), 1);
|
2008-11-26 22:33:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
do {
|
|
|
|
$token = array_shift($tokens);
|
|
|
|
$conditions = false;
|
2009-03-10 20:20:32 +00:00
|
|
|
if (preg_match_all('/\[([^=]+=\/[^\/]+\/|[^\]]+)\]/', $token, $m)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$conditions = $m[1];
|
|
|
|
$token = substr($token, 0, strpos($token, '['));
|
|
|
|
}
|
|
|
|
$matches = array();
|
|
|
|
foreach ($contexts as $key => $context) {
|
|
|
|
if (!isset($context['trace'])) {
|
2008-09-28 17:41:23 +00:00
|
|
|
$context = array('trace' => array(null), 'item' => $context, 'key' => $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($token === '..') {
|
2012-09-14 18:26:00 +00:00
|
|
|
if (count($context['trace']) === 1) {
|
2008-09-19 18:08:27 +00:00
|
|
|
$context['trace'][] = $context['key'];
|
|
|
|
}
|
2009-11-19 22:13:35 +00:00
|
|
|
$parent = implode('/', $context['trace']) . '/.';
|
2008-09-03 15:40:25 +00:00
|
|
|
$context['item'] = Set::extract($parent, $data);
|
2008-05-30 11:40:08 +00:00
|
|
|
$context['key'] = array_pop($context['trace']);
|
2008-11-26 22:33:21 +00:00
|
|
|
if (isset($context['trace'][1]) && $context['trace'][1] > 0) {
|
|
|
|
$context['item'] = $context['item'][0];
|
2009-07-26 10:46:07 +00:00
|
|
|
} elseif (!empty($context['item'][$key])) {
|
2008-11-26 22:33:21 +00:00
|
|
|
$context['item'] = $context['item'][$key];
|
2009-02-24 03:01:06 +00:00
|
|
|
} else {
|
|
|
|
$context['item'] = array_shift($context['item']);
|
2008-11-26 22:33:21 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$matches[] = $context;
|
|
|
|
continue;
|
|
|
|
}
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($token === '@*' && is_array($context['item'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$matches[] = array(
|
2008-09-15 18:05:31 +00:00
|
|
|
'trace' => array_merge($context['trace'], (array)$key),
|
2008-05-30 11:40:08 +00:00
|
|
|
'key' => $key,
|
|
|
|
'item' => array_keys($context['item']),
|
|
|
|
);
|
2011-08-09 22:26:45 +00:00
|
|
|
} elseif (is_array($context['item'])
|
|
|
|
&& array_key_exists($token, $context['item'])
|
2012-09-14 18:26:00 +00:00
|
|
|
&& !(strval($key) === strval($token) && count($tokens) === 1 && $tokens[0] === '.')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$items = $context['item'][$token];
|
2008-11-26 22:33:21 +00:00
|
|
|
if (!is_array($items)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$items = array($items);
|
2008-11-26 22:33:21 +00:00
|
|
|
} elseif (!isset($items[0])) {
|
|
|
|
$current = current($items);
|
2010-03-25 03:25:02 +00:00
|
|
|
$currentKey = key($items);
|
|
|
|
if (!is_array($current) || (is_array($current) && count($items) <= 1 && !is_numeric($currentKey))) {
|
2008-11-26 22:33:21 +00:00
|
|
|
$items = array($items);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-12-17 21:33:11 +00:00
|
|
|
|
|
|
|
foreach ($items as $key => $item) {
|
|
|
|
$ctext = array($context['key']);
|
|
|
|
if (!is_numeric($key)) {
|
|
|
|
$ctext[] = $token;
|
2010-04-10 20:57:50 +00:00
|
|
|
$tok = array_shift($tokens);
|
|
|
|
if (isset($items[$tok])) {
|
|
|
|
$ctext[] = $tok;
|
|
|
|
$item = $items[$tok];
|
2008-12-17 21:33:11 +00:00
|
|
|
$matches[] = array(
|
|
|
|
'trace' => array_merge($context['trace'], $ctext),
|
2010-04-10 20:57:50 +00:00
|
|
|
'key' => $tok,
|
2008-12-17 21:33:11 +00:00
|
|
|
'item' => $item,
|
|
|
|
);
|
|
|
|
break;
|
2010-04-10 20:57:50 +00:00
|
|
|
} elseif ($tok !== null) {
|
|
|
|
array_unshift($tokens, $tok);
|
2008-12-17 21:33:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$key = $token;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$matches[] = array(
|
2008-12-17 21:33:11 +00:00
|
|
|
'trace' => array_merge($context['trace'], $ctext),
|
|
|
|
'key' => $key,
|
2008-05-30 11:40:08 +00:00
|
|
|
'item' => $item,
|
|
|
|
);
|
|
|
|
}
|
2011-08-09 22:26:45 +00:00
|
|
|
} elseif ($key === $token || (ctype_digit($token) && $key == $token) || $token === '.') {
|
|
|
|
$context['trace'][] = $key;
|
|
|
|
$matches[] = array(
|
|
|
|
'trace' => $context['trace'],
|
|
|
|
'key' => $key,
|
|
|
|
'item' => $context['item'],
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($conditions) {
|
|
|
|
foreach ($conditions as $condition) {
|
|
|
|
$filtered = array();
|
|
|
|
$length = count($matches);
|
|
|
|
foreach ($matches as $i => $match) {
|
2008-12-17 04:13:45 +00:00
|
|
|
if (Set::matches(array($condition), $match['item'], $i + 1, $length)) {
|
2010-03-27 20:23:46 +00:00
|
|
|
$filtered[$i] = $match;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$matches = $filtered;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$contexts = $matches;
|
2008-11-26 22:33:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($tokens)) {
|
|
|
|
break;
|
|
|
|
}
|
2012-02-23 13:38:02 +00:00
|
|
|
} while (1);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$r = array();
|
2008-11-26 22:33:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($matches as $match) {
|
|
|
|
if ((!$options['flatten'] || is_array($match['item'])) && !is_int($match['key'])) {
|
|
|
|
$r[] = array($match['key'] => $match['item']);
|
|
|
|
} else {
|
|
|
|
$r[] = $match['item'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $r;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* This function can be used to see if a single item or a given xpath match certain conditions.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $conditions An array of condition strings or an XPath expression
|
2013-07-05 12:36:40 +00:00
|
|
|
* @param array $data An array of data to execute the match on
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $i Optional: The 'nth'-number of the item being matched.
|
|
|
|
* @param int $length Length.
|
|
|
|
* @return bool
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::matches
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function matches($conditions, $data = array(), $i = null, $length = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($conditions)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (is_string($conditions)) {
|
2012-11-22 13:28:09 +00:00
|
|
|
return (bool)Set::extract($conditions, $data);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
foreach ($conditions as $condition) {
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($condition === ':last') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($i != $length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
2008-09-14 17:17:49 +00:00
|
|
|
} elseif ($condition === ':first') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($i != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!preg_match('/(.+?)([><!]?[=]|[><])(.*)/', $condition, $match)) {
|
|
|
|
if (ctype_digit($condition)) {
|
2008-06-20 20:17:23 +00:00
|
|
|
if ($i != $condition) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif (preg_match_all('/(?:^[0-9]+|(?<=,)[0-9]+)/', $condition, $matches)) {
|
|
|
|
return in_array($i, $matches[0]);
|
|
|
|
} elseif (!array_key_exists($condition, $data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-16 06:52:07 +00:00
|
|
|
list(, $key, $op, $expected) = $match;
|
2012-05-31 01:19:38 +00:00
|
|
|
if (!(isset($data[$key]) || array_key_exists($key, $data))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-09-23 11:33:36 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$val = $data[$key];
|
2008-09-23 11:33:36 +00:00
|
|
|
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($op === '=' && $expected && $expected{0} === '/') {
|
2008-05-30 11:40:08 +00:00
|
|
|
return preg_match($expected, $val);
|
2008-09-23 11:33:36 +00:00
|
|
|
}
|
|
|
|
if ($op === '=' && $val != $expected) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
2008-09-23 11:33:36 +00:00
|
|
|
}
|
|
|
|
if ($op === '!=' && $val == $expected) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
2008-09-23 11:33:36 +00:00
|
|
|
}
|
|
|
|
if ($op === '>' && $val <= $expected) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
2008-09-23 11:33:36 +00:00
|
|
|
}
|
|
|
|
if ($op === '<' && $val >= $expected) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
2008-09-23 11:33:36 +00:00
|
|
|
}
|
|
|
|
if ($op === '<=' && $val > $expected) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
2008-09-23 11:33:36 +00:00
|
|
|
}
|
|
|
|
if ($op === '>=' && $val < $expected) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets a value from an array or object that is contained in a given path using an array path syntax, i.e.:
|
|
|
|
* "{n}.Person.{[a-z]+}" - Where "{n}" represents a numeric key, "Person" represents a string literal,
|
|
|
|
* and "{[a-z]+}" (i.e. any string literal enclosed in brackets besides {n} and {s}) is interpreted as
|
|
|
|
* a regular expression.
|
|
|
|
*
|
|
|
|
* @param array $data Array from where to extract
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $path As an array, or as a dot-separated string.
|
2014-03-08 10:26:03 +00:00
|
|
|
* @return mixed An array of matched items or the content of a single selected item or null in any of these cases: $path or $data are null, no items found.
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::classicExtract
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function classicExtract($data, $path = null) {
|
2008-07-31 23:50:32 +00:00
|
|
|
if (empty($path)) {
|
|
|
|
return $data;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (is_object($data)) {
|
2011-08-11 17:56:06 +00:00
|
|
|
if (!($data instanceof ArrayAccess || $data instanceof Traversable)) {
|
|
|
|
$data = get_object_vars($data);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-08-11 17:56:06 +00:00
|
|
|
if (empty($data)) {
|
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-12-06 14:08:06 +00:00
|
|
|
if (is_string($path) && strpos($path, '{') !== false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$path = String::tokenize($path, '.', '{', '}');
|
2010-12-09 03:45:18 +00:00
|
|
|
} elseif (is_string($path)) {
|
2010-12-06 14:08:06 +00:00
|
|
|
$path = explode('.', $path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$tmp = array();
|
|
|
|
|
2010-12-06 14:08:06 +00:00
|
|
|
if (empty($path)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($path as $i => $key) {
|
2008-09-14 17:17:49 +00:00
|
|
|
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
|
2012-05-23 02:33:46 +00:00
|
|
|
if (isset($data[$key])) {
|
|
|
|
$data = $data[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2008-09-14 17:17:49 +00:00
|
|
|
} elseif ($key === '{n}') {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($data as $j => $val) {
|
|
|
|
if (is_int($j)) {
|
|
|
|
$tmpPath = array_slice($path, $i + 1);
|
|
|
|
if (empty($tmpPath)) {
|
|
|
|
$tmp[] = $val;
|
|
|
|
} else {
|
|
|
|
$tmp[] = Set::classicExtract($val, $tmpPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tmp;
|
2008-09-14 17:17:49 +00:00
|
|
|
} elseif ($key === '{s}') {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($data as $j => $val) {
|
|
|
|
if (is_string($j)) {
|
|
|
|
$tmpPath = array_slice($path, $i + 1);
|
|
|
|
if (empty($tmpPath)) {
|
|
|
|
$tmp[] = $val;
|
|
|
|
} else {
|
|
|
|
$tmp[] = Set::classicExtract($val, $tmpPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tmp;
|
2013-09-06 16:36:57 +00:00
|
|
|
} elseif (strpos($key, '{') !== false && strpos($key, '}') !== false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$pattern = substr($key, 1, -1);
|
|
|
|
|
|
|
|
foreach ($data as $j => $val) {
|
2011-12-16 06:52:07 +00:00
|
|
|
if (preg_match('/^' . $pattern . '/s', $j) !== 0) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$tmpPath = array_slice($path, $i + 1);
|
|
|
|
if (empty($tmpPath)) {
|
|
|
|
$tmp[$j] = $val;
|
|
|
|
} else {
|
|
|
|
$tmp[$j] = Set::classicExtract($val, $tmpPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tmp;
|
|
|
|
} else {
|
|
|
|
if (isset($data[$key])) {
|
|
|
|
$data = $data[$key];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Inserts $data into an array as defined by $path.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $list Where to insert into
|
|
|
|
* @param string $path A dot-separated string.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $data Data to insert
|
|
|
|
* @return array
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::insert
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function insert($list, $path, $data = null) {
|
2012-03-21 02:01:57 +00:00
|
|
|
return Hash::insert($list, $path, $data);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Removes an element from a Set or array as defined by $path.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $list From where to remove
|
|
|
|
* @param string $path A dot-separated string.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Array with $path removed from its value
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::remove
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function remove($list, $path = null) {
|
2012-03-21 02:01:57 +00:00
|
|
|
return Hash::remove($list, $path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Checks if a particular path is set in an array
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $data Data to check on
|
|
|
|
* @param string|array $path A dot-separated string.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if path is found, false otherwise
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::check
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function check($data, $path = null) {
|
2008-07-31 23:50:32 +00:00
|
|
|
if (empty($path)) {
|
|
|
|
return $data;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!is_array($path)) {
|
|
|
|
$path = explode('.', $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($path as $i => $key) {
|
2008-09-14 17:17:49 +00:00
|
|
|
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$key = intval($key);
|
|
|
|
}
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($i === count($path) - 1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return (is_array($data) && array_key_exists($key, $data));
|
|
|
|
}
|
2008-09-24 12:08:24 +00:00
|
|
|
|
|
|
|
if (!is_array($data) || !array_key_exists($key, $data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$data =& $data[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Computes the difference between a Set and an array, two Sets, or two arrays
|
|
|
|
*
|
|
|
|
* @param mixed $val1 First value
|
|
|
|
* @param mixed $val2 Second value
|
2010-01-29 20:01:07 +00:00
|
|
|
* @return array Returns the key => value pairs that are not common in $val1 and $val2
|
2012-02-23 13:38:02 +00:00
|
|
|
* The expression for this function is($val1 - $val2) + ($val2 - ($val1 - $val2))
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::diff
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function diff($val1, $val2 = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($val1)) {
|
|
|
|
return (array)$val2;
|
2008-09-24 12:08:24 +00:00
|
|
|
}
|
|
|
|
if (empty($val2)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return (array)$val1;
|
|
|
|
}
|
2010-01-29 20:45:03 +00:00
|
|
|
$intersection = array_intersect_key($val1, $val2);
|
2010-01-31 23:23:32 +00:00
|
|
|
while (($key = key($intersection)) !== null) {
|
2010-01-29 20:01:07 +00:00
|
|
|
if ($val1[$key] == $val2[$key]) {
|
|
|
|
unset($val1[$key]);
|
|
|
|
unset($val2[$key]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-31 23:23:32 +00:00
|
|
|
next($intersection);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-01-29 20:01:07 +00:00
|
|
|
return $val1 + $val2;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Determines if one Set or array contains the exact keys and values of another.
|
|
|
|
*
|
|
|
|
* @param array $val1 First value
|
|
|
|
* @param array $val2 Second value
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if $val1 contains $val2, false otherwise
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::contains
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function contains($val1, $val2 = null) {
|
2008-07-31 23:50:32 +00:00
|
|
|
if (empty($val1) || empty($val2)) {
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-07-31 23:50:32 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($val2 as $key => $val) {
|
|
|
|
if (is_numeric($key)) {
|
2008-07-31 19:36:28 +00:00
|
|
|
Set::contains($val, $val1);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
if (!isset($val1[$key]) || $val1[$key] != $val) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Counts the dimensions of an array. If $all is set to false (which is the default) it will
|
|
|
|
* only consider the dimension of the first element in the array.
|
|
|
|
*
|
|
|
|
* @param array $array Array to count dimensions on
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $all Set to true to count the dimension considering all elements in array
|
|
|
|
* @param int $count Start the dimension count at this number
|
|
|
|
* @return int The number of dimensions in $array
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::countDim
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2014-04-08 01:27:45 +00:00
|
|
|
public static function countDim($array, $all = false, $count = 0) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($all) {
|
|
|
|
$depth = array($count);
|
|
|
|
if (is_array($array) && reset($array) !== false) {
|
|
|
|
foreach ($array as $value) {
|
|
|
|
$depth[] = Set::countDim($value, true, $count + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$return = max($depth);
|
|
|
|
} else {
|
|
|
|
if (is_array(reset($array))) {
|
|
|
|
$return = Set::countDim(reset($array)) + 1;
|
|
|
|
} else {
|
|
|
|
$return = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Normalizes a string or array list.
|
|
|
|
*
|
|
|
|
* @param mixed $list List to normalize
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $assoc If true, $list will be converted to an associative array
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $sep If $list is a string, it will be split into an array with $sep
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $trim If true, separated strings will be trimmed
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::normalize
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function normalize($list, $assoc = true, $sep = ',', $trim = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_string($list)) {
|
|
|
|
$list = explode($sep, $list);
|
|
|
|
if ($trim) {
|
2008-09-12 05:11:34 +00:00
|
|
|
foreach ($list as $key => $value) {
|
|
|
|
$list[$key] = trim($value);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($assoc) {
|
2012-03-21 02:01:57 +00:00
|
|
|
return Hash::normalize($list);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} elseif (is_array($list)) {
|
2012-03-21 02:01:57 +00:00
|
|
|
$list = Hash::normalize($list, $assoc);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates an associative array using a $path1 as the path to build its keys, and optionally
|
|
|
|
* $path2 as path to get the values. If $path2 is not specified, all values will be initialized
|
|
|
|
* to null (useful for Set::merge). You can optionally group the values by what is obtained when
|
|
|
|
* following the path specified in $groupPath.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array|object $data Array or object from where to extract keys and values
|
|
|
|
* @param string|array $path1 As an array, or as a dot-separated string.
|
|
|
|
* @param string|array $path2 As an array, or as a dot-separated string.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $groupPath As an array, or as a dot-separated string.
|
|
|
|
* @return array Combined array
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::combine
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function combine($data, $path1 = null, $path2 = null, $groupPath = null) {
|
2008-06-20 20:17:23 +00:00
|
|
|
if (empty($data)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_object($data)) {
|
2011-08-11 17:56:06 +00:00
|
|
|
if (!($data instanceof ArrayAccess || $data instanceof Traversable)) {
|
|
|
|
$data = get_object_vars($data);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($path1)) {
|
|
|
|
$format = array_shift($path1);
|
|
|
|
$keys = Set::format($data, $format, $path1);
|
|
|
|
} else {
|
|
|
|
$keys = Set::extract($data, $path1);
|
|
|
|
}
|
2010-03-09 04:07:36 +00:00
|
|
|
if (empty($keys)) {
|
|
|
|
return array();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!empty($path2) && is_array($path2)) {
|
|
|
|
$format = array_shift($path2);
|
|
|
|
$vals = Set::format($data, $format, $path2);
|
|
|
|
} elseif (!empty($path2)) {
|
|
|
|
$vals = Set::extract($data, $path2);
|
|
|
|
} else {
|
|
|
|
$count = count($keys);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$vals[$i] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-21 22:30:43 +00:00
|
|
|
if ($groupPath) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$group = Set::extract($data, $groupPath);
|
|
|
|
if (!empty($group)) {
|
|
|
|
$c = count($keys);
|
|
|
|
for ($i = 0; $i < $c; $i++) {
|
|
|
|
if (!isset($group[$i])) {
|
|
|
|
$group[$i] = 0;
|
|
|
|
}
|
|
|
|
if (!isset($out[$group[$i]])) {
|
|
|
|
$out[$group[$i]] = array();
|
|
|
|
}
|
|
|
|
$out[$group[$i]][$keys[$i]] = $vals[$i];
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|
2010-03-09 04:07:36 +00:00
|
|
|
if (empty($vals)) {
|
|
|
|
return array();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return array_combine($keys, $vals);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-09-25 19:05:06 +00:00
|
|
|
* Converts an object into an array.
|
2014-06-04 18:58:55 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param object $object Object to reverse
|
2009-09-25 19:05:06 +00:00
|
|
|
* @return array Array representation of given object
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::reverse
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function reverse($object) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = array();
|
2010-07-28 22:53:52 +00:00
|
|
|
if ($object instanceof SimpleXMLElement) {
|
|
|
|
return Xml::toArray($object);
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif (is_object($object)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$keys = get_object_vars($object);
|
|
|
|
if (isset($keys['_name_'])) {
|
|
|
|
$identity = $keys['_name_'];
|
|
|
|
unset($keys['_name_']);
|
|
|
|
}
|
|
|
|
$new = array();
|
|
|
|
foreach ($keys as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$new[$key] = (array)Set::reverse($value);
|
|
|
|
} else {
|
2012-03-05 03:49:38 +00:00
|
|
|
// @codingStandardsIgnoreStart Legacy junk
|
2008-08-24 22:08:13 +00:00
|
|
|
if (isset($value->_name_)) {
|
|
|
|
$new = array_merge($new, Set::reverse($value));
|
2008-08-22 19:17:48 +00:00
|
|
|
} else {
|
|
|
|
$new[$key] = Set::reverse($value);
|
2012-03-12 13:02:29 +00:00
|
|
|
}
|
2012-03-05 03:49:38 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($identity)) {
|
|
|
|
$out[$identity] = $new;
|
|
|
|
} else {
|
|
|
|
$out = $new;
|
|
|
|
}
|
|
|
|
} elseif (is_array($object)) {
|
|
|
|
foreach ($object as $key => $value) {
|
|
|
|
$out[$key] = Set::reverse($value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$out = $object;
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-22 16:32:41 +00:00
|
|
|
/**
|
|
|
|
* Collapses a multi-dimensional array into a single dimension, using a delimited array path for
|
|
|
|
* each array element's key, i.e. array(array('Foo' => array('Bar' => 'Far'))) becomes
|
|
|
|
* array('0.Foo.Bar' => 'Far').
|
|
|
|
*
|
|
|
|
* @param array $data Array to flatten
|
|
|
|
* @param string $separator String used to separate array key elements in a path, defaults to '.'
|
|
|
|
* @return array
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::flatten
|
2008-09-22 16:32:41 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function flatten($data, $separator = '.') {
|
2012-03-21 02:01:57 +00:00
|
|
|
return Hash::flatten($data, $separator);
|
2008-09-22 16:32:41 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-03-15 13:07:49 +00:00
|
|
|
/**
|
2014-07-30 20:11:03 +00:00
|
|
|
* Expand/unflattens a string to an array
|
2012-03-15 13:07:49 +00:00
|
|
|
*
|
|
|
|
* For example, unflattens an array that was collapsed with `Set::flatten()`
|
|
|
|
* into a multi-dimensional array. So, `array('0.Foo.Bar' => 'Far')` becomes
|
|
|
|
* `array(array('Foo' => array('Bar' => 'Far')))`.
|
|
|
|
*
|
|
|
|
* @param array $data Flattened array
|
|
|
|
* @param string $separator The delimiter used
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function expand($data, $separator = '.') {
|
2012-03-21 02:01:57 +00:00
|
|
|
return Hash::expand($data, $separator);
|
2012-03-15 13:07:49 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Flattens an array for sorting
|
|
|
|
*
|
2014-06-04 18:58:55 +00:00
|
|
|
* @param array $results Array to flatten.
|
|
|
|
* @param string $key Key.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected static function _flatten($results, $key = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$stack = array();
|
|
|
|
foreach ($results as $k => $r) {
|
|
|
|
$id = $k;
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($key !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$id = $key;
|
|
|
|
}
|
2009-11-21 20:14:21 +00:00
|
|
|
if (is_array($r) && !empty($r)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$stack = array_merge($stack, Set::_flatten($r, $id));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$stack[] = array('id' => $id, 'value' => $r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $stack;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sorts an array by any value, determined by a Set-compatible path
|
|
|
|
*
|
2009-09-25 19:05:06 +00:00
|
|
|
* @param array $data An array of data to sort
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $path A Set-compatible path to the array value
|
2009-09-25 19:05:06 +00:00
|
|
|
* @param string $dir Direction of sorting - either ascending (ASC), or descending (DESC)
|
|
|
|
* @return array Sorted array of data
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::sort
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function sort($data, $path, $dir) {
|
2012-11-30 18:05:14 +00:00
|
|
|
if (empty($data)) {
|
|
|
|
return $data;
|
|
|
|
}
|
2010-11-25 11:39:08 +00:00
|
|
|
$originalKeys = array_keys($data);
|
2012-03-12 13:02:29 +00:00
|
|
|
$numeric = false;
|
2010-11-25 11:39:08 +00:00
|
|
|
if (is_numeric(implode('', $originalKeys))) {
|
|
|
|
$data = array_values($data);
|
2012-03-12 13:02:29 +00:00
|
|
|
$numeric = true;
|
2010-11-25 11:39:08 +00:00
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$result = Set::_flatten(Set::extract($data, $path));
|
2008-05-30 11:40:08 +00:00
|
|
|
list($keys, $values) = array(Set::extract($result, '{n}.id'), Set::extract($result, '{n}.value'));
|
2009-03-14 19:56:28 +00:00
|
|
|
|
2009-01-14 04:06:01 +00:00
|
|
|
$dir = strtolower($dir);
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($dir === 'asc') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$dir = SORT_ASC;
|
2008-09-14 17:17:49 +00:00
|
|
|
} elseif ($dir === 'desc') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$dir = SORT_DESC;
|
|
|
|
}
|
|
|
|
array_multisort($values, $dir, $keys, $dir);
|
|
|
|
$sorted = array();
|
|
|
|
$keys = array_unique($keys);
|
|
|
|
|
|
|
|
foreach ($keys as $k) {
|
2012-03-13 05:45:06 +00:00
|
|
|
if ($numeric) {
|
2012-03-12 13:02:29 +00:00
|
|
|
$sorted[] = $data[$k];
|
2012-03-13 05:45:06 +00:00
|
|
|
} else {
|
|
|
|
if (isset($originalKeys[$k])) {
|
|
|
|
$sorted[$originalKeys[$k]] = $data[$originalKeys[$k]];
|
|
|
|
} else {
|
|
|
|
$sorted[$k] = $data[$k];
|
|
|
|
}
|
2012-03-12 13:02:29 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $sorted;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-09-24 01:03:09 +00:00
|
|
|
/**
|
|
|
|
* Allows the application of a callback method to elements of an
|
|
|
|
* array extracted by a Set::extract() compatible path.
|
|
|
|
*
|
2009-09-25 18:31:05 +00:00
|
|
|
* @param mixed $path Set-compatible path to the array value
|
|
|
|
* @param array $data An array of data to extract from & then process with the $callback.
|
2009-09-24 01:03:09 +00:00
|
|
|
* @param mixed $callback Callback method to be applied to extracted data.
|
|
|
|
* See http://ca2.php.net/manual/en/language.pseudo-types.php#language.types.callback for examples
|
|
|
|
* of callback formats.
|
2009-09-25 18:31:05 +00:00
|
|
|
* @param array $options Options are:
|
|
|
|
* - type : can be pass, map, or reduce. Map will handoff the given callback
|
|
|
|
* to array_map, reduce will handoff to array_reduce, and pass will
|
|
|
|
* use call_user_func_array().
|
2009-09-24 01:03:09 +00:00
|
|
|
* @return mixed Result of the callback when applied to extracted data
|
2011-11-18 19:13:46 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::apply
|
2009-09-24 01:03:09 +00:00
|
|
|
*/
|
2010-04-26 02:45:56 +00:00
|
|
|
public static function apply($path, $data, $callback, $options = array()) {
|
2009-09-24 01:03:09 +00:00
|
|
|
$defaults = array('type' => 'pass');
|
2014-04-07 23:25:14 +00:00
|
|
|
$options += $defaults;
|
2009-09-24 01:03:09 +00:00
|
|
|
$extracted = Set::extract($path, $data);
|
|
|
|
|
|
|
|
if ($options['type'] === 'map') {
|
2010-04-26 02:45:56 +00:00
|
|
|
return array_map($callback, $extracted);
|
2009-09-24 01:03:09 +00:00
|
|
|
} elseif ($options['type'] === 'reduce') {
|
2010-04-26 02:45:56 +00:00
|
|
|
return array_reduce($extracted, $callback);
|
2009-09-24 01:03:09 +00:00
|
|
|
} elseif ($options['type'] === 'pass') {
|
2010-04-26 02:45:56 +00:00
|
|
|
return call_user_func_array($callback, array($extracted));
|
2009-09-24 01:03:09 +00:00
|
|
|
}
|
2010-04-26 02:45:56 +00:00
|
|
|
return null;
|
2009-09-24 01:03:09 +00:00
|
|
|
}
|
2012-01-09 23:24:38 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-10 00:24:20 +00:00
|
|
|
* Takes in a flat array and returns a nested array
|
2012-01-09 23:24:38 +00:00
|
|
|
*
|
2014-06-04 18:58:55 +00:00
|
|
|
* @param mixed $data Data
|
2012-01-09 23:24:38 +00:00
|
|
|
* @param array $options Options are:
|
2012-01-18 12:05:44 +00:00
|
|
|
* children - the key name to use in the resultset for children
|
|
|
|
* idPath - the path to a key that identifies each entry
|
2012-01-09 23:48:15 +00:00
|
|
|
* parentPath - the path to a key that identifies the parent of each entry
|
2012-01-18 12:05:44 +00:00
|
|
|
* root - the id of the desired top-most result
|
2012-01-09 23:24:38 +00:00
|
|
|
* @return array of results, nested
|
|
|
|
* @link
|
|
|
|
*/
|
|
|
|
public static function nest($data, $options = array()) {
|
|
|
|
if (!$data) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:24:20 +00:00
|
|
|
$alias = key(current($data));
|
2012-01-10 09:18:59 +00:00
|
|
|
$options += array(
|
2012-01-10 00:24:20 +00:00
|
|
|
'idPath' => "/$alias/id",
|
|
|
|
'parentPath' => "/$alias/parent_id",
|
2012-01-18 12:05:44 +00:00
|
|
|
'children' => 'children',
|
|
|
|
'root' => null
|
2012-01-10 09:18:59 +00:00
|
|
|
);
|
2012-01-09 23:24:38 +00:00
|
|
|
|
|
|
|
$return = $idMap = array();
|
2012-01-09 23:48:15 +00:00
|
|
|
$ids = Set::extract($data, $options['idPath']);
|
2012-01-10 00:24:20 +00:00
|
|
|
$idKeys = explode('/', trim($options['idPath'], '/'));
|
|
|
|
$parentKeys = explode('/', trim($options['parentPath'], '/'));
|
2012-01-09 23:24:38 +00:00
|
|
|
|
|
|
|
foreach ($data as $result) {
|
|
|
|
$result[$options['children']] = array();
|
2012-01-10 00:24:20 +00:00
|
|
|
|
2012-01-12 08:45:28 +00:00
|
|
|
$id = Set::get($result, $idKeys);
|
|
|
|
$parentId = Set::get($result, $parentKeys);
|
2012-01-10 00:24:20 +00:00
|
|
|
|
2012-01-09 23:48:15 +00:00
|
|
|
if (isset($idMap[$id][$options['children']])) {
|
2012-01-09 23:24:38 +00:00
|
|
|
$idMap[$id] = array_merge($result, (array)$idMap[$id]);
|
|
|
|
} else {
|
2012-01-09 23:48:15 +00:00
|
|
|
$idMap[$id] = array_merge($result, array($options['children'] => array()));
|
2012-01-09 23:24:38 +00:00
|
|
|
}
|
|
|
|
if (!$parentId || !in_array($parentId, $ids)) {
|
|
|
|
$return[] =& $idMap[$id];
|
|
|
|
} else {
|
2012-01-09 23:48:15 +00:00
|
|
|
$idMap[$parentId][$options['children']][] =& $idMap[$id];
|
2012-01-09 23:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-10 00:24:20 +00:00
|
|
|
|
2012-01-18 12:05:44 +00:00
|
|
|
if ($options['root']) {
|
|
|
|
$root = $options['root'];
|
|
|
|
} else {
|
|
|
|
$root = Set::get($return[0], $parentKeys);
|
|
|
|
}
|
2012-01-10 00:24:20 +00:00
|
|
|
|
|
|
|
foreach ($return as $i => $result) {
|
2012-01-18 12:05:44 +00:00
|
|
|
$id = Set::get($result, $idKeys);
|
2012-01-12 08:45:28 +00:00
|
|
|
$parentId = Set::get($result, $parentKeys);
|
2012-01-18 12:05:44 +00:00
|
|
|
if ($id !== $root && $parentId != $root) {
|
2012-01-10 00:24:20 +00:00
|
|
|
unset($return[$i]);
|
2012-01-09 23:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-10 00:24:20 +00:00
|
|
|
|
2012-01-18 12:05:44 +00:00
|
|
|
return array_values($return);
|
2012-01-09 23:24:38 +00:00
|
|
|
}
|
2012-01-10 08:47:51 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-12 08:45:28 +00:00
|
|
|
* Return the value at the specified position
|
2012-01-10 08:47:51 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $input an array
|
|
|
|
* @param string|array $path string or array of array keys
|
2012-01-10 08:47:51 +00:00
|
|
|
* @return the value at the specified position or null if it doesn't exist
|
|
|
|
*/
|
2012-01-12 08:45:28 +00:00
|
|
|
public static function get($input, $path = null) {
|
2012-01-10 08:47:51 +00:00
|
|
|
if (is_string($path)) {
|
2012-01-18 12:05:44 +00:00
|
|
|
if (strpos($path, '/') !== false) {
|
|
|
|
$keys = explode('/', trim($path, '/'));
|
|
|
|
} else {
|
|
|
|
$keys = explode('.', trim($path, '.'));
|
|
|
|
}
|
2012-01-10 08:47:51 +00:00
|
|
|
} else {
|
|
|
|
$keys = $path;
|
|
|
|
}
|
2012-03-21 02:01:57 +00:00
|
|
|
return Hash::get($input, $keys);
|
2012-01-10 08:47:51 +00:00
|
|
|
}
|
2012-03-03 22:31:47 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|