Update to CakePHP 2.8.0 copy in lib folder

This commit is contained in:
SteveGilvarry committed 2016-02-25 23:25:24 +11:00
1 parent 28e94764d7
commit b294f210dc
443 files changed
+19245 -7546

No files matched your search

+166 -93
View File
@@ -14,7 +14,7 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('String', 'Utility');
App::uses('CakeText', 'Utility');
/**
* Library of array functions for manipulating and extracting data
@@ -38,18 +38,26 @@ class Hash {
* @param string|array $path The path being searched for. Either a dot
* separated string, or an array of path segments.
* @param mixed $default The return value when the path does not exist
* @throws InvalidArgumentException
* @return mixed The value fetched from the array, or null.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::get
*/
public static function get(array $data, $path, $default = null) {
if (empty($data)) {
if (empty($data) || $path === '' || $path === null) {
return $default;
}
if (is_string($path) || is_numeric($path)) {
$parts = explode('.', $path);
} else {
if (!is_array($path)) {
throw new InvalidArgumentException(__d('cake_dev',
'Invalid Parameter %s, should be dot separated path or array.',
$path
));
}
$parts = $path;
}
foreach ($parts as $key) {
if (is_array($data) && isset($data[$key])) {
$data =& $data[$key];
@@ -57,6 +65,7 @@ class Hash {
return $default;
}
}
return $data;
}
@@ -67,6 +76,7 @@ class Hash {
*
* - `{n}` Matches any numeric key, or integer.
* - `{s}` Matches any string key.
* - `{*}` Matches any value.
* - `Foo` Matches any key with the exact same value.
*
* There are a number of attribute operators:
@@ -96,13 +106,13 @@ class Hash {
// Simple paths.
if (!preg_match('/[{\[]/', $path)) {
return (array)self::get($data, $path);
return (array)static::get($data, $path);
}
if (strpos($path, '[') === false) {
$tokens = explode('.', $path);
} else {
$tokens = String::tokenize($path, '.', '[', ']');
$tokens = CakeText::tokenize($path, '.', '[', ']');
}
$_key = '__set_item__';
@@ -112,11 +122,11 @@ class Hash {
foreach ($tokens as $token) {
$next = array();
list($token, $conditions) = self::_splitConditions($token);
list($token, $conditions) = static::_splitConditions($token);
foreach ($context[$_key] as $item) {
foreach ((array)$item as $k => $v) {
if (self::_matchToken($k, $token)) {
if (static::_matchToken($k, $token)) {
$next[] = $v;
}
}
@@ -126,7 +136,7 @@ class Hash {
if ($conditions) {
$filter = array();
foreach ($next as $item) {
if (is_array($item) && self::_matches($item, $conditions)) {
if (is_array($item) && static::_matches($item, $conditions)) {
$filter[] = $item;
}
}
@@ -159,19 +169,19 @@ class Hash {
*
* @param string $key The key in the array being searched.
* @param string $token The token being matched.
* @return boolean
* @return bool
*/
protected static function _matchToken($key, $token) {
if ($token === '{n}') {
return is_numeric($key);
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;
}
if ($token === '{s}') {
return is_string($key);
}
if (is_numeric($token)) {
return ($key == $token);
}
return ($key === $token);
}
/**
@@ -179,7 +189,7 @@ class Hash {
*
* @param array $data Array of data to match.
* @param string $selector The patterns to match.
* @return boolean Fitness of expression.
* @return bool Fitness of expression.
*/
protected static function _matches(array $data, $selector) {
preg_match_all(
@@ -208,7 +218,10 @@ class Hash {
if (isset($data[$attr])) {
$prop = $data[$attr];
}
if ($prop === true || $prop === false) {
$isBool = is_bool($prop);
if ($isBool && is_numeric($val)) {
$prop = $prop ? '1' : '0';
} elseif ($isBool) {
$prop = $prop ? 'true' : 'false';
}
@@ -217,8 +230,7 @@ class Hash {
if (!preg_match($val, $prop)) {
return false;
}
} elseif (
($op === '=' && $prop != $val) ||
} elseif (($op === '=' && $prop != $val) ||
($op === '!=' && $prop == $val) ||
($op === '>' && $prop <= $val) ||
($op === '<' && $prop >= $val) ||
@@ -238,7 +250,7 @@ class Hash {
*
* @param array $data The data to insert into.
* @param string $path The path to insert at.
* @param array $values The values to insert.
* @param mixed $values The values to insert.
* @return array The data with $values inserted.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::insert
*/
@@ -246,26 +258,26 @@ class Hash {
if (strpos($path, '[') === false) {
$tokens = explode('.', $path);
} else {
$tokens = String::tokenize($path, '.', '[', ']');
$tokens = CakeText::tokenize($path, '.', '[', ']');
}
if (strpos($path, '{') === false && strpos($path, '[') === false) {
return self::_simpleOp('insert', $data, $tokens, $values);
return static::_simpleOp('insert', $data, $tokens, $values);
}
$token = array_shift($tokens);
$nextPath = implode('.', $tokens);
list($token, $conditions) = self::_splitConditions($token);
list($token, $conditions) = static::_splitConditions($token);
foreach ($data as $k => $v) {
if (self::_matchToken($k, $token)) {
if ($conditions && self::_matches($v, $conditions)) {
if (static::_matchToken($k, $token)) {
if ($conditions && static::_matches($v, $conditions)) {
$data[$k] = array_merge($v, $values);
continue;
}
if (!$conditions) {
$data[$k] = self::insert($v, $nextPath, $values);
$data[$k] = static::insert($v, $nextPath, $values);
}
}
}
@@ -279,7 +291,7 @@ class Hash {
* @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.
* @return array $data.
* @return array data.
*/
protected static function _simpleOp($op, $data, $path, $values = null) {
$_list =& $data;
@@ -287,8 +299,8 @@ class Hash {
$count = count($path);
$last = $count - 1;
foreach ($path as $i => $key) {
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
$key = intval($key);
if ((is_numeric($key) && intval($key) > 0 || $key === '0') && strpos($key, '0') !== 0) {
$key = (int)$key;
}
if ($op === 'insert') {
if ($i === $last) {
@@ -329,30 +341,30 @@ class Hash {
if (strpos($path, '[') === false) {
$tokens = explode('.', $path);
} else {
$tokens = String::tokenize($path, '.', '[', ']');
$tokens = CakeText::tokenize($path, '.', '[', ']');
}
if (strpos($path, '{') === false && strpos($path, '[') === false) {
return self::_simpleOp('remove', $data, $tokens);
return static::_simpleOp('remove', $data, $tokens);
}
$token = array_shift($tokens);
$nextPath = implode('.', $tokens);
list($token, $conditions) = self::_splitConditions($token);
list($token, $conditions) = static::_splitConditions($token);
foreach ($data as $k => $v) {
$match = self::_matchToken($k, $token);
$match = static::_matchToken($k, $token);
if ($match && is_array($v)) {
if ($conditions && self::_matches($v, $conditions)) {
if ($conditions && static::_matches($v, $conditions)) {
unset($data[$k]);
continue;
}
$data[$k] = self::remove($v, $nextPath);
$data[$k] = static::remove($v, $nextPath);
if (empty($data[$k])) {
unset($data[$k]);
}
} elseif ($match) {
} elseif ($match && empty($nextPath)) {
unset($data[$k]);
}
}
@@ -380,9 +392,9 @@ class Hash {
if (is_array($keyPath)) {
$format = array_shift($keyPath);
$keys = self::format($data, $keyPath, $format);
$keys = static::format($data, $keyPath, $format);
} else {
$keys = self::extract($data, $keyPath);
$keys = static::extract($data, $keyPath);
}
if (empty($keys)) {
return array();
@@ -390,9 +402,9 @@ class Hash {
if (!empty($valuePath) && is_array($valuePath)) {
$format = array_shift($valuePath);
$vals = self::format($data, $valuePath, $format);
$vals = static::format($data, $valuePath, $format);
} elseif (!empty($valuePath)) {
$vals = self::extract($data, $valuePath);
$vals = static::extract($data, $valuePath);
}
if (empty($vals)) {
$vals = array_fill(0, count($keys), null);
@@ -406,7 +418,7 @@ class Hash {
}
if ($groupPath !== null) {
$group = self::extract($data, $groupPath);
$group = static::extract($data, $groupPath);
if (!empty($group)) {
$c = count($keys);
for ($i = 0; $i < $c; $i++) {
@@ -433,9 +445,9 @@ class Hash {
*
* Usage:
*
* {{{
* ```
* $result = Hash::format($users, array('{n}.User.id', '{n}.User.name'), '%s : %s');
* }}}
* ```
*
* The `$format` string can use any format options that `vsprintf()` and `sprintf()` do.
*
@@ -453,11 +465,11 @@ class Hash {
$count = count($paths);
if (!$count) {
return;
return null;
}
for ($i = 0; $i < $count; $i++) {
$extracted[] = self::extract($data, $paths[$i]);
$extracted[] = static::extract($data, $paths[$i]);
}
$out = array();
$data = $extracted;
@@ -481,7 +493,7 @@ class Hash {
*
* @param array $data The data to search through.
* @param array $needle The values to file in $data
* @return boolean true if $data contains $needle, false otherwise
* @return bool true if $data contains $needle, false otherwise
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::contains
*/
public static function contains(array $data, array $needle) {
@@ -522,12 +534,12 @@ class Hash {
*
* @param array $data The data to check.
* @param string $path The path to check for.
* @return boolean Existence of path.
* @return bool Existence of path.
* @see Hash::extract()
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::check
*/
public static function check(array $data, $path) {
$results = self::extract($data, $path);
$results = static::extract($data, $path);
if (!is_array($results)) {
return false;
}
@@ -539,14 +551,14 @@ class Hash {
*
* @param array $data Either an array to filter, or value when in callback
* @param callable $callback A function to filter the data with. Defaults to
* `self::_filter()` Which strips out all non-zero empty values.
* `static::_filter()` Which strips out all non-zero empty values.
* @return array Filtered array
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::filter
*/
public static function filter(array $data, $callback = array('self', '_filter')) {
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = self::filter($v, $callback);
$data[$k] = static::filter($v, $callback);
}
}
return array_filter($data, $callback);
@@ -556,7 +568,7 @@ class Hash {
* Callback function for filtering.
*
* @param array $var Array to filter.
* @return boolean
* @return bool
*/
protected static function _filter($var) {
if ($var === 0 || $var === '0' || !empty($var)) {
@@ -619,6 +631,9 @@ class Hash {
*/
public static function expand($data, $separator = '.') {
$result = array();
$stack = array();
foreach ($data as $flat => $value) {
$keys = explode($separator, $flat);
$keys = array_reverse($keys);
@@ -631,7 +646,24 @@ class Hash {
$k => $child
);
}
$result = self::merge($result, $child);
$stack[] = array($child, &$result);
while (!empty($stack)) {
foreach ($stack as $curKey => &$curMerge) {
foreach ($curMerge[0] as $key => &$val) {
if (!empty($curMerge[1][$key]) && (array)$curMerge[1][$key] === $curMerge[1][$key] && (array)$val === $val) {
$stack[] = array(&$val, &$curMerge[1][$key]);
} elseif ((int)$key === $key && isset($curMerge[1][$key])) {
$curMerge[1][] = $val;
} else {
$curMerge[1][$key] = $val;
}
}
unset($stack[$curKey]);
}
unset($curMerge);
}
}
return $result;
}
@@ -651,19 +683,28 @@ class Hash {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::merge
*/
public static function merge(array $data, $merge) {
$args = func_get_args();
$return = current($args);
$args = array_slice(func_get_args(), 1);
$return = $data;
while (($arg = next($args)) !== false) {
foreach ((array)$arg as $key => $val) {
if (!empty($return[$key]) && is_array($return[$key]) && is_array($val)) {
$return[$key] = self::merge($return[$key], $val);
} elseif (is_int($key) && isset($return[$key])) {
$return[] = $val;
} else {
$return[$key] = $val;
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) {
if (!empty($curMerge[1][$key]) && (array)$curMerge[1][$key] === $curMerge[1][$key] && (array)$val === $val) {
$stack[] = array(&$val, &$curMerge[1][$key]);
} elseif ((int)$key === $key && isset($curMerge[1][$key])) {
$curMerge[1][] = $val;
} else {
$curMerge[1][$key] = $val;
}
}
unset($stack[$curKey]);
}
unset($curMerge);
}
return $return;
}
@@ -671,8 +712,8 @@ class Hash {
/**
* Checks to see if all the values in the array are numeric
*
* @param array $array The array to check.
* @return boolean true if values are numeric, false otherwise
* @param array $data The array to check.
* @return bool true if values are numeric, false otherwise
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::numeric
*/
public static function numeric(array $data) {
@@ -689,8 +730,8 @@ class Hash {
* If you have an un-even or heterogenous array, consider using Hash::maxDimensions()
* to get the dimensions of the array.
*
* @param array $array Array to count dimensions on
* @return integer The number of dimensions in $data
* @param array $data Array to count dimensions on
* @return int The number of dimensions in $data
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::dimensions
*/
public static function dimensions(array $data) {
@@ -715,17 +756,17 @@ class Hash {
* number of dimensions in a mixed array.
*
* @param array $data Array to count dimensions on
* @return integer The maximum number of dimensions in $data
* @return int The maximum number of dimensions in $data
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::maxDimensions
*/
public static function maxDimensions(array $data) {
public static function maxDimensions($data) {
$depth = array();
if (is_array($data) && reset($data) !== false) {
foreach ($data as $value) {
$depth[] = self::dimensions((array)$value) + 1;
$depth[] = static::maxDimensions($value) + 1;
}
}
return max($depth);
return empty($depth) ? 0 : max($depth);
}
/**
@@ -739,7 +780,7 @@ class Hash {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::map
*/
public static function map(array $data, $path, $function) {
$values = (array)self::extract($data, $path);
$values = (array)static::extract($data, $path);
return array_map($function, $values);
}
@@ -753,7 +794,7 @@ class Hash {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::reduce
*/
public static function reduce(array $data, $path, $function) {
$values = (array)self::extract($data, $path);
$values = (array)static::extract($data, $path);
return array_reduce($values, $function);
}
@@ -778,7 +819,7 @@ class Hash {
* @return mixed The results of the applied method.
*/
public static function apply(array $data, $path, $function) {
$values = (array)self::extract($data, $path);
$values = (array)static::extract($data, $path);
return call_user_func($function, $values);
}
@@ -798,14 +839,23 @@ class Hash {
* - `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'
*
* To do case insensitive sorting, pass the type as an array as follows:
*
* ```
* array('type' => 'regular', 'ignoreCase' => true)
* ```
*
* When using the array form, `type` defaults to 'regular'. The `ignoreCase` option
* defaults to `false`.
*
* @param array $data An array of data to sort
* @param string $path A Set-compatible path to the array value
* @param string $dir See directions above.
* @param string $type See direction types above. Defaults to 'regular'.
* @param string $dir See directions above. Defaults to 'asc'.
* @param array|string $type See direction types above. Defaults to 'regular'.
* @return array Sorted array of data
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort
*/
public static function sort(array $data, $path, $dir, $type = 'regular') {
public static function sort(array $data, $path, $dir = 'asc', $type = 'regular') {
if (empty($data)) {
return array();
}
@@ -814,7 +864,7 @@ class Hash {
if ($numeric) {
$data = array_values($data);
}
$sortValues = self::extract($data, $path);
$sortValues = static::extract($data, $path);
$sortCount = count($sortValues);
$dataCount = count($data);
@@ -823,15 +873,26 @@ class Hash {
if ($sortCount < $dataCount) {
$sortValues = array_pad($sortValues, $dataCount, null);
}
$result = self::_squash($sortValues);
$keys = self::extract($result, '{n}.id');
$values = self::extract($result, '{n}.value');
$result = static::_squash($sortValues);
$keys = static::extract($result, '{n}.id');
$values = static::extract($result, '{n}.value');
$dir = strtolower($dir);
$type = strtolower($type);
$ignoreCase = false;
// $type can be overloaded for case insensitive sort
if (is_array($type)) {
$type += array('ignoreCase' => false, 'type' => 'regular');
$ignoreCase = $type['ignoreCase'];
$type = $type['type'];
} else {
$type = strtolower($type);
}
if ($type === 'natural' && version_compare(PHP_VERSION, '5.4.0', '<')) {
$type = 'regular';
}
if ($dir === 'asc') {
$dir = SORT_ASC;
} else {
@@ -846,7 +907,12 @@ class Hash {
} else {
$type = SORT_REGULAR;
}
array_multisort($values, $dir, $type, $keys, $dir, $type);
if ($ignoreCase) {
$values = array_map('mb_strtolower', $values);
}
array_multisort($values, $dir, $type, $keys, $dir);
$sorted = array();
$keys = array_unique($keys);
@@ -880,7 +946,7 @@ class Hash {
$id = $key;
}
if (is_array($r) && !empty($r)) {
$stack = array_merge($stack, self::_squash($r, $id));
$stack = array_merge($stack, static::_squash($r, $id));
} else {
$stack[] = array('id' => $id, 'value' => $r);
}
@@ -936,7 +1002,7 @@ class Hash {
if (!array_key_exists($key, $data)) {
$data[$key] = $value;
} elseif (is_array($value)) {
$data[$key] = self::mergeDiff($data[$key], $compare[$key]);
$data[$key] = static::mergeDiff($data[$key], $compare[$key]);
}
}
return $data;
@@ -946,7 +1012,7 @@ class Hash {
* Normalizes an array, and converts it to a standard format.
*
* @param array $data List to normalize
* @param boolean $assoc If true, $data will be converted to an associative array.
* @param bool $assoc If true, $data will be converted to an associative array.
* @return array
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::normalize
*/
@@ -993,6 +1059,7 @@ class Hash {
* @param array $options Options are:
* @return array of results, nested
* @see Hash::extract()
* @throws InvalidArgumentException When providing invalid data.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::nest
*/
public static function nest(array $data, $options = array()) {
@@ -1009,7 +1076,7 @@ class Hash {
);
$return = $idMap = array();
$ids = self::extract($data, $options['idPath']);
$ids = static::extract($data, $options['idPath']);
$idKeys = explode('.', $options['idPath']);
array_shift($idKeys);
@@ -1020,8 +1087,8 @@ class Hash {
foreach ($data as $result) {
$result[$options['children']] = array();
$id = self::get($result, $idKeys);
$parentId = self::get($result, $parentKeys);
$id = static::get($result, $idKeys);
$parentId = static::get($result, $parentKeys);
if (isset($idMap[$id][$options['children']])) {
$idMap[$id] = array_merge($result, (array)$idMap[$id]);
@@ -1035,15 +1102,21 @@ class Hash {
}
}
if (!$return) {
throw new InvalidArgumentException(__d('cake_dev',
'Invalid data array to nest.'
));
}
if ($options['root']) {
$root = $options['root'];
} else {
$root = self::get($return[0], $parentKeys);
$root = static::get($return[0], $parentKeys);
}
foreach ($return as $i => $result) {
$id = self::get($result, $idKeys);
$parentId = self::get($result, $parentKeys);
$id = static::get($result, $idKeys);
$parentId = static::get($result, $parentKeys);
if ($id !== $root && $parentId != $root) {
unset($return[$i]);
}