2012-01-12 02:33:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2012-01-12 02:33:14 +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
|
2012-01-12 02:33:14 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2012-01-12 02:33:14 +00:00
|
|
|
* @package Cake.Utility
|
2012-02-28 21:52:54 +00:00
|
|
|
* @since CakePHP(tm) v 2.2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2012-01-12 02:33:14 +00:00
|
|
|
*/
|
|
|
|
|
2015-01-05 00:00:57 +00:00
|
|
|
App::uses('CakeText', 'Utility');
|
2012-01-12 02:33:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Library of array functions for manipulating and extracting data
|
|
|
|
* from arrays or 'sets' of data.
|
|
|
|
*
|
2012-03-27 02:57:50 +00:00
|
|
|
* `Hash` provides an improved interface, more consistent and
|
2012-12-22 22:48:15 +00:00
|
|
|
* predictable set of features over `Set`. While it lacks the spotty
|
2012-01-26 01:34:43 +00:00
|
|
|
* support for pseudo Xpath, its more fully featured dot notation provides
|
2012-03-27 02:57:50 +00:00
|
|
|
* similar features in a more consistent implementation.
|
2012-01-12 02:33:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Utility
|
|
|
|
*/
|
2012-02-28 21:52:54 +00:00
|
|
|
class Hash {
|
2012-01-12 02:33:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a single value specified by $path out of $data.
|
2012-03-27 02:57:50 +00:00
|
|
|
* Does not support the full dot notation feature set,
|
|
|
|
* but is faster for simple read operations.
|
2012-01-12 02:33:14 +00:00
|
|
|
*
|
|
|
|
* @param array $data Array of data to operate on.
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $path The path being searched for. Either a dot
|
2012-02-23 02:53:33 +00:00
|
|
|
* separated string, or an array of path segments.
|
2013-10-31 22:31:48 +00:00
|
|
|
* @param mixed $default The return value when the path does not exist
|
2014-06-20 01:03:17 +00:00
|
|
|
* @throws InvalidArgumentException
|
2012-01-12 02:33:14 +00:00
|
|
|
* @return mixed The value fetched from the array, or null.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::get
|
2012-01-12 02:33:14 +00:00
|
|
|
*/
|
2013-10-31 22:31:48 +00:00
|
|
|
public static function get(array $data, $path, $default = null) {
|
2016-10-21 14:54:39 +00:00
|
|
|
if (empty($data) || $path === null) {
|
2013-10-31 22:31:48 +00:00
|
|
|
return $default;
|
2012-01-12 02:33:14 +00:00
|
|
|
}
|
2013-01-22 02:13:35 +00:00
|
|
|
if (is_string($path) || is_numeric($path)) {
|
2012-02-23 02:40:48 +00:00
|
|
|
$parts = explode('.', $path);
|
2017-02-14 02:50:51 +00:00
|
|
|
} elseif (is_bool($path) || $path === null) {
|
2017-02-14 19:12:38 +00:00
|
|
|
$parts = array($path);
|
2012-02-23 02:40:48 +00:00
|
|
|
} else {
|
2014-06-20 01:03:17 +00:00
|
|
|
if (!is_array($path)) {
|
2014-07-03 17:43:55 +00:00
|
|
|
throw new InvalidArgumentException(__d('cake_dev',
|
2017-02-14 02:50:51 +00:00
|
|
|
'Invalid path parameter: %s, should be dot separated path or array.',
|
|
|
|
var_export($path, true)
|
2014-07-03 17:43:55 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
$parts = $path;
|
2012-02-23 02:40:48 +00:00
|
|
|
}
|
2014-07-03 17:43:55 +00:00
|
|
|
|
2012-06-24 16:47:26 +00:00
|
|
|
foreach ($parts as $key) {
|
2012-01-22 13:03:15 +00:00
|
|
|
if (is_array($data) && isset($data[$key])) {
|
2012-01-12 02:33:14 +00:00
|
|
|
$data =& $data[$key];
|
|
|
|
} else {
|
2013-10-31 22:31:48 +00:00
|
|
|
return $default;
|
2012-01-12 02:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-20 01:03:17 +00:00
|
|
|
|
2012-01-12 02:33:14 +00:00
|
|
|
return $data;
|
|
|
|
}
|
2012-01-12 02:59:56 +00:00
|
|
|
|
2012-01-22 13:03:15 +00:00
|
|
|
/**
|
|
|
|
* Gets the values from an array matching the $path expression.
|
|
|
|
* The path expression is a dot separated expression, that can contain a set
|
|
|
|
* of patterns and expressions:
|
|
|
|
*
|
2012-03-27 02:57:50 +00:00
|
|
|
* - `{n}` Matches any numeric key, or integer.
|
2012-01-22 13:03:15 +00:00
|
|
|
* - `{s}` Matches any string key.
|
2015-05-02 02:01:45 +00:00
|
|
|
* - `{*}` Matches any value.
|
2012-03-27 02:57:50 +00:00
|
|
|
* - `Foo` Matches any key with the exact same value.
|
2012-01-22 19:52:54 +00:00
|
|
|
*
|
|
|
|
* There are a number of attribute operators:
|
|
|
|
*
|
|
|
|
* - `=`, `!=` Equality.
|
|
|
|
* - `>`, `<`, `>=`, `<=` Value comparison.
|
|
|
|
* - `=/.../` Regular expression pattern match.
|
2012-01-22 13:03:15 +00:00
|
|
|
*
|
|
|
|
* Given a set of User array data, from a `$User->find('all')` call:
|
|
|
|
*
|
|
|
|
* - `1.User.name` Get the name of the user at index 1.
|
|
|
|
* - `{n}.User.name` Get the name of every user in the set of users.
|
2017-10-06 09:15:52 +00:00
|
|
|
* - `{n}.User[id].name` Get the name of every user with an id key.
|
|
|
|
* - `{n}.User[id>=2].name` Get the name of every user with an id key greater than or equal to 2.
|
2012-03-27 02:57:50 +00:00
|
|
|
* - `{n}.User[username=/^paul/]` Get User elements with username matching `^paul`.
|
2012-01-22 13:03:15 +00:00
|
|
|
*
|
|
|
|
* @param array $data The data to extract from.
|
|
|
|
* @param string $path The path to extract.
|
2012-12-22 22:48:15 +00:00
|
|
|
* @return array An array of the extracted values. Returns an empty array
|
2012-01-22 13:03:15 +00:00
|
|
|
* if there are no matches.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::extract
|
2012-01-22 13:03:15 +00:00
|
|
|
*/
|
2012-01-12 02:59:56 +00:00
|
|
|
public static function extract(array $data, $path) {
|
2012-01-22 13:03:15 +00:00
|
|
|
if (empty($path)) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple paths.
|
|
|
|
if (!preg_match('/[{\[]/', $path)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
return (array)static::get($data, $path);
|
2012-01-22 13:03:15 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 19:40:27 +00:00
|
|
|
if (strpos($path, '[') === false) {
|
2012-01-22 19:52:54 +00:00
|
|
|
$tokens = explode('.', $path);
|
|
|
|
} else {
|
2015-01-05 00:00:57 +00:00
|
|
|
$tokens = CakeText::tokenize($path, '.', '[', ']');
|
2012-01-22 19:52:54 +00:00
|
|
|
}
|
2012-01-22 13:03:15 +00:00
|
|
|
|
|
|
|
$_key = '__set_item__';
|
|
|
|
|
|
|
|
$context = array($_key => array($data));
|
|
|
|
|
2012-06-24 16:47:26 +00:00
|
|
|
foreach ($tokens as $token) {
|
2012-01-22 13:03:15 +00:00
|
|
|
$next = array();
|
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
list($token, $conditions) = static::_splitConditions($token);
|
2012-01-24 03:35:54 +00:00
|
|
|
|
2012-01-22 13:03:15 +00:00
|
|
|
foreach ($context[$_key] as $item) {
|
2012-12-14 22:45:52 +00:00
|
|
|
foreach ((array)$item as $k => $v) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (static::_matchToken($k, $token)) {
|
2012-01-26 03:41:20 +00:00
|
|
|
$next[] = $v;
|
2012-01-22 13:03:15 +00:00
|
|
|
}
|
2012-01-24 03:35:54 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 02:57:50 +00:00
|
|
|
|
2012-01-24 03:35:54 +00:00
|
|
|
// Filter for attributes.
|
|
|
|
if ($conditions) {
|
|
|
|
$filter = array();
|
|
|
|
foreach ($next as $item) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (is_array($item) && static::_matches($item, $conditions)) {
|
2012-01-24 03:35:54 +00:00
|
|
|
$filter[] = $item;
|
2012-01-22 19:52:54 +00:00
|
|
|
}
|
2012-01-22 13:03:15 +00:00
|
|
|
}
|
2012-01-24 03:35:54 +00:00
|
|
|
$next = $filter;
|
2012-01-22 13:03:15 +00:00
|
|
|
}
|
|
|
|
$context = array($_key => $next);
|
|
|
|
|
2012-06-24 16:47:26 +00:00
|
|
|
}
|
2012-01-27 02:33:03 +00:00
|
|
|
return $context[$_key];
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
2017-10-06 09:15:52 +00:00
|
|
|
|
2013-08-23 10:27:06 +00:00
|
|
|
/**
|
|
|
|
* Split token conditions
|
|
|
|
*
|
|
|
|
* @param string $token the token being splitted.
|
|
|
|
* @return array array(token, conditions) with token splitted
|
|
|
|
*/
|
|
|
|
protected static function _splitConditions($token) {
|
|
|
|
$conditions = false;
|
|
|
|
$position = strpos($token, '[');
|
|
|
|
if ($position !== false) {
|
|
|
|
$conditions = substr($token, $position);
|
|
|
|
$token = substr($token, 0, $position);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($token, $conditions);
|
|
|
|
}
|
2012-01-12 02:59:56 +00:00
|
|
|
|
2012-01-26 03:41:20 +00:00
|
|
|
/**
|
|
|
|
* Check a key against a token.
|
|
|
|
*
|
|
|
|
* @param string $key The key in the array being searched.
|
|
|
|
* @param string $token The token being matched.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-01-26 03:41:20 +00:00
|
|
|
*/
|
|
|
|
protected static function _matchToken($key, $token) {
|
2015-05-02 02:01:45 +00:00
|
|
|
switch ($token) {
|
|
|
|
case '{n}':
|
|
|
|
return is_numeric($key);
|
|
|
|
case '{s}':
|
|
|
|
return is_string($key);
|
|
|
|
case '{*}':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return is_numeric($token) ? ($key == $token) : $key === $token;
|
2012-02-20 05:01:11 +00:00
|
|
|
}
|
2012-01-26 03:41:20 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 19:52:54 +00:00
|
|
|
/**
|
2012-03-27 02:57:50 +00:00
|
|
|
* Checks whether or not $data matches the attribute patterns
|
2012-01-22 19:52:54 +00:00
|
|
|
*
|
|
|
|
* @param array $data Array of data to match.
|
2012-03-27 02:57:50 +00:00
|
|
|
* @param string $selector The patterns to match.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Fitness of expression.
|
2012-03-27 02:57:50 +00:00
|
|
|
*/
|
2012-01-22 19:52:54 +00:00
|
|
|
protected static function _matches(array $data, $selector) {
|
|
|
|
preg_match_all(
|
2013-05-09 16:18:26 +00:00
|
|
|
'/(\[ (?P<attr>[^=><!]+?) (\s* (?P<op>[><!]?[=]|[><]) \s* (?P<val>(?:\/.*?\/ | [^\]]+)) )? \])/x',
|
2012-01-22 19:52:54 +00:00
|
|
|
$selector,
|
|
|
|
$conditions,
|
|
|
|
PREG_SET_ORDER
|
|
|
|
);
|
|
|
|
|
2012-06-24 16:47:26 +00:00
|
|
|
foreach ($conditions as $cond) {
|
2012-01-22 19:52:54 +00:00
|
|
|
$attr = $cond['attr'];
|
|
|
|
$op = isset($cond['op']) ? $cond['op'] : null;
|
|
|
|
$val = isset($cond['val']) ? $cond['val'] : null;
|
|
|
|
|
|
|
|
// Presence test.
|
2012-01-24 12:27:21 +00:00
|
|
|
if (empty($op) && empty($val) && !isset($data[$attr])) {
|
2012-01-25 03:27:01 +00:00
|
|
|
return false;
|
2012-01-22 19:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty attribute = fail.
|
2012-05-31 01:28:18 +00:00
|
|
|
if (!(isset($data[$attr]) || array_key_exists($attr, $data))) {
|
2012-01-25 03:27:01 +00:00
|
|
|
return false;
|
2012-01-24 12:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 00:37:08 +00:00
|
|
|
$prop = null;
|
|
|
|
if (isset($data[$attr])) {
|
|
|
|
$prop = $data[$attr];
|
|
|
|
}
|
2014-06-12 15:30:48 +00:00
|
|
|
$isBool = is_bool($prop);
|
|
|
|
if ($isBool && is_numeric($val)) {
|
|
|
|
$prop = $prop ? '1' : '0';
|
|
|
|
} elseif ($isBool) {
|
2014-04-11 00:37:08 +00:00
|
|
|
$prop = $prop ? 'true' : 'false';
|
|
|
|
}
|
2012-01-24 12:27:21 +00:00
|
|
|
|
2012-01-25 03:27:01 +00:00
|
|
|
// Pattern matches and other operators.
|
|
|
|
if ($op === '=' && $val && $val[0] === '/') {
|
|
|
|
if (!preg_match($val, $prop)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-09 02:17:35 +00:00
|
|
|
} elseif (($op === '=' && $prop != $val) ||
|
2012-01-24 12:27:21 +00:00
|
|
|
($op === '!=' && $prop == $val) ||
|
|
|
|
($op === '>' && $prop <= $val) ||
|
|
|
|
($op === '<' && $prop >= $val) ||
|
|
|
|
($op === '>=' && $prop < $val) ||
|
|
|
|
($op === '<=' && $prop > $val)
|
|
|
|
) {
|
|
|
|
return false;
|
2012-01-22 19:52:54 +00:00
|
|
|
}
|
2012-01-25 03:27:01 +00:00
|
|
|
|
2012-01-22 19:52:54 +00:00
|
|
|
}
|
2012-01-24 12:27:21 +00:00
|
|
|
return true;
|
2012-01-22 19:52:54 +00:00
|
|
|
}
|
|
|
|
|
2012-01-27 02:33:03 +00:00
|
|
|
/**
|
2012-03-27 02:57:50 +00:00
|
|
|
* Insert $values into an array with the given $path. You can use
|
|
|
|
* `{n}` and `{s}` elements to insert $data multiple times.
|
2012-01-27 02:33:03 +00:00
|
|
|
*
|
|
|
|
* @param array $data The data to insert into.
|
|
|
|
* @param string $path The path to insert at.
|
2015-04-13 16:20:16 +00:00
|
|
|
* @param mixed $values The values to insert.
|
2012-01-27 02:33:03 +00:00
|
|
|
* @return array The data with $values inserted.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::insert
|
2012-01-27 02:33:03 +00:00
|
|
|
*/
|
2012-01-12 02:59:56 +00:00
|
|
|
public static function insert(array $data, $path, $values = null) {
|
2013-08-23 10:27:06 +00:00
|
|
|
if (strpos($path, '[') === false) {
|
|
|
|
$tokens = explode('.', $path);
|
|
|
|
} else {
|
2015-01-05 00:00:57 +00:00
|
|
|
$tokens = CakeText::tokenize($path, '.', '[', ']');
|
2013-08-23 10:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($path, '{') === false && strpos($path, '[') === false) {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::_simpleOp('insert', $data, $tokens, $values);
|
2012-01-26 01:34:43 +00:00
|
|
|
}
|
2012-01-12 02:59:56 +00:00
|
|
|
|
2012-01-27 02:33:03 +00:00
|
|
|
$token = array_shift($tokens);
|
|
|
|
$nextPath = implode('.', $tokens);
|
2013-08-23 10:27:06 +00:00
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
list($token, $conditions) = static::_splitConditions($token);
|
2013-08-23 10:27:06 +00:00
|
|
|
|
2012-01-27 02:33:03 +00:00
|
|
|
foreach ($data as $k => $v) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (static::_matchToken($k, $token)) {
|
|
|
|
if ($conditions && static::_matches($v, $conditions)) {
|
2013-08-25 01:49:17 +00:00
|
|
|
$data[$k] = array_merge($v, $values);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$conditions) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$data[$k] = static::insert($v, $nextPath, $values);
|
2013-08-23 10:27:06 +00:00
|
|
|
}
|
2012-01-27 02:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2012-01-26 01:34:43 +00:00
|
|
|
|
2012-01-27 02:33:03 +00:00
|
|
|
/**
|
2012-01-27 03:12:18 +00:00
|
|
|
* Perform a simple insert/remove operation.
|
2012-01-27 02:33:03 +00:00
|
|
|
*
|
2012-01-27 03:12:18 +00:00
|
|
|
* @param string $op The operation to do.
|
|
|
|
* @param array $data The data to operate on.
|
|
|
|
* @param array $path The path to work on.
|
|
|
|
* @param mixed $values The values to insert when doing inserts.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return array data.
|
2012-01-27 02:33:03 +00:00
|
|
|
*/
|
2012-01-27 03:12:18 +00:00
|
|
|
protected static function _simpleOp($op, $data, $path, $values = null) {
|
2012-01-27 02:33:03 +00:00
|
|
|
$_list =& $data;
|
|
|
|
|
|
|
|
$count = count($path);
|
2012-04-01 01:25:02 +00:00
|
|
|
$last = $count - 1;
|
2012-01-27 02:33:03 +00:00
|
|
|
foreach ($path as $i => $key) {
|
2014-09-10 20:47:25 +00:00
|
|
|
if ((is_numeric($key) && intval($key) > 0 || $key === '0') && strpos($key, '0') !== 0) {
|
2014-09-10 13:52:57 +00:00
|
|
|
$key = (int)$key;
|
2012-01-27 02:33:03 +00:00
|
|
|
}
|
2012-01-27 03:12:18 +00:00
|
|
|
if ($op === 'insert') {
|
2012-04-01 01:25:02 +00:00
|
|
|
if ($i === $last) {
|
2012-01-27 03:12:18 +00:00
|
|
|
$_list[$key] = $values;
|
2012-04-01 01:25:02 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
if (!isset($_list[$key])) {
|
|
|
|
$_list[$key] = array();
|
2012-01-27 03:12:18 +00:00
|
|
|
}
|
2012-04-01 01:25:02 +00:00
|
|
|
$_list =& $_list[$key];
|
2012-01-27 03:12:18 +00:00
|
|
|
if (!is_array($_list)) {
|
2012-03-27 02:39:13 +00:00
|
|
|
$_list = array();
|
2012-01-27 03:12:18 +00:00
|
|
|
}
|
2012-04-01 01:25:02 +00:00
|
|
|
} elseif ($op === 'remove') {
|
|
|
|
if ($i === $last) {
|
2012-01-27 03:12:18 +00:00
|
|
|
unset($_list[$key]);
|
2012-04-01 01:37:10 +00:00
|
|
|
return $data;
|
2012-01-27 02:33:03 +00:00
|
|
|
}
|
2012-04-01 01:37:10 +00:00
|
|
|
if (!isset($_list[$key])) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
$_list =& $_list[$key];
|
2012-01-27 02:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 01:34:43 +00:00
|
|
|
/**
|
|
|
|
* Remove data matching $path from the $data array.
|
2012-03-27 02:57:50 +00:00
|
|
|
* You can use `{n}` and `{s}` to remove multiple elements
|
|
|
|
* from $data.
|
2012-01-26 01:34:43 +00:00
|
|
|
*
|
|
|
|
* @param array $data The data to operate on
|
|
|
|
* @param string $path A path expression to use to remove.
|
|
|
|
* @return array The modified array.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::remove
|
2012-01-26 01:34:43 +00:00
|
|
|
*/
|
2012-01-12 02:59:56 +00:00
|
|
|
public static function remove(array $data, $path) {
|
2013-08-23 10:27:06 +00:00
|
|
|
if (strpos($path, '[') === false) {
|
|
|
|
$tokens = explode('.', $path);
|
|
|
|
} else {
|
2015-01-05 00:00:57 +00:00
|
|
|
$tokens = CakeText::tokenize($path, '.', '[', ']');
|
2013-08-23 10:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($path, '{') === false && strpos($path, '[') === false) {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::_simpleOp('remove', $data, $tokens);
|
2012-01-27 02:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$token = array_shift($tokens);
|
|
|
|
$nextPath = implode('.', $tokens);
|
2013-08-23 10:27:06 +00:00
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
list($token, $conditions) = static::_splitConditions($token);
|
2013-08-23 10:27:06 +00:00
|
|
|
|
2012-01-27 02:33:03 +00:00
|
|
|
foreach ($data as $k => $v) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$match = static::_matchToken($k, $token);
|
2012-01-27 02:33:03 +00:00
|
|
|
if ($match && is_array($v)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if ($conditions && static::_matches($v, $conditions)) {
|
2013-08-23 10:27:06 +00:00
|
|
|
unset($data[$k]);
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
$data[$k] = static::remove($v, $nextPath);
|
2013-08-23 10:27:06 +00:00
|
|
|
if (empty($data[$k])) {
|
|
|
|
unset($data[$k]);
|
|
|
|
}
|
2014-09-23 20:00:49 +00:00
|
|
|
} elseif ($match && empty($nextPath)) {
|
2012-01-27 02:33:03 +00:00
|
|
|
unset($data[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2012-02-20 04:34:51 +00:00
|
|
|
/**
|
|
|
|
* Creates an associative array using `$keyPath` as the path to build its keys, and optionally
|
|
|
|
* `$valuePath` as path to get the values. If `$valuePath` is not specified, all values will be initialized
|
2012-03-11 01:57:18 +00:00
|
|
|
* to null (useful for Hash::merge). You can optionally group the values by what is obtained when
|
2012-02-20 04:34:51 +00:00
|
|
|
* following the path specified in `$groupPath`.
|
|
|
|
*
|
|
|
|
* @param array $data Array from where to extract keys and values
|
|
|
|
* @param string $keyPath A dot-separated string.
|
|
|
|
* @param string $valuePath A dot-separated string.
|
|
|
|
* @param string $groupPath A dot-separated string.
|
|
|
|
* @return array Combined array
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::combine
|
2013-12-23 08:01:43 +00:00
|
|
|
* @throws CakeException CakeException When keys and values count is unequal.
|
2012-02-20 04:34:51 +00:00
|
|
|
*/
|
|
|
|
public static function combine(array $data, $keyPath, $valuePath = null, $groupPath = null) {
|
|
|
|
if (empty($data)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($keyPath)) {
|
|
|
|
$format = array_shift($keyPath);
|
2015-07-21 08:22:53 +00:00
|
|
|
$keys = static::format($data, $keyPath, $format);
|
2012-02-20 04:34:51 +00:00
|
|
|
} else {
|
2015-07-21 08:22:53 +00:00
|
|
|
$keys = static::extract($data, $keyPath);
|
2012-02-20 04:34:51 +00:00
|
|
|
}
|
|
|
|
if (empty($keys)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($valuePath) && is_array($valuePath)) {
|
|
|
|
$format = array_shift($valuePath);
|
2015-07-21 08:22:53 +00:00
|
|
|
$vals = static::format($data, $valuePath, $format);
|
2012-02-20 04:34:51 +00:00
|
|
|
} elseif (!empty($valuePath)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$vals = static::extract($data, $valuePath);
|
2012-02-20 04:34:51 +00:00
|
|
|
}
|
2013-12-21 22:46:05 +00:00
|
|
|
if (empty($vals)) {
|
|
|
|
$vals = array_fill(0, count($keys), null);
|
|
|
|
}
|
2012-02-20 04:34:51 +00:00
|
|
|
|
2013-12-21 22:46:05 +00:00
|
|
|
if (count($keys) !== count($vals)) {
|
|
|
|
throw new CakeException(__d(
|
|
|
|
'cake_dev',
|
|
|
|
'Hash::combine() needs an equal number of keys + values.'
|
|
|
|
));
|
2012-02-20 04:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 05:01:11 +00:00
|
|
|
if ($groupPath !== null) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$group = static::extract($data, $groupPath);
|
2012-02-20 04:34:51 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($vals)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
return array_combine($keys, $vals);
|
2012-02-20 05:01:11 +00:00
|
|
|
}
|
2012-01-12 02:59:56 +00:00
|
|
|
|
2012-02-20 05:01:11 +00:00
|
|
|
/**
|
2013-03-05 07:05:14 +00:00
|
|
|
* Returns a formatted series of values extracted from `$data`, using
|
2012-02-20 05:01:11 +00:00
|
|
|
* `$format` as the format and `$paths` as the values to extract.
|
2012-04-04 16:21:52 +00:00
|
|
|
*
|
2012-02-22 03:21:24 +00:00
|
|
|
* Usage:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2012-03-11 01:57:18 +00:00
|
|
|
* $result = Hash::format($users, array('{n}.User.id', '{n}.User.name'), '%s : %s');
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2012-02-22 03:21:24 +00:00
|
|
|
*
|
|
|
|
* The `$format` string can use any format options that `vsprintf()` and `sprintf()` do.
|
2012-02-20 05:01:11 +00:00
|
|
|
*
|
|
|
|
* @param array $data Source array from which to extract the data
|
2017-10-06 09:15:52 +00:00
|
|
|
* @param array $paths An array containing one or more Hash::extract()-style key paths
|
2012-02-20 05:01:11 +00:00
|
|
|
* @param string $format Format string into which values will be inserted, see sprintf()
|
|
|
|
* @return array An array of strings extracted from `$path` and formatted with `$format`
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::format
|
2012-02-20 05:01:11 +00:00
|
|
|
* @see sprintf()
|
2012-02-28 21:52:54 +00:00
|
|
|
* @see Hash::extract()
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::format
|
2012-02-20 05:01:11 +00:00
|
|
|
*/
|
|
|
|
public static function format(array $data, array $paths, $format) {
|
|
|
|
$extracted = array();
|
|
|
|
$count = count($paths);
|
|
|
|
|
|
|
|
if (!$count) {
|
2015-09-25 15:11:20 +00:00
|
|
|
return null;
|
2012-02-20 05:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$extracted[] = static::extract($data, $paths[$i]);
|
2012-02-20 05:01:11 +00:00
|
|
|
}
|
|
|
|
$out = array();
|
|
|
|
$data = $extracted;
|
|
|
|
$count = count($data[0]);
|
|
|
|
|
2012-03-27 02:57:50 +00:00
|
|
|
$countTwo = count($data);
|
2012-02-20 05:01:11 +00:00
|
|
|
for ($j = 0; $j < $count; $j++) {
|
|
|
|
$args = array();
|
2012-03-27 02:57:50 +00:00
|
|
|
for ($i = 0; $i < $countTwo; $i++) {
|
2012-02-20 05:01:11 +00:00
|
|
|
if (array_key_exists($j, $data[$i])) {
|
|
|
|
$args[] = $data[$i][$j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$out[] = vsprintf($format, $args);
|
|
|
|
}
|
|
|
|
return $out;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 03:42:58 +00:00
|
|
|
/**
|
|
|
|
* Determines if one array contains the exact keys and values of another.
|
|
|
|
*
|
|
|
|
* @param array $data The data to search through.
|
|
|
|
* @param array $needle The values to file in $data
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if $data contains $needle, false otherwise
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::contains
|
2012-01-16 03:42:58 +00:00
|
|
|
*/
|
|
|
|
public static function contains(array $data, array $needle) {
|
|
|
|
if (empty($data) || empty($needle)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-22 02:53:28 +00:00
|
|
|
$stack = array();
|
|
|
|
|
|
|
|
while (!empty($needle)) {
|
|
|
|
$key = key($needle);
|
|
|
|
$val = $needle[$key];
|
|
|
|
unset($needle[$key]);
|
2012-01-12 02:59:56 +00:00
|
|
|
|
2013-09-20 01:43:59 +00:00
|
|
|
if (array_key_exists($key, $data) && is_array($val)) {
|
2012-01-22 02:53:28 +00:00
|
|
|
$next = $data[$key];
|
|
|
|
unset($data[$key]);
|
|
|
|
|
|
|
|
if (!empty($val)) {
|
|
|
|
$stack[] = array($val, $next);
|
2012-01-16 03:42:58 +00:00
|
|
|
}
|
2013-09-20 01:43:59 +00:00
|
|
|
} elseif (!array_key_exists($key, $data) || $data[$key] != $val) {
|
2012-01-16 03:42:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-01-22 02:53:28 +00:00
|
|
|
|
|
|
|
if (empty($needle) && !empty($stack)) {
|
|
|
|
list($needle, $data) = array_pop($stack);
|
|
|
|
}
|
2012-01-16 03:42:58 +00:00
|
|
|
}
|
|
|
|
return true;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 04:11:43 +00:00
|
|
|
/**
|
|
|
|
* Test whether or not a given path exists in $data.
|
2012-02-28 21:52:54 +00:00
|
|
|
* This method uses the same path syntax as Hash::extract()
|
2012-02-20 04:11:43 +00:00
|
|
|
*
|
|
|
|
* Checking for paths that could target more than one element will
|
|
|
|
* make sure that at least one matching element exists.
|
|
|
|
*
|
|
|
|
* @param array $data The data to check.
|
|
|
|
* @param string $path The path to check for.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Existence of path.
|
2012-02-28 21:52:54 +00:00
|
|
|
* @see Hash::extract()
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::check
|
2012-02-20 04:11:43 +00:00
|
|
|
*/
|
2012-01-12 02:59:56 +00:00
|
|
|
public static function check(array $data, $path) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$results = static::extract($data, $path);
|
2012-02-20 04:11:43 +00:00
|
|
|
if (!is_array($results)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return count($results) > 0;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 03:03:25 +00:00
|
|
|
/**
|
2012-02-23 02:53:33 +00:00
|
|
|
* Recursively filters a data set.
|
2012-01-22 03:03:25 +00:00
|
|
|
*
|
|
|
|
* @param array $data Either an array to filter, or value when in callback
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param callable $callback A function to filter the data with. Defaults to
|
2015-07-21 08:22:53 +00:00
|
|
|
* `static::_filter()` Which strips out all non-zero empty values.
|
2012-01-22 03:03:25 +00:00
|
|
|
* @return array Filtered array
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::filter
|
2012-01-22 03:03:25 +00:00
|
|
|
*/
|
2012-02-23 02:53:33 +00:00
|
|
|
public static function filter(array $data, $callback = array('self', '_filter')) {
|
2012-01-22 03:03:25 +00:00
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
if (is_array($v)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$data[$k] = static::filter($v, $callback);
|
2012-01-22 03:03:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-23 02:53:33 +00:00
|
|
|
return array_filter($data, $callback);
|
2012-01-22 03:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for filtering.
|
|
|
|
*
|
|
|
|
* @param array $var Array to filter.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-01-22 03:03:25 +00:00
|
|
|
*/
|
|
|
|
protected static function _filter($var) {
|
2017-03-10 02:29:44 +00:00
|
|
|
if ($var === 0 || $var === 0.0 || $var === '0' || !empty($var)) {
|
2012-01-22 03:03:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-15 03:47:44 +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
|
2012-01-16 02:38:13 +00:00
|
|
|
* array('0.Foo.Bar' => 'Far').)
|
2012-01-15 03:47:44 +00:00
|
|
|
*
|
|
|
|
* @param array $data Array to flatten
|
|
|
|
* @param string $separator String used to separate array key elements in a path, defaults to '.'
|
|
|
|
* @return array
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::flatten
|
2012-01-15 03:47:44 +00:00
|
|
|
*/
|
|
|
|
public static function flatten(array $data, $separator = '.') {
|
|
|
|
$result = array();
|
|
|
|
$stack = array();
|
|
|
|
$path = null;
|
2012-01-12 02:59:56 +00:00
|
|
|
|
2012-01-15 03:47:44 +00:00
|
|
|
reset($data);
|
|
|
|
while (!empty($data)) {
|
|
|
|
$key = key($data);
|
|
|
|
$element = $data[$key];
|
|
|
|
unset($data[$key]);
|
|
|
|
|
2012-11-02 22:23:39 +00:00
|
|
|
if (is_array($element) && !empty($element)) {
|
2012-01-15 03:47:44 +00:00
|
|
|
if (!empty($data)) {
|
|
|
|
$stack[] = array($data, $path);
|
|
|
|
}
|
|
|
|
$data = $element;
|
2013-01-08 10:49:52 +00:00
|
|
|
reset($data);
|
2012-01-15 03:47:44 +00:00
|
|
|
$path .= $key . $separator;
|
|
|
|
} else {
|
|
|
|
$result[$path . $key] = $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($data) && !empty($stack)) {
|
|
|
|
list($data, $path) = array_pop($stack);
|
2013-01-08 10:49:52 +00:00
|
|
|
reset($data);
|
2012-01-15 03:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-03-18 23:00:25 +00:00
|
|
|
/**
|
2013-10-19 15:54:43 +00:00
|
|
|
* Expands a flat array to a nested array.
|
2012-03-18 23:00:25 +00:00
|
|
|
*
|
|
|
|
* For example, unflattens an array that was collapsed with `Hash::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
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::expand
|
2012-03-18 23:00:25 +00:00
|
|
|
*/
|
|
|
|
public static function expand($data, $separator = '.') {
|
|
|
|
$result = array();
|
2014-10-01 18:22:44 +00:00
|
|
|
|
|
|
|
$stack = array();
|
|
|
|
|
2012-03-18 23:00:25 +00:00
|
|
|
foreach ($data as $flat => $value) {
|
|
|
|
$keys = explode($separator, $flat);
|
|
|
|
$keys = array_reverse($keys);
|
|
|
|
$child = array(
|
|
|
|
$keys[0] => $value
|
|
|
|
);
|
|
|
|
array_shift($keys);
|
|
|
|
foreach ($keys as $k) {
|
|
|
|
$child = array(
|
|
|
|
$k => $child
|
|
|
|
);
|
|
|
|
}
|
2014-10-01 18:22:44 +00:00
|
|
|
|
|
|
|
$stack[] = array($child, &$result);
|
|
|
|
|
|
|
|
while (!empty($stack)) {
|
|
|
|
foreach ($stack as $curKey => &$curMerge) {
|
|
|
|
foreach ($curMerge[0] as $key => &$val) {
|
2014-10-01 19:40:22 +00:00
|
|
|
if (!empty($curMerge[1][$key]) && (array)$curMerge[1][$key] === $curMerge[1][$key] && (array)$val === $val) {
|
2014-10-01 18:22:44 +00:00
|
|
|
$stack[] = array(&$val, &$curMerge[1][$key]);
|
2014-10-01 19:40:22 +00:00
|
|
|
} elseif ((int)$key === $key && isset($curMerge[1][$key])) {
|
2014-10-01 18:22:44 +00:00
|
|
|
$curMerge[1][] = $val;
|
2014-10-01 19:40:22 +00:00
|
|
|
} else {
|
2014-10-01 18:22:44 +00:00
|
|
|
$curMerge[1][$key] = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($stack[$curKey]);
|
|
|
|
}
|
|
|
|
unset($curMerge);
|
|
|
|
}
|
2012-03-18 23:00:25 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-01-16 02:38:13 +00:00
|
|
|
/**
|
|
|
|
* This function can be thought of as a hybrid between PHP's `array_merge` and `array_merge_recursive`.
|
|
|
|
*
|
2012-03-27 02:57:50 +00:00
|
|
|
* The difference between this method and the built-in ones, is that if an array key contains another array, then
|
2012-12-22 22:48:15 +00:00
|
|
|
* Hash::merge() will behave in a recursive fashion (unlike `array_merge`). But it will not act recursively for
|
2012-01-16 02:38:13 +00:00
|
|
|
* keys that contain scalar values (unlike `array_merge_recursive`).
|
|
|
|
*
|
|
|
|
* Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
|
|
|
|
*
|
|
|
|
* @param array $data Array to be merged
|
|
|
|
* @param mixed $merge Array to merge with. The argument and all trailing arguments will be array cast when merged
|
|
|
|
* @return array Merged array
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::merge
|
2012-01-16 02:38:13 +00:00
|
|
|
*/
|
2012-01-12 02:59:56 +00:00
|
|
|
public static function merge(array $data, $merge) {
|
2014-10-01 18:22:44 +00:00
|
|
|
$args = array_slice(func_get_args(), 1);
|
|
|
|
$return = $data;
|
|
|
|
|
|
|
|
foreach ($args as &$curArg) {
|
|
|
|
$stack[] = array((array)$curArg, &$return);
|
|
|
|
}
|
|
|
|
unset($curArg);
|
|
|
|
|
|
|
|
while (!empty($stack)) {
|
|
|
|
foreach ($stack as $curKey => &$curMerge) {
|
|
|
|
foreach ($curMerge[0] as $key => &$val) {
|
2014-10-01 19:40:22 +00:00
|
|
|
if (!empty($curMerge[1][$key]) && (array)$curMerge[1][$key] === $curMerge[1][$key] && (array)$val === $val) {
|
2014-10-01 18:22:44 +00:00
|
|
|
$stack[] = array(&$val, &$curMerge[1][$key]);
|
2014-10-01 19:40:22 +00:00
|
|
|
} elseif ((int)$key === $key && isset($curMerge[1][$key])) {
|
2014-10-01 18:22:44 +00:00
|
|
|
$curMerge[1][] = $val;
|
2014-10-01 19:40:22 +00:00
|
|
|
} else {
|
2014-10-01 18:22:44 +00:00
|
|
|
$curMerge[1][$key] = $val;
|
|
|
|
}
|
2012-01-16 02:38:13 +00:00
|
|
|
}
|
2014-10-01 18:22:44 +00:00
|
|
|
unset($stack[$curKey]);
|
2012-01-16 02:38:13 +00:00
|
|
|
}
|
2014-10-01 18:22:44 +00:00
|
|
|
unset($curMerge);
|
2012-01-16 02:38:13 +00:00
|
|
|
}
|
|
|
|
return $return;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 03:11:22 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if all the values in the array are numeric
|
|
|
|
*
|
2014-05-28 16:39:54 +00:00
|
|
|
* @param array $data The array to check.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if values are numeric, false otherwise
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::numeric
|
2012-01-22 03:11:22 +00:00
|
|
|
*/
|
|
|
|
public static function numeric(array $data) {
|
|
|
|
if (empty($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-12 22:41:38 +00:00
|
|
|
return $data === array_filter($data, 'is_numeric');
|
2012-01-22 03:11:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-15 03:01:08 +00:00
|
|
|
/**
|
2012-03-27 02:57:50 +00:00
|
|
|
* Counts the dimensions of an array.
|
2012-01-15 03:01:08 +00:00
|
|
|
* Only considers the dimension of the first element in the array.
|
|
|
|
*
|
2017-10-06 09:15:52 +00:00
|
|
|
* If you have an un-even or heterogeneous array, consider using Hash::maxDimensions()
|
2012-01-15 03:01:08 +00:00
|
|
|
* to get the dimensions of the array.
|
|
|
|
*
|
2014-05-28 16:39:54 +00:00
|
|
|
* @param array $data Array to count dimensions on
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int The number of dimensions in $data
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::dimensions
|
2012-01-15 03:01:08 +00:00
|
|
|
*/
|
2012-01-12 02:59:56 +00:00
|
|
|
public static function dimensions(array $data) {
|
2012-01-15 03:01:08 +00:00
|
|
|
if (empty($data)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
reset($data);
|
|
|
|
$depth = 1;
|
|
|
|
while ($elem = array_shift($data)) {
|
|
|
|
if (is_array($elem)) {
|
|
|
|
$depth += 1;
|
|
|
|
$data =& $elem;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $depth;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-15 03:01:08 +00:00
|
|
|
/**
|
|
|
|
* Counts the dimensions of *all* array elements. Useful for finding the maximum
|
|
|
|
* number of dimensions in a mixed array.
|
2012-03-27 02:57:50 +00:00
|
|
|
*
|
2012-01-15 03:01:08 +00:00
|
|
|
* @param array $data Array to count dimensions on
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int The maximum number of dimensions in $data
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::maxDimensions
|
2012-01-15 03:01:08 +00:00
|
|
|
*/
|
2015-07-21 02:16:50 +00:00
|
|
|
public static function maxDimensions($data) {
|
|
|
|
$depth = array();
|
2012-01-15 03:01:08 +00:00
|
|
|
if (is_array($data) && reset($data) !== false) {
|
|
|
|
foreach ($data as $value) {
|
2015-07-21 02:16:50 +00:00
|
|
|
$depth[] = static::maxDimensions($value) + 1;
|
2012-01-15 03:01:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 02:21:15 +00:00
|
|
|
return empty($depth) ? 0 : max($depth);
|
2012-01-15 03:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map a callback across all elements in a set.
|
|
|
|
* Can be provided a path to only modify slices of the set.
|
|
|
|
*
|
2012-02-22 03:53:57 +00:00
|
|
|
* @param array $data The data to map over, and extract data out of.
|
|
|
|
* @param string $path The path to extract for mapping over.
|
|
|
|
* @param callable $function The function to call on each extracted value.
|
|
|
|
* @return array An array of the modified values.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::map
|
2012-01-15 03:01:08 +00:00
|
|
|
*/
|
2012-02-22 03:53:57 +00:00
|
|
|
public static function map(array $data, $path, $function) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$values = (array)static::extract($data, $path);
|
2012-02-22 03:53:57 +00:00
|
|
|
return array_map($function, $values);
|
|
|
|
}
|
2012-01-12 02:59:56 +00:00
|
|
|
|
2012-02-22 03:53:57 +00:00
|
|
|
/**
|
|
|
|
* Reduce a set of extracted values using `$function`.
|
|
|
|
*
|
|
|
|
* @param array $data The data to reduce.
|
|
|
|
* @param string $path The path to extract from $data.
|
2012-11-28 22:30:47 +00:00
|
|
|
* @param callable $function The function to call on each extracted value.
|
2012-02-22 03:53:57 +00:00
|
|
|
* @return mixed The reduced value.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::reduce
|
2012-02-22 03:53:57 +00:00
|
|
|
*/
|
|
|
|
public static function reduce(array $data, $path, $function) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$values = (array)static::extract($data, $path);
|
2012-02-22 03:53:57 +00:00
|
|
|
return array_reduce($values, $function);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply a callback to a set of extracted values using `$function`.
|
|
|
|
* The function will get the extracted values as the first argument.
|
|
|
|
*
|
2012-12-17 04:03:43 +00:00
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* You can easily count the results of an extract using apply().
|
|
|
|
* For example to count the comments on an Article:
|
|
|
|
*
|
2017-10-06 09:15:52 +00:00
|
|
|
* ```
|
|
|
|
* $count = Hash::apply($data, 'Article.Comment.{n}', 'count');
|
|
|
|
* ```
|
2012-12-17 04:03:43 +00:00
|
|
|
*
|
|
|
|
* You could also use a function like `array_sum` to sum the results.
|
|
|
|
*
|
2017-10-06 09:15:52 +00:00
|
|
|
* ```
|
|
|
|
* $total = Hash::apply($data, '{n}.Item.price', 'array_sum');
|
|
|
|
* ```
|
2012-12-17 04:03:43 +00:00
|
|
|
*
|
2012-02-22 03:53:57 +00:00
|
|
|
* @param array $data The data to reduce.
|
|
|
|
* @param string $path The path to extract from $data.
|
2012-11-28 22:30:47 +00:00
|
|
|
* @param callable $function The function to call on each extracted value.
|
2012-02-22 03:53:57 +00:00
|
|
|
* @return mixed The results of the applied method.
|
|
|
|
*/
|
|
|
|
public static function apply(array $data, $path, $function) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$values = (array)static::extract($data, $path);
|
2012-02-22 03:53:57 +00:00
|
|
|
return call_user_func($function, $values);
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 01:34:43 +00:00
|
|
|
/**
|
2016-02-23 12:09:03 +00:00
|
|
|
* Sorts an array by any value, determined by a Hash-compatible path
|
2012-01-26 01:34:43 +00:00
|
|
|
*
|
2012-04-02 03:09:15 +00:00
|
|
|
* ### Sort directions
|
|
|
|
*
|
|
|
|
* - `asc` Sort ascending.
|
|
|
|
* - `desc` Sort descending.
|
|
|
|
*
|
2017-10-06 09:15:52 +00:00
|
|
|
* ### Sort types
|
2012-04-02 03:09:15 +00:00
|
|
|
*
|
2013-01-24 23:04:47 +00:00
|
|
|
* - `regular` For regular sorting (don't change types)
|
|
|
|
* - `numeric` Compare values numerically
|
|
|
|
* - `string` Compare values as strings
|
2016-08-26 12:32:21 +00:00
|
|
|
* - `locale` Compare items as strings, based on the current locale
|
2013-01-24 23:04:47 +00:00
|
|
|
* - `natural` Compare items as strings using "natural ordering" in a human friendly way.
|
|
|
|
* Will sort foo10 below foo2 as an example. Requires PHP 5.4 or greater or it will fallback to 'regular'
|
2015-08-26 01:39:02 +00:00
|
|
|
*
|
2015-08-13 01:14:08 +00:00
|
|
|
* To do case insensitive sorting, pass the type as an array as follows:
|
2015-08-26 01:39:02 +00:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* array('type' => 'regular', 'ignoreCase' => true)
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* When using the array form, `type` defaults to 'regular'. The `ignoreCase` option
|
|
|
|
* defaults to `false`.
|
2012-04-02 03:09:15 +00:00
|
|
|
*
|
2012-01-26 01:34:43 +00:00
|
|
|
* @param array $data An array of data to sort
|
2016-02-23 12:09:03 +00:00
|
|
|
* @param string $path A Hash-compatible path to the array value
|
2014-05-30 14:13:09 +00:00
|
|
|
* @param string $dir See directions above. Defaults to 'asc'.
|
2015-08-26 01:39:02 +00:00
|
|
|
* @param array|string $type See direction types above. Defaults to 'regular'.
|
2012-01-26 01:34:43 +00:00
|
|
|
* @return array Sorted array of data
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort
|
2012-01-26 01:34:43 +00:00
|
|
|
*/
|
2015-08-13 01:14:08 +00:00
|
|
|
public static function sort(array $data, $path, $dir = 'asc', $type = 'regular') {
|
2012-11-30 18:05:14 +00:00
|
|
|
if (empty($data)) {
|
|
|
|
return array();
|
|
|
|
}
|
2012-01-26 01:34:43 +00:00
|
|
|
$originalKeys = array_keys($data);
|
2012-03-14 01:56:48 +00:00
|
|
|
$numeric = is_numeric(implode('', $originalKeys));
|
|
|
|
if ($numeric) {
|
2012-01-26 01:34:43 +00:00
|
|
|
$data = array_values($data);
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
$sortValues = static::extract($data, $path);
|
2012-02-20 04:05:38 +00:00
|
|
|
$sortCount = count($sortValues);
|
|
|
|
$dataCount = count($data);
|
|
|
|
|
|
|
|
// Make sortValues match the data length, as some keys could be missing
|
|
|
|
// the sorted value path.
|
|
|
|
if ($sortCount < $dataCount) {
|
|
|
|
$sortValues = array_pad($sortValues, $dataCount, null);
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
$result = static::_squash($sortValues);
|
|
|
|
$keys = static::extract($result, '{n}.id');
|
|
|
|
$values = static::extract($result, '{n}.value');
|
2012-01-26 01:34:43 +00:00
|
|
|
|
|
|
|
$dir = strtolower($dir);
|
2015-08-13 01:14:08 +00:00
|
|
|
$ignoreCase = false;
|
2015-08-26 01:39:02 +00:00
|
|
|
|
2015-08-13 01:14:08 +00:00
|
|
|
// $type can be overloaded for case insensitive sort
|
|
|
|
if (is_array($type)) {
|
2015-08-26 01:39:02 +00:00
|
|
|
$type += array('ignoreCase' => false, 'type' => 'regular');
|
|
|
|
$ignoreCase = $type['ignoreCase'];
|
|
|
|
$type = $type['type'];
|
2015-08-13 01:14:08 +00:00
|
|
|
} else {
|
|
|
|
$type = strtolower($type);
|
|
|
|
}
|
|
|
|
|
2015-08-14 05:32:23 +00:00
|
|
|
if ($type === 'natural' && version_compare(PHP_VERSION, '5.4.0', '<')) {
|
2015-08-13 01:14:08 +00:00
|
|
|
$type = 'regular';
|
2012-04-04 16:21:52 +00:00
|
|
|
}
|
2015-08-12 04:30:08 +00:00
|
|
|
|
2012-01-26 01:34:43 +00:00
|
|
|
if ($dir === 'asc') {
|
|
|
|
$dir = SORT_ASC;
|
2012-04-02 03:09:15 +00:00
|
|
|
} else {
|
2012-01-26 01:34:43 +00:00
|
|
|
$dir = SORT_DESC;
|
|
|
|
}
|
2012-04-02 03:09:15 +00:00
|
|
|
if ($type === 'numeric') {
|
|
|
|
$type = SORT_NUMERIC;
|
|
|
|
} elseif ($type === 'string') {
|
|
|
|
$type = SORT_STRING;
|
|
|
|
} elseif ($type === 'natural') {
|
|
|
|
$type = SORT_NATURAL;
|
2016-08-26 12:32:21 +00:00
|
|
|
} elseif ($type === 'locale') {
|
|
|
|
$type = SORT_LOCALE_STRING;
|
2012-04-02 03:09:15 +00:00
|
|
|
} else {
|
|
|
|
$type = SORT_REGULAR;
|
|
|
|
}
|
2015-08-12 04:30:08 +00:00
|
|
|
|
|
|
|
if ($ignoreCase) {
|
2015-08-26 01:49:10 +00:00
|
|
|
$values = array_map('mb_strtolower', $values);
|
2015-08-12 04:30:08 +00:00
|
|
|
}
|
|
|
|
array_multisort($values, $dir, $type, $keys, $dir);
|
|
|
|
|
2012-01-26 01:34:43 +00:00
|
|
|
$sorted = array();
|
|
|
|
$keys = array_unique($keys);
|
2012-01-12 02:59:56 +00:00
|
|
|
|
2012-01-26 01:34:43 +00:00
|
|
|
foreach ($keys as $k) {
|
2012-03-14 01:56:48 +00:00
|
|
|
if ($numeric) {
|
|
|
|
$sorted[] = $data[$k];
|
|
|
|
continue;
|
2012-03-27 02:57:50 +00:00
|
|
|
}
|
2012-03-14 01:56:48 +00:00
|
|
|
if (isset($originalKeys[$k])) {
|
|
|
|
$sorted[$originalKeys[$k]] = $data[$originalKeys[$k]];
|
|
|
|
} else {
|
|
|
|
$sorted[$k] = $data[$k];
|
|
|
|
}
|
2012-01-26 01:34:43 +00:00
|
|
|
}
|
|
|
|
return $sorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for sort()
|
2013-03-05 07:05:14 +00:00
|
|
|
* Squashes an array to a single hash so it can be sorted.
|
2012-01-26 01:34:43 +00:00
|
|
|
*
|
|
|
|
* @param array $data The data to squash.
|
|
|
|
* @param string $key The key for the data.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected static function _squash($data, $key = null) {
|
|
|
|
$stack = array();
|
|
|
|
foreach ($data as $k => $r) {
|
|
|
|
$id = $k;
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($key !== null) {
|
2012-01-26 01:34:43 +00:00
|
|
|
$id = $key;
|
|
|
|
}
|
|
|
|
if (is_array($r) && !empty($r)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$stack = array_merge($stack, static::_squash($r, $id));
|
2012-01-26 01:34:43 +00:00
|
|
|
} else {
|
|
|
|
$stack[] = array('id' => $id, 'value' => $r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $stack;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 02:38:13 +00:00
|
|
|
/**
|
|
|
|
* Computes the difference between two complex arrays.
|
|
|
|
* This method differs from the built-in array_diff() in that it will preserve keys
|
|
|
|
* and work on multi-dimensional arrays.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $data First value
|
|
|
|
* @param array $compare Second value
|
2012-03-12 02:52:55 +00:00
|
|
|
* @return array Returns the key => value pairs that are not common in $data and $compare
|
|
|
|
* The expression for this function is ($data - $compare) + ($compare - ($data - $compare))
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::diff
|
2012-01-16 02:38:13 +00:00
|
|
|
*/
|
2012-03-12 02:52:55 +00:00
|
|
|
public static function diff(array $data, $compare) {
|
2012-01-16 02:38:13 +00:00
|
|
|
if (empty($data)) {
|
2012-03-12 02:52:55 +00:00
|
|
|
return (array)$compare;
|
2012-01-16 02:38:13 +00:00
|
|
|
}
|
2012-03-12 02:52:55 +00:00
|
|
|
if (empty($compare)) {
|
2012-01-16 02:38:13 +00:00
|
|
|
return (array)$data;
|
|
|
|
}
|
2012-03-12 02:52:55 +00:00
|
|
|
$intersection = array_intersect_key($data, $compare);
|
2012-01-16 02:38:13 +00:00
|
|
|
while (($key = key($intersection)) !== null) {
|
2012-03-12 02:52:55 +00:00
|
|
|
if ($data[$key] == $compare[$key]) {
|
2012-01-16 02:38:13 +00:00
|
|
|
unset($data[$key]);
|
2012-03-12 02:52:55 +00:00
|
|
|
unset($compare[$key]);
|
2012-01-16 02:38:13 +00:00
|
|
|
}
|
|
|
|
next($intersection);
|
|
|
|
}
|
2012-03-12 02:52:55 +00:00
|
|
|
return $data + $compare;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-01 00:12:29 +00:00
|
|
|
* Merges the difference between $data and $compare onto $data.
|
2012-03-12 02:52:55 +00:00
|
|
|
*
|
|
|
|
* @param array $data The data to append onto.
|
|
|
|
* @param array $compare The data to compare and append onto.
|
|
|
|
* @return array The merged array.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::mergeDiff
|
2012-03-12 02:52:55 +00:00
|
|
|
*/
|
|
|
|
public static function mergeDiff(array $data, $compare) {
|
|
|
|
if (empty($data) && !empty($compare)) {
|
|
|
|
return $compare;
|
|
|
|
}
|
|
|
|
if (empty($compare)) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
foreach ($compare as $key => $value) {
|
|
|
|
if (!array_key_exists($key, $data)) {
|
|
|
|
$data[$key] = $value;
|
|
|
|
} elseif (is_array($value)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$data[$key] = static::mergeDiff($data[$key], $compare[$key]);
|
2012-03-12 02:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 02:42:54 +00:00
|
|
|
/**
|
|
|
|
* Normalizes an array, and converts it to a standard format.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $data List to normalize
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $assoc If true, $data will be converted to an associative array.
|
2012-01-16 02:42:54 +00:00
|
|
|
* @return array
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::normalize
|
2012-01-16 02:42:54 +00:00
|
|
|
*/
|
2012-01-12 02:59:56 +00:00
|
|
|
public static function normalize(array $data, $assoc = true) {
|
2012-01-16 02:42:54 +00:00
|
|
|
$keys = array_keys($data);
|
|
|
|
$count = count($keys);
|
|
|
|
$numeric = true;
|
|
|
|
|
|
|
|
if (!$assoc) {
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
if (!is_int($keys[$i])) {
|
|
|
|
$numeric = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$numeric || $assoc) {
|
|
|
|
$newList = array();
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
if (is_int($keys[$i])) {
|
|
|
|
$newList[$data[$keys[$i]]] = null;
|
|
|
|
} else {
|
|
|
|
$newList[$keys[$i]] = $data[$keys[$i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$data = $newList;
|
|
|
|
}
|
|
|
|
return $data;
|
2012-01-12 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 02:40:48 +00:00
|
|
|
/**
|
|
|
|
* Takes in a flat array and returns a nested array
|
|
|
|
*
|
2012-02-23 02:53:33 +00:00
|
|
|
* ### Options:
|
|
|
|
*
|
|
|
|
* - `children` The key name to use in the resultset for children.
|
|
|
|
* - `idPath` The path to a key that identifies each entry. Should be
|
2012-02-28 21:52:54 +00:00
|
|
|
* compatible with Hash::extract(). Defaults to `{n}.$alias.id`
|
2012-02-23 02:53:33 +00:00
|
|
|
* - `parentPath` The path to a key that identifies the parent of each entry.
|
2012-02-28 21:52:54 +00:00
|
|
|
* Should be compatible with Hash::extract(). Defaults to `{n}.$alias.parent_id`
|
2012-02-23 02:53:33 +00:00
|
|
|
* - `root` The id of the desired top-most result.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $data The data to nest.
|
2012-02-23 02:40:48 +00:00
|
|
|
* @param array $options Options are:
|
|
|
|
* @return array of results, nested
|
2012-03-11 01:57:18 +00:00
|
|
|
* @see Hash::extract()
|
2014-07-08 11:01:02 +00:00
|
|
|
* @throws InvalidArgumentException When providing invalid data.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::nest
|
2012-02-23 02:40:48 +00:00
|
|
|
*/
|
2012-02-23 02:53:33 +00:00
|
|
|
public static function nest(array $data, $options = array()) {
|
2012-02-23 02:40:48 +00:00
|
|
|
if (!$data) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
$alias = key(current($data));
|
|
|
|
$options += array(
|
|
|
|
'idPath' => "{n}.$alias.id",
|
|
|
|
'parentPath' => "{n}.$alias.parent_id",
|
|
|
|
'children' => 'children',
|
|
|
|
'root' => null
|
|
|
|
);
|
|
|
|
|
|
|
|
$return = $idMap = array();
|
2015-07-21 08:22:53 +00:00
|
|
|
$ids = static::extract($data, $options['idPath']);
|
2012-02-23 02:40:48 +00:00
|
|
|
|
|
|
|
$idKeys = explode('.', $options['idPath']);
|
|
|
|
array_shift($idKeys);
|
|
|
|
|
|
|
|
$parentKeys = explode('.', $options['parentPath']);
|
|
|
|
array_shift($parentKeys);
|
|
|
|
|
|
|
|
foreach ($data as $result) {
|
|
|
|
$result[$options['children']] = array();
|
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
$id = static::get($result, $idKeys);
|
|
|
|
$parentId = static::get($result, $parentKeys);
|
2012-02-23 02:40:48 +00:00
|
|
|
|
|
|
|
if (isset($idMap[$id][$options['children']])) {
|
|
|
|
$idMap[$id] = array_merge($result, (array)$idMap[$id]);
|
|
|
|
} else {
|
|
|
|
$idMap[$id] = array_merge($result, array($options['children'] => array()));
|
|
|
|
}
|
|
|
|
if (!$parentId || !in_array($parentId, $ids)) {
|
|
|
|
$return[] =& $idMap[$id];
|
|
|
|
} else {
|
|
|
|
$idMap[$parentId][$options['children']][] =& $idMap[$id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 11:01:02 +00:00
|
|
|
if (!$return) {
|
|
|
|
throw new InvalidArgumentException(__d('cake_dev',
|
|
|
|
'Invalid data array to nest.'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-02-23 02:40:48 +00:00
|
|
|
if ($options['root']) {
|
|
|
|
$root = $options['root'];
|
|
|
|
} else {
|
2015-07-21 08:22:53 +00:00
|
|
|
$root = static::get($return[0], $parentKeys);
|
2012-02-23 02:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($return as $i => $result) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$id = static::get($result, $idKeys);
|
|
|
|
$parentId = static::get($result, $parentKeys);
|
2012-02-23 02:40:48 +00:00
|
|
|
if ($id !== $root && $parentId != $root) {
|
|
|
|
unset($return[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_values($return);
|
|
|
|
}
|
|
|
|
|
2015-08-26 01:39:02 +00:00
|
|
|
}
|