mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge branch '2.2-hash' into 2.2
Conflicts: lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php lib/Cake/View/Helper/FormHelper.php
This commit is contained in:
commit
9f9feec222
55 changed files with 3359 additions and 408 deletions
|
@ -127,7 +127,7 @@ class IniReader implements ConfigReaderInterface {
|
|||
$value = false;
|
||||
}
|
||||
if (strpos($key, '.') !== false) {
|
||||
$values = Set::insert($values, $key, $value);
|
||||
$values = Hash::insert($values, $key, $value);
|
||||
} else {
|
||||
$values[$key] = $value;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ App::uses('Controller', 'Controller');
|
|||
App::uses('ComponentCollection', 'Controller');
|
||||
App::uses('AclComponent', 'Controller/Component');
|
||||
App::uses('DbAcl', 'Model');
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* Shell for ACL management. This console is known to have issues with zend.ze1_compatibility_mode
|
||||
|
@ -558,8 +559,9 @@ class AclShell extends AppShell {
|
|||
$identifier = var_export($identifier, true);
|
||||
}
|
||||
$this->error(__d('cake_console', 'Could not find node using reference "%s"', $identifier));
|
||||
return;
|
||||
}
|
||||
return Set::extract($node, "0.{$class}.id");
|
||||
return Hash::get($node, "0.{$class}.id");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -300,7 +300,7 @@ class ConsoleShell extends AppShell {
|
|||
break;
|
||||
case (preg_match("/^routes\s+show/i", $command, $tmp) == true):
|
||||
$router = Router::getInstance();
|
||||
$this->out(implode("\n", Set::extract($router->routes, '{n}.0')));
|
||||
$this->out(implode("\n", Hash::extract($router->routes, '{n}.0')));
|
||||
break;
|
||||
case (preg_match("/^route\s+(\(.*\))$/i", $command, $tmp) == true):
|
||||
if ($url = eval('return array' . $tmp[1] . ';')) {
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
App::uses('AppShell', 'Console/Command');
|
||||
App::uses('File', 'Utility');
|
||||
App::uses('Folder', 'Utility');
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* Language string extractor
|
||||
|
@ -431,7 +432,7 @@ class ExtractTask extends AppShell {
|
|||
return;
|
||||
}
|
||||
|
||||
$dims = Set::countDim($rules);
|
||||
$dims = Hash::dimensions($rules);
|
||||
if ($dims == 1 || ($dims == 2 && isset($rules['message']))) {
|
||||
$rules = array($rules);
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
App::uses('AclInterface', 'Controller/Component/Acl');
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* DbAcl implements an ACL control system in the database. ARO's and ACO's are
|
||||
|
|
|
@ -30,7 +30,7 @@ class IniAcl extends Object implements AclInterface {
|
|||
public $config = null;
|
||||
|
||||
/**
|
||||
* The Set::classicExtract() path to the user/aro identifier in the
|
||||
* The Hash::extract() path to the user/aro identifier in the
|
||||
* acl.ini file. This path will be used to extract the string
|
||||
* representation of a user used in the ini file.
|
||||
*
|
||||
|
@ -97,7 +97,7 @@ class IniAcl extends Object implements AclInterface {
|
|||
$aclConfig = $this->config;
|
||||
|
||||
if (is_array($aro)) {
|
||||
$aro = Set::classicExtract($aro, $this->userPath);
|
||||
$aro = Hash::get($aro, $this->userPath);
|
||||
}
|
||||
|
||||
if (isset($aclConfig[$aro]['deny'])) {
|
||||
|
|
|
@ -494,7 +494,7 @@ class PhpAro {
|
|||
// detect cycles
|
||||
$roles = $this->roles($dependency);
|
||||
|
||||
if (in_array($role, Set::flatten($roles))) {
|
||||
if (in_array($role, Hash::flatten($roles))) {
|
||||
$path = '';
|
||||
|
||||
foreach ($roles as $roleDependencies) {
|
||||
|
|
|
@ -12,8 +12,8 @@
|
|||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::uses('Security', 'Utility');
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* Base Authentication class with common methods and properties.
|
||||
|
@ -58,7 +58,7 @@ abstract class BaseAuthenticate {
|
|||
*/
|
||||
public function __construct(ComponentCollection $collection, $settings) {
|
||||
$this->_Collection = $collection;
|
||||
$this->settings = Set::merge($this->settings, $settings);
|
||||
$this->settings = Hash::merge($this->settings, $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* Abstract base authorization adapter for AuthComponent.
|
||||
|
@ -69,7 +70,7 @@ abstract class BaseAuthorize {
|
|||
$this->_Collection = $collection;
|
||||
$controller = $collection->getController();
|
||||
$this->controller($controller);
|
||||
$this->settings = Set::merge($this->settings, $settings);
|
||||
$this->settings = Hash::merge($this->settings, $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -19,11 +19,11 @@
|
|||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
|
||||
App::uses('Component', 'Controller');
|
||||
App::uses('Router', 'Routing');
|
||||
App::uses('Security', 'Utility');
|
||||
App::uses('Debugger', 'Utility');
|
||||
App::uses('Hash', 'Utility');
|
||||
App::uses('CakeSession', 'Model/Datasource');
|
||||
App::uses('BaseAuthorize', 'Controller/Component/Auth');
|
||||
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
|
||||
|
@ -398,7 +398,7 @@ class AuthComponent extends Component {
|
|||
return;
|
||||
}
|
||||
$this->_authorizeObjects = array();
|
||||
$config = Set::normalize($this->authorize);
|
||||
$config = Hash::normalize((array)$this->authorize);
|
||||
$global = array();
|
||||
if (isset($config[AuthComponent::ALL])) {
|
||||
$global = $config[AuthComponent::ALL];
|
||||
|
@ -656,7 +656,7 @@ class AuthComponent extends Component {
|
|||
return;
|
||||
}
|
||||
$this->_authenticateObjects = array();
|
||||
$config = Set::normalize($this->authenticate);
|
||||
$config = Hash::normalize((array)$this->authenticate);
|
||||
$global = array();
|
||||
if (isset($config[AuthComponent::ALL])) {
|
||||
$global = $config[AuthComponent::ALL];
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
App::uses('Component', 'Controller');
|
||||
App::uses('Security', 'Utility');
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* Cookie Component.
|
||||
|
@ -239,7 +240,7 @@ class CookieComponent extends Component {
|
|||
if (!isset($this->_values[$this->name][$names[0]])) {
|
||||
$this->_values[$this->name][$names[0]] = array();
|
||||
}
|
||||
$this->_values[$this->name][$names[0]] = Set::insert($this->_values[$this->name][$names[0]], $names[1], $value);
|
||||
$this->_values[$this->name][$names[0]] = Hash::insert($this->_values[$this->name][$names[0]], $names[1], $value);
|
||||
$this->_write('[' . implode('][', $names) . ']', $value);
|
||||
}
|
||||
}
|
||||
|
@ -276,7 +277,7 @@ class CookieComponent extends Component {
|
|||
}
|
||||
|
||||
if (!empty($names[1])) {
|
||||
return Set::extract($this->_values[$this->name][$key], $names[1]);
|
||||
return Hash::get($this->_values[$this->name][$key], $names[1]);
|
||||
}
|
||||
return $this->_values[$this->name][$key];
|
||||
}
|
||||
|
@ -310,7 +311,7 @@ class CookieComponent extends Component {
|
|||
}
|
||||
$names = explode('.', $key, 2);
|
||||
if (isset($this->_values[$this->name][$names[0]])) {
|
||||
$this->_values[$this->name][$names[0]] = Set::remove($this->_values[$this->name][$names[0]], $names[1]);
|
||||
$this->_values[$this->name][$names[0]] = Hash::remove($this->_values[$this->name][$names[0]], $names[1]);
|
||||
}
|
||||
$this->_delete('[' . implode('][', $names) . ']');
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
* @since CakePHP(tm) v 2.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* This component is used to handle automatic model data pagination. The primary way to use this
|
||||
|
@ -194,7 +195,7 @@ class PaginatorComponent extends Component {
|
|||
'pageCount' => $pageCount,
|
||||
'order' => $order,
|
||||
'limit' => $limit,
|
||||
'options' => Set::diff($options, $defaults),
|
||||
'options' => Hash::diff($options, $defaults),
|
||||
'paramType' => $options['paramType']
|
||||
);
|
||||
if (!isset($this->Controller->request['paging'])) {
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
App::uses('Component', 'Controller');
|
||||
App::uses('String', 'Utility');
|
||||
App::uses('Hash', 'Utility');
|
||||
App::uses('Security', 'Utility');
|
||||
|
||||
/**
|
||||
|
@ -443,7 +444,7 @@ class SecurityComponent extends Component {
|
|||
$unlocked = explode('|', $unlocked);
|
||||
|
||||
$lockedFields = array();
|
||||
$fields = Set::flatten($check);
|
||||
$fields = Hash::flatten($check);
|
||||
$fieldList = array_keys($fields);
|
||||
$multi = array();
|
||||
|
||||
|
|
|
@ -13,8 +13,12 @@
|
|||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::uses('Set', 'Utility');
|
||||
App::uses('Hash', 'Utility');
|
||||
App::uses('ConfigReaderInterface', 'Configure');
|
||||
/**
|
||||
* Compatibility with 2.1, which expects Configure to load Set.
|
||||
*/
|
||||
App::uses('Set', 'Utility');
|
||||
|
||||
/**
|
||||
* Configuration class. Used for managing runtime configuration information.
|
||||
|
@ -127,12 +131,7 @@ class Configure {
|
|||
}
|
||||
|
||||
foreach ($config as $name => $value) {
|
||||
$pointer = &self::$_values;
|
||||
foreach (explode('.', $name) as $key) {
|
||||
$pointer = &$pointer[$key];
|
||||
}
|
||||
$pointer = $value;
|
||||
unset($pointer);
|
||||
self::$_values = Hash::insert(self::$_values, $name, $value);
|
||||
}
|
||||
|
||||
if (isset($config['debug']) && function_exists('ini_set')) {
|
||||
|
@ -163,18 +162,7 @@ class Configure {
|
|||
if ($var === null) {
|
||||
return self::$_values;
|
||||
}
|
||||
if (isset(self::$_values[$var])) {
|
||||
return self::$_values[$var];
|
||||
}
|
||||
$pointer = &self::$_values;
|
||||
foreach (explode('.', $var) as $key) {
|
||||
if (isset($pointer[$key])) {
|
||||
$pointer = &$pointer[$key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return $pointer;
|
||||
return Hash::get(self::$_values, $var);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -193,11 +181,7 @@ class Configure {
|
|||
public static function delete($var = null) {
|
||||
$keys = explode('.', $var);
|
||||
$last = array_pop($keys);
|
||||
$pointer = &self::$_values;
|
||||
foreach ($keys as $key) {
|
||||
$pointer = &$pointer[$key];
|
||||
}
|
||||
unset($pointer[$last]);
|
||||
self::$_values = Hash::remove(self::$_values, $var);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -286,7 +270,7 @@ class Configure {
|
|||
$keys = array_keys($values);
|
||||
foreach ($keys as $key) {
|
||||
if (($c = self::read($key)) && is_array($values[$key]) && is_array($c)) {
|
||||
$values[$key] = Set::merge($c, $values[$key]);
|
||||
$values[$key] = Hash::merge($c, $values[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -181,7 +181,7 @@ class Object {
|
|||
*
|
||||
* @param array $properties The name of the properties to merge.
|
||||
* @param string $class The class to merge the property with.
|
||||
* @param boolean $normalize Set to true to run the properties through Set::normalize() before merging.
|
||||
* @param boolean $normalize Set to true to run the properties through Hash::normalize() before merging.
|
||||
* @return void
|
||||
*/
|
||||
protected function _mergeVars($properties, $class, $normalize = true) {
|
||||
|
@ -194,10 +194,10 @@ class Object {
|
|||
$this->{$var} != $classProperties[$var]
|
||||
) {
|
||||
if ($normalize) {
|
||||
$classProperties[$var] = Set::normalize($classProperties[$var]);
|
||||
$this->{$var} = Set::normalize($this->{$var});
|
||||
$classProperties[$var] = Hash::normalize($classProperties[$var]);
|
||||
$this->{$var} = Hash::normalize($this->{$var});
|
||||
}
|
||||
$this->{$var} = Set::merge($classProperties[$var], $this->{$var});
|
||||
$this->{$var} = Hash::merge($classProperties[$var], $this->{$var});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -385,7 +385,7 @@ class I18n {
|
|||
$this->_domains[$domain][$this->_lang][$this->category]["%plural-c"] = $switch;
|
||||
unset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]);
|
||||
}
|
||||
$this->_domains = Set::pushDiff($this->_domains, $merge);
|
||||
$this->_domains = Hash::mergeDiff($this->_domains, $merge);
|
||||
|
||||
if (isset($this->_domains[$domain][$this->_lang][$this->category][null])) {
|
||||
unset($this->_domains[$domain][$this->_lang][$this->category][null]);
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
App::uses('AclNode', 'Model');
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* ACL behavior
|
||||
|
@ -131,7 +132,7 @@ class AclBehavior extends ModelBehavior {
|
|||
$types = array($types);
|
||||
}
|
||||
foreach ($types as $type) {
|
||||
$node = Set::extract($this->node($model, null, $type), "0.{$type}.id");
|
||||
$node = Hash::extract($this->node($model, null, $type), "0.{$type}.id");
|
||||
if (!empty($node)) {
|
||||
$model->{$type}->delete($node);
|
||||
}
|
||||
|
|
|
@ -321,7 +321,7 @@ class ContainableBehavior extends ModelBehavior {
|
|||
$key = $option;
|
||||
$optionKey = true;
|
||||
if (!empty($newChildren)) {
|
||||
$children = Set::merge($children, $newChildren);
|
||||
$children = Hash::merge($children, $newChildren);
|
||||
}
|
||||
}
|
||||
if ($optionKey && isset($children[$key])) {
|
||||
|
|
|
@ -363,10 +363,10 @@ class TreeBehavior extends ModelBehavior {
|
|||
}
|
||||
|
||||
if ($valuePath == null) {
|
||||
$valuePath = array('{0}{1}', '{n}.tree_prefix', '{n}.' . $Model->alias . '.' . $Model->displayField);
|
||||
$valuePath = array('%s%s', '{n}.tree_prefix', '{n}.' . $Model->alias . '.' . $Model->displayField);
|
||||
|
||||
} elseif (is_string($valuePath)) {
|
||||
$valuePath = array('{0}{1}', '{n}.tree_prefix', $valuePath);
|
||||
$valuePath = array('%s%s', '{n}.tree_prefix', $valuePath);
|
||||
|
||||
} else {
|
||||
$valuePath[0] = '{' . (count($valuePath) - 1) . '}' . $valuePath[0];
|
||||
|
@ -386,7 +386,7 @@ class TreeBehavior extends ModelBehavior {
|
|||
if (empty($results)) {
|
||||
return array();
|
||||
}
|
||||
return Set::combine($results, $keyPath, $valuePath);
|
||||
return Hash::combine($results, $keyPath, $valuePath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::uses('Set', 'Utility');
|
||||
App::uses('Hash', 'Utility');
|
||||
App::uses('Security', 'Utility');
|
||||
|
||||
/**
|
||||
|
@ -216,7 +216,7 @@ class CakeSession {
|
|||
if (empty($name)) {
|
||||
return false;
|
||||
}
|
||||
$result = Set::classicExtract($_SESSION, $name);
|
||||
$result = Hash::get($_SESSION, $name);
|
||||
return isset($result);
|
||||
}
|
||||
|
||||
|
@ -245,7 +245,7 @@ class CakeSession {
|
|||
*/
|
||||
public static function delete($name) {
|
||||
if (self::check($name)) {
|
||||
self::_overwrite($_SESSION, Set::remove($_SESSION, $name));
|
||||
self::_overwrite($_SESSION, Hash::remove($_SESSION, $name));
|
||||
return (self::check($name) == false);
|
||||
}
|
||||
self::_setError(2, __d('cake_dev', "%s doesn't exist", $name));
|
||||
|
@ -364,9 +364,9 @@ class CakeSession {
|
|||
if (empty($name)) {
|
||||
return false;
|
||||
}
|
||||
$result = Set::classicExtract($_SESSION, $name);
|
||||
$result = Hash::get($_SESSION, $name);
|
||||
|
||||
if (!is_null($result)) {
|
||||
if (isset($result)) {
|
||||
return $result;
|
||||
}
|
||||
self::_setError(2, "$name doesn't exist");
|
||||
|
@ -405,8 +405,8 @@ class CakeSession {
|
|||
$write = array($name => $value);
|
||||
}
|
||||
foreach ($write as $key => $val) {
|
||||
self::_overwrite($_SESSION, Set::insert($_SESSION, $key, $val));
|
||||
if (Set::classicExtract($_SESSION, $key) !== $val) {
|
||||
self::_overwrite($_SESSION, Hash::insert($_SESSION, $key, $val));
|
||||
if (Hash::get($_SESSION, $key) !== $val) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -452,7 +452,7 @@ class CakeSession {
|
|||
if (isset($sessionConfig['defaults'])) {
|
||||
$defaults = self::_defaultConfig($sessionConfig['defaults']);
|
||||
if ($defaults) {
|
||||
$sessionConfig = Set::merge($defaults, $sessionConfig);
|
||||
$sessionConfig = Hash::merge($defaults, $sessionConfig);
|
||||
}
|
||||
}
|
||||
if (!isset($sessionConfig['ini']['session.cookie_secure']) && env('HTTPS')) {
|
||||
|
|
|
@ -421,7 +421,7 @@ class Postgres extends DboSource {
|
|||
$match[1] = $this->name($match[1]);
|
||||
} elseif (!$constant) {
|
||||
$parts = explode('.', $match[1]);
|
||||
if (!Set::numeric($parts)) {
|
||||
if (!Hash::numeric($parts)) {
|
||||
$match[1] = $this->name($match[1]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1354,7 +1354,7 @@ class DboSource extends DataSource {
|
|||
}
|
||||
}
|
||||
}
|
||||
$result = Set::pushDiff($result, array($association => $merged));
|
||||
$result = Hash::mergeDiff($result, array($association => $merged));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1409,7 +1409,7 @@ class DboSource extends DataSource {
|
|||
if ($mergeKeys[0] === $dataKeys[0] || $mergeKeys === $dataKeys) {
|
||||
$data[$association][$association] = $merge[0][$association];
|
||||
} else {
|
||||
$diff = Set::diff($dataAssocTmp, $mergeAssocTmp);
|
||||
$diff = Hash::diff($dataAssocTmp, $mergeAssocTmp);
|
||||
$data[$association] = array_merge($merge[0][$association], $diff);
|
||||
}
|
||||
} elseif ($selfJoin && array_key_exists($association, $merge[0])) {
|
||||
|
@ -1935,7 +1935,7 @@ class DboSource extends DataSource {
|
|||
return false;
|
||||
}
|
||||
$conditions = $this->conditions(array(
|
||||
$model->primaryKey => Set::extract($idList, "{n}.{$model->alias}.{$model->primaryKey}")
|
||||
$model->primaryKey => Hash::extract($idList, "{n}.{$model->alias}.{$model->primaryKey}")
|
||||
));
|
||||
}
|
||||
return $conditions;
|
||||
|
@ -2257,7 +2257,7 @@ class DboSource extends DataSource {
|
|||
} else {
|
||||
if (strpos($fields[$i], ',') === false) {
|
||||
$build = explode('.', $fields[$i]);
|
||||
if (!Set::numeric($build)) {
|
||||
if (!Hash::numeric($build)) {
|
||||
$fields[$i] = $this->name(implode('.', $build));
|
||||
}
|
||||
}
|
||||
|
@ -2269,7 +2269,7 @@ class DboSource extends DataSource {
|
|||
$field[1] = $this->name($alias . '.' . $field[1]);
|
||||
} else {
|
||||
$field[0] = explode('.', $field[1]);
|
||||
if (!Set::numeric($field[0])) {
|
||||
if (!Hash::numeric($field[0])) {
|
||||
$field[0] = implode('.', array_map(array(&$this, 'name'), $field[0]));
|
||||
$fields[$i] = preg_replace('/\(' . $field[1] . '\)/', '(' . $field[0] . ')', $fields[$i], 1);
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
App::uses('ClassRegistry', 'Utility');
|
||||
App::uses('Validation', 'Utility');
|
||||
App::uses('String', 'Utility');
|
||||
App::uses('Set', 'Utility');
|
||||
App::uses('Hash', 'Utility');
|
||||
App::uses('BehaviorCollection', 'Model');
|
||||
App::uses('ModelBehavior', 'Model');
|
||||
App::uses('ConnectionManager', 'Model');
|
||||
|
@ -1728,7 +1728,7 @@ class Model extends Object implements CakeEventListener {
|
|||
$this->getEventManager()->dispatch($event);
|
||||
}
|
||||
if (!empty($this->data)) {
|
||||
$success = Set::merge($success, $this->data);
|
||||
$success = Hash::merge($success, $this->data);
|
||||
}
|
||||
$this->data = false;
|
||||
$this->_clearCache();
|
||||
|
@ -1819,7 +1819,7 @@ class Model extends Object implements CakeEventListener {
|
|||
'fields' => $associationForeignKey,
|
||||
));
|
||||
|
||||
$oldLinks = Set::extract($links, "{n}.{$associationForeignKey}");
|
||||
$oldLinks = Hash::extract($links, "{n}.{$associationForeignKey}");
|
||||
if (!empty($oldLinks)) {
|
||||
if ($keepExisting && !empty($newJoins)) {
|
||||
$conditions[$associationForeignKey] = array_diff($oldLinks, $newJoins);
|
||||
|
@ -1993,7 +1993,7 @@ class Model extends Object implements CakeEventListener {
|
|||
*/
|
||||
public function saveAll($data = null, $options = array()) {
|
||||
$options = array_merge(array('validate' => 'first'), $options);
|
||||
if (Set::numeric(array_keys($data))) {
|
||||
if (Hash::numeric(array_keys($data))) {
|
||||
if ($options['validate'] === 'only') {
|
||||
return $this->validateMany($data, $options);
|
||||
}
|
||||
|
@ -2514,7 +2514,7 @@ class Model extends Object implements CakeEventListener {
|
|||
return false;
|
||||
}
|
||||
|
||||
$ids = Set::extract($ids, "{n}.{$this->alias}.{$this->primaryKey}");
|
||||
$ids = Hash::extract($ids, "{n}.{$this->alias}.{$this->primaryKey}");
|
||||
if (empty($ids)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -2837,7 +2837,7 @@ class Model extends Object implements CakeEventListener {
|
|||
return array();
|
||||
}
|
||||
$lst = $query['list'];
|
||||
return Set::combine($results, $lst['keyPath'], $lst['valuePath'], $lst['groupPath']);
|
||||
return Hash::combine($results, $lst['keyPath'], $lst['valuePath'], $lst['groupPath']);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2873,8 +2873,8 @@ class Model extends Object implements CakeEventListener {
|
|||
unset($query['conditions'][$field . ' <']);
|
||||
$return = array();
|
||||
if (isset($results[0])) {
|
||||
$prevVal = Set::extract('/' . str_replace('.', '/', $field), $results[0]);
|
||||
$query['conditions'][$field . ' >='] = $prevVal[0];
|
||||
$prevVal = Hash::get($results[0], $field);
|
||||
$query['conditions'][$field . ' >='] = $prevVal;
|
||||
$query['conditions'][$field . ' !='] = $value;
|
||||
$query['limit'] = 2;
|
||||
} else {
|
||||
|
@ -2883,14 +2883,14 @@ class Model extends Object implements CakeEventListener {
|
|||
$query['limit'] = 1;
|
||||
}
|
||||
$query['order'] = $field . ' ASC';
|
||||
$return2 = $this->find('all', $query);
|
||||
$neighbors = $this->find('all', $query);
|
||||
if (!array_key_exists('prev', $return)) {
|
||||
$return['prev'] = $return2[0];
|
||||
$return['prev'] = $neighbors[0];
|
||||
}
|
||||
if (count($return2) === 2) {
|
||||
$return['next'] = $return2[1];
|
||||
} elseif (count($return2) === 1 && !$return['prev']) {
|
||||
$return['next'] = $return2[0];
|
||||
if (count($neighbors) === 2) {
|
||||
$return['next'] = $neighbors[1];
|
||||
} elseif (count($neighbors) === 1 && !$return['prev']) {
|
||||
$return['next'] = $neighbors[0];
|
||||
} else {
|
||||
$return['next'] = null;
|
||||
}
|
||||
|
@ -2915,9 +2915,9 @@ class Model extends Object implements CakeEventListener {
|
|||
if (isset($query['parent'])) {
|
||||
$parent = $query['parent'];
|
||||
}
|
||||
return Set::nest($results, array(
|
||||
'idPath' => '/' . $this->alias . '/' . $this->primaryKey,
|
||||
'parentPath' => '/' . $this->alias . '/' . $parent
|
||||
return Hash::nest($results, array(
|
||||
'idPath' => '{n}.' . $this->alias . '.' . $this->primaryKey,
|
||||
'parentPath' => '{n}.' . $this->alias . '.' . $parent
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,7 +105,7 @@ class Permission extends AppModel {
|
|||
}
|
||||
|
||||
$inherited = array();
|
||||
$acoIDs = Set::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
|
||||
$acoIDs = Hash::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
|
||||
|
||||
$count = count($aroPath);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
|
@ -123,7 +123,7 @@ class Permission extends AppModel {
|
|||
if (empty($perms)) {
|
||||
continue;
|
||||
} else {
|
||||
$perms = Set::extract($perms, '{n}.' . $this->alias);
|
||||
$perms = Hash::extract($perms, '{n}.' . $this->alias);
|
||||
foreach ($perms as $perm) {
|
||||
if ($action == '*') {
|
||||
|
||||
|
@ -223,13 +223,17 @@ class Permission extends AppModel {
|
|||
if (empty($obj['Aro']) || empty($obj['Aco'])) {
|
||||
return false;
|
||||
}
|
||||
$aro = Hash::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id');
|
||||
$aco = Hash::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id');
|
||||
$aro = current($aro);
|
||||
$aco = current($aco);
|
||||
|
||||
return array(
|
||||
'aro' => Set::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id'),
|
||||
'aco' => Set::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id'),
|
||||
'aro' => $aro,
|
||||
'aco' => $aco,
|
||||
'link' => $this->find('all', array('conditions' => array(
|
||||
$this->alias . '.aro_id' => Set::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id'),
|
||||
$this->alias . '.aco_id' => Set::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id')
|
||||
$this->alias . '.aro_id' => $aro,
|
||||
$this->alias . '.aco_id' => $aco
|
||||
)))
|
||||
);
|
||||
}
|
||||
|
|
|
@ -153,7 +153,7 @@ class CakeRequest implements ArrayAccess {
|
|||
* into a single array. Variables prefixed with `data` will overwrite those without.
|
||||
*
|
||||
* If you have mixed POST values be careful not to make any top level keys numeric
|
||||
* containing arrays. Set::merge() is used to merge data, and it has possibly
|
||||
* containing arrays. Hash::merge() is used to merge data, and it has possibly
|
||||
* unexpected behavior in this situation.
|
||||
*
|
||||
* @return void
|
||||
|
@ -181,7 +181,7 @@ class CakeRequest implements ArrayAccess {
|
|||
$this->data = $data;
|
||||
} else {
|
||||
unset($this->data['data']);
|
||||
$this->data = Set::merge($this->data, $data);
|
||||
$this->data = Hash::merge($this->data, $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -519,7 +519,7 @@ class CakeRequest implements ArrayAccess {
|
|||
public function addDetector($name, $options) {
|
||||
$name = strtolower($name);
|
||||
if (isset($this->_detectors[$name]) && isset($options['options'])) {
|
||||
$options = Set::merge($this->_detectors[$name], $options);
|
||||
$options = Hash::merge($this->_detectors[$name], $options);
|
||||
}
|
||||
$this->_detectors[$name] = $options;
|
||||
}
|
||||
|
@ -748,10 +748,10 @@ class CakeRequest implements ArrayAccess {
|
|||
public function data($name) {
|
||||
$args = func_get_args();
|
||||
if (count($args) == 2) {
|
||||
$this->data = Set::insert($this->data, $name, $args[1]);
|
||||
$this->data = Hash::insert($this->data, $name, $args[1]);
|
||||
return $this;
|
||||
}
|
||||
return Set::classicExtract($this->data, $name);
|
||||
return Hash::get($this->data, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1386,7 +1386,7 @@ class CakeEmail {
|
|||
|
||||
$msg = array();
|
||||
|
||||
$contentIds = array_filter((array)Set::classicExtract($this->_attachments, '{s}.contentId'));
|
||||
$contentIds = array_filter((array)Hash::extract($this->_attachments, '{s}.contentId'));
|
||||
$hasInlineAttachments = count($contentIds) > 0;
|
||||
$hasAttachments = !empty($this->_attachments);
|
||||
$hasMultipleTypes = count($rendered) > 1;
|
||||
|
|
|
@ -153,7 +153,7 @@ class HttpSocket extends CakeSocket {
|
|||
$this->_configUri($config['request']['uri']);
|
||||
unset($config['request']['uri']);
|
||||
}
|
||||
$this->config = Set::merge($this->config, $config);
|
||||
$this->config = Hash::merge($this->config, $config);
|
||||
}
|
||||
parent::__construct($this->config);
|
||||
}
|
||||
|
@ -271,7 +271,7 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
$request['uri'] = $this->url($request['uri']);
|
||||
$request['uri'] = $this->_parseUri($request['uri'], true);
|
||||
$this->request = Set::merge($this->request, array_diff_key($this->config['request'], array('cookies' => true)), $request);
|
||||
$this->request = Hash::merge($this->request, array_diff_key($this->config['request'], array('cookies' => true)), $request);
|
||||
|
||||
$this->_configUri($this->request['uri']);
|
||||
|
||||
|
@ -445,7 +445,7 @@ class HttpSocket extends CakeSocket {
|
|||
$uri = $this->_buildUri($uri);
|
||||
}
|
||||
|
||||
$request = Set::merge(array('method' => 'GET', 'uri' => $uri), $request);
|
||||
$request = Hash::merge(array('method' => 'GET', 'uri' => $uri), $request);
|
||||
return $this->request($request);
|
||||
}
|
||||
|
||||
|
@ -467,7 +467,7 @@ class HttpSocket extends CakeSocket {
|
|||
* @return mixed Result of request, either false on failure or the response to the request.
|
||||
*/
|
||||
public function post($uri = null, $data = array(), $request = array()) {
|
||||
$request = Set::merge(array('method' => 'POST', 'uri' => $uri, 'body' => $data), $request);
|
||||
$request = Hash::merge(array('method' => 'POST', 'uri' => $uri, 'body' => $data), $request);
|
||||
return $this->request($request);
|
||||
}
|
||||
|
||||
|
@ -480,7 +480,7 @@ class HttpSocket extends CakeSocket {
|
|||
* @return mixed Result of request
|
||||
*/
|
||||
public function put($uri = null, $data = array(), $request = array()) {
|
||||
$request = Set::merge(array('method' => 'PUT', 'uri' => $uri, 'body' => $data), $request);
|
||||
$request = Hash::merge(array('method' => 'PUT', 'uri' => $uri, 'body' => $data), $request);
|
||||
return $this->request($request);
|
||||
}
|
||||
|
||||
|
@ -493,7 +493,7 @@ class HttpSocket extends CakeSocket {
|
|||
* @return mixed Result of request
|
||||
*/
|
||||
public function delete($uri = null, $data = array(), $request = array()) {
|
||||
$request = Set::merge(array('method' => 'DELETE', 'uri' => $uri, 'body' => $data), $request);
|
||||
$request = Hash::merge(array('method' => 'DELETE', 'uri' => $uri, 'body' => $data), $request);
|
||||
return $this->request($request);
|
||||
}
|
||||
|
||||
|
@ -638,8 +638,8 @@ class HttpSocket extends CakeSocket {
|
|||
'uri' => array_intersect_key($uri, $this->config['request']['uri'])
|
||||
)
|
||||
);
|
||||
$this->config = Set::merge($this->config, $config);
|
||||
$this->config = Set::merge($this->config, array_intersect_key($this->config['request']['uri'], $this->config));
|
||||
$this->config = Hash::merge($this->config, $config);
|
||||
$this->config = Hash::merge($this->config, array_intersect_key($this->config['request']['uri'], $this->config));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -494,7 +494,7 @@ class CakeRoute {
|
|||
$named = array();
|
||||
foreach ($params['named'] as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$flat = Set::flatten($value, '][');
|
||||
$flat = Hash::flatten($value, '][');
|
||||
foreach ($flat as $namedKey => $namedValue) {
|
||||
$named[] = $key . "[$namedKey]" . $separator . rawurlencode($namedValue);
|
||||
}
|
||||
|
|
|
@ -891,7 +891,7 @@ class Router {
|
|||
}
|
||||
}
|
||||
|
||||
list($args, $named) = array(Set::filter($args, true), Set::filter($named, true));
|
||||
list($args, $named) = array(Hash::filter($args), Hash::filter($named));
|
||||
foreach (self::$_prefixes as $prefix) {
|
||||
if (!empty($url[$prefix])) {
|
||||
$url['action'] = str_replace($prefix . '_', '', $url['action']);
|
||||
|
@ -924,7 +924,7 @@ class Router {
|
|||
if (!empty($named)) {
|
||||
foreach ($named as $name => $value) {
|
||||
if (is_array($value)) {
|
||||
$flattend = Set::flatten($value, '][');
|
||||
$flattend = Hash::flatten($value, '][');
|
||||
foreach ($flattend as $namedKey => $namedValue) {
|
||||
$output .= '/' . $name . "[$namedKey]" . self::$_namedConfig['separator'] . rawurlencode($namedValue);
|
||||
}
|
||||
|
|
|
@ -163,7 +163,7 @@ class ShellTest extends CakeTestCase {
|
|||
$this->assertEquals($expected, $this->Shell->tasks);
|
||||
|
||||
$expected = array('Fixture' => null, 'DbConfig' => array('one', 'two'));
|
||||
$this->assertEquals($expected, Set::normalize($this->Shell->tasks), 'Normalized results are wrong.');
|
||||
$this->assertEquals($expected, Hash::normalize($this->Shell->tasks), 'Normalized results are wrong.');
|
||||
$this->assertEquals(array('Comment', 'Posts'), $this->Shell->uses, 'Merged models are wrong.');
|
||||
}
|
||||
|
||||
|
|
|
@ -108,7 +108,7 @@ class PaginatorControllerPost extends CakeTestModel {
|
|||
public function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
|
||||
if ($conditions == 'popular') {
|
||||
$conditions = array($this->name . '.' . $this->primaryKey . ' > ' => '1');
|
||||
$options = Set::merge($fields, compact('conditions'));
|
||||
$options = Hash::merge($fields, compact('conditions'));
|
||||
return parent::find('all', $options);
|
||||
}
|
||||
return parent::find($conditions, $fields);
|
||||
|
@ -353,47 +353,47 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
$Controller->request->query = array();
|
||||
$Controller->constructClasses();
|
||||
|
||||
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$this->assertEquals(array(1, 2, 3), $results);
|
||||
|
||||
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id');
|
||||
$this->assertEquals(array(1, 2, 3, 4, 5, 6), $results);
|
||||
|
||||
$Controller->modelClass = null;
|
||||
|
||||
$Controller->uses[0] = 'Plugin.PaginatorControllerPost';
|
||||
$results = Set::extract($Controller->Paginator->paginate(), '{n}.PaginatorControllerPost.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate(), '{n}.PaginatorControllerPost.id');
|
||||
$this->assertEquals(array(1, 2, 3), $results);
|
||||
|
||||
$Controller->request->params['named'] = array('page' => '-1');
|
||||
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
||||
$this->assertEquals(array(1, 2, 3), $results);
|
||||
|
||||
$Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'asc');
|
||||
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
||||
$this->assertEquals(array(1, 2, 3), $results);
|
||||
|
||||
$Controller->request->params['named'] = array('sort' => 'PaginatorControllerPost.id', 'direction' => 'desc');
|
||||
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
||||
$this->assertEquals(array(3, 2, 1), $results);
|
||||
|
||||
$Controller->request->params['named'] = array('sort' => 'id', 'direction' => 'desc');
|
||||
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
||||
$this->assertEquals(array(3, 2, 1), $results);
|
||||
|
||||
$Controller->request->params['named'] = array('sort' => 'NotExisting.field', 'direction' => 'desc');
|
||||
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page'], 'Invalid field in query %s');
|
||||
$this->assertEquals(array(1, 2, 3), $results);
|
||||
|
||||
$Controller->request->params['named'] = array(
|
||||
'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
|
||||
);
|
||||
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$this->assertEquals(array('PaginatorControllerPost.author_id' => 'asc'), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0]);
|
||||
$this->assertEquals(array(1, 3, 2), $results);
|
||||
|
||||
|
@ -475,7 +475,7 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
$Controller->request->params['named'] = array('page' => '-1', 'contain' => array('PaginatorControllerComment'));
|
||||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
||||
$this->assertEquals(array(1, 2, 3), Set::extract($result, '{n}.PaginatorControllerPost.id'));
|
||||
$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
|
||||
$this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
|
||||
|
||||
$Controller->request->params['named'] = array('page' => '-1');
|
||||
|
@ -488,7 +488,7 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
);
|
||||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
|
||||
$this->assertEquals(array(1, 2, 3), Set::extract($result, '{n}.PaginatorControllerPost.id'));
|
||||
$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
|
||||
$this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));
|
||||
|
||||
$Controller->Paginator->settings = array(
|
||||
|
@ -497,7 +497,7 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
),
|
||||
);
|
||||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$this->assertEquals(array(2, 3), Set::extract($result, '{n}.PaginatorControllerPost.id'));
|
||||
$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
|
||||
$this->assertEquals(array('PaginatorControllerPost.id > ' => '1'), $Controller->PaginatorControllerPost->lastQueries[1]['conditions']);
|
||||
|
||||
$Controller->request->params['named'] = array('limit' => 12);
|
||||
|
@ -572,7 +572,7 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
);
|
||||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
|
||||
$this->assertEquals(array(2, 3), Set::extract($result, '{n}.PaginatorControllerPost.id'));
|
||||
$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
|
||||
$this->assertEquals(
|
||||
$Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
|
||||
array('PaginatorControllerPost.id > ' => '1')
|
||||
|
@ -595,7 +595,7 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
'maxLimit' => 10,
|
||||
'paramType' => 'named'
|
||||
);
|
||||
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
|
||||
$this->assertEquals('PaginatorControllerPost.id DESC', $Controller->params['paging']['PaginatorControllerPost']['order']);
|
||||
$this->assertEquals(array(3, 2, 1), $results);
|
||||
}
|
||||
|
@ -621,11 +621,11 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
'paramType' => 'named'
|
||||
);
|
||||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$this->assertEquals(array(4, 3, 2), Set::extract($result, '{n}.PaginatorControllerPost.offset_test'));
|
||||
$this->assertEquals(array(4, 3, 2), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
|
||||
|
||||
$Controller->request->params['named'] = array('sort' => 'offset_test', 'direction' => 'asc');
|
||||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$this->assertEquals(array(2, 3, 4), Set::extract($result, '{n}.PaginatorControllerPost.offset_test'));
|
||||
$this->assertEquals(array(2, 3, 4), Hash::extract($result, '{n}.PaginatorControllerPost.offset_test'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -645,11 +645,11 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
'paramType' => 'named'
|
||||
);
|
||||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$this->assertEquals(array(4, 2, 2), Set::extract($result, '{n}.PaginatorAuthor.joined_offset'));
|
||||
$this->assertEquals(array(4, 2, 2), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
|
||||
|
||||
$Controller->request->params['named'] = array('sort' => 'PaginatorAuthor.joined_offset', 'direction' => 'asc');
|
||||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$this->assertEquals(array(2, 2, 4), Set::extract($result, '{n}.PaginatorAuthor.joined_offset'));
|
||||
$this->assertEquals(array(2, 2, 4), Hash::extract($result, '{n}.PaginatorAuthor.joined_offset'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -974,7 +974,7 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
);
|
||||
$Controller->passedArgs = array('sort' => 'PaginatorControllerPost.title', 'dir' => 'asc');
|
||||
$result = $Controller->paginate('PaginatorControllerComment');
|
||||
$this->assertEquals(array(1, 2, 3, 4, 5, 6), Set::extract($result, '{n}.PaginatorControllerComment.id'));
|
||||
$this->assertEquals(array(1, 2, 3, 4, 5, 6), Hash::extract($result, '{n}.PaginatorControllerComment.id'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -992,7 +992,7 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
$this->assertTrue(!empty($result));
|
||||
|
||||
$result = $Controller->paginate();
|
||||
$this->assertEquals(Set::extract($result, '{n}.PaginatorCustomPost.id'), array(1, 2, 3, 4));
|
||||
$this->assertEquals(array(1, 2, 3, 4), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
|
||||
|
||||
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
||||
$this->assertEquals(4, $result['current']);
|
||||
|
@ -1000,7 +1000,7 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
|
||||
$Controller->paginate = array('published');
|
||||
$result = $Controller->paginate();
|
||||
$this->assertEquals(Set::extract($result, '{n}.PaginatorCustomPost.id'), array(1, 2, 3));
|
||||
$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
|
||||
|
||||
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
||||
$this->assertEquals(3, $result['current']);
|
||||
|
@ -1008,7 +1008,7 @@ class PaginatorComponentTest extends CakeTestCase {
|
|||
|
||||
$Controller->paginate = array('published', 'limit' => 2);
|
||||
$result = $Controller->paginate();
|
||||
$this->assertEquals(Set::extract($result, '{n}.PaginatorCustomPost.id'), array(1, 2));
|
||||
$this->assertEquals(array(1, 2), Hash::extract($result, '{n}.PaginatorCustomPost.id'));
|
||||
|
||||
$result = $Controller->params['paging']['PaginatorCustomPost'];
|
||||
$this->assertEquals(2, $result['current']);
|
||||
|
|
|
@ -104,7 +104,7 @@ class ControllerPost extends CakeTestModel {
|
|||
public function find($type = 'first', $options = array()) {
|
||||
if ($type == 'popular') {
|
||||
$conditions = array($this->name . '.' . $this->primaryKey . ' > ' => '1');
|
||||
$options = Set::merge($options, compact('conditions'));
|
||||
$options = Hash::merge($options, compact('conditions'));
|
||||
return parent::find('all', $options);
|
||||
}
|
||||
return parent::find($type, $options);
|
||||
|
@ -900,7 +900,7 @@ class ControllerTest extends CakeTestCase {
|
|||
|
||||
$this->assertEquals(0, count(array_diff_key($TestController->helpers, array_flip($helpers))));
|
||||
$this->assertEquals(0, count(array_diff($TestController->uses, $uses)));
|
||||
$this->assertEquals(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0);
|
||||
$this->assertEquals(count(array_diff_assoc(Hash::normalize($TestController->components), Hash::normalize($components))), 0);
|
||||
|
||||
$expected = array('ControllerComment', 'ControllerAlias', 'ControllerPost');
|
||||
$this->assertEquals($expected, $TestController->uses, '$uses was merged incorrectly, ControllerTestAppController models should be last.');
|
||||
|
@ -1277,7 +1277,7 @@ class ControllerTest extends CakeTestCase {
|
|||
$expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named');
|
||||
$this->assertEquals($expected, $Controller->paginate);
|
||||
|
||||
$results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
|
||||
$results = Hash::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
|
||||
$this->assertEquals(array(1, 2, 3), $results);
|
||||
|
||||
$Controller->passedArgs = array();
|
||||
|
|
|
@ -254,35 +254,33 @@ class AclNodeTest extends CakeTestCase {
|
|||
*/
|
||||
public function testNode() {
|
||||
$Aco = new DbAcoTest();
|
||||
$result = Set::extract($Aco->node('Controller1'), '{n}.DbAcoTest.id');
|
||||
$result = Hash::extract($Aco->node('Controller1'), '{n}.DbAcoTest.id');
|
||||
$expected = array(2, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($Aco->node('Controller1/action1'), '{n}.DbAcoTest.id');
|
||||
$result = Hash::extract($Aco->node('Controller1/action1'), '{n}.DbAcoTest.id');
|
||||
$expected = array(3, 2, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($Aco->node('Controller2/action1'), '{n}.DbAcoTest.id');
|
||||
$result = Hash::extract($Aco->node('Controller2/action1'), '{n}.DbAcoTest.id');
|
||||
$expected = array(7, 6, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($Aco->node('Controller1/action2'), '{n}.DbAcoTest.id');
|
||||
$result = Hash::extract($Aco->node('Controller1/action2'), '{n}.DbAcoTest.id');
|
||||
$expected = array(5, 2, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($Aco->node('Controller1/action1/record1'), '{n}.DbAcoTest.id');
|
||||
$result = Hash::extract($Aco->node('Controller1/action1/record1'), '{n}.DbAcoTest.id');
|
||||
$expected = array(4, 3, 2, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($Aco->node('Controller2/action1/record1'), '{n}.DbAcoTest.id');
|
||||
$result = Hash::extract($Aco->node('Controller2/action1/record1'), '{n}.DbAcoTest.id');
|
||||
$expected = array(8, 7, 6, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($Aco->node('Controller2/action3'), '{n}.DbAcoTest.id');
|
||||
$this->assertNull($result);
|
||||
$this->assertFalse($Aco->node('Controller2/action3'));
|
||||
|
||||
$result = Set::extract($Aco->node('Controller2/action3/record5'), '{n}.DbAcoTest.id');
|
||||
$this->assertNull($result);
|
||||
$this->assertFalse($Aco->node('Controller2/action3/record5'));
|
||||
|
||||
$result = $Aco->node('');
|
||||
$this->assertEquals(null, $result);
|
||||
|
@ -307,12 +305,12 @@ class AclNodeTest extends CakeTestCase {
|
|||
public function testNodeArrayFind() {
|
||||
$Aro = new DbAroTest();
|
||||
Configure::write('DbAclbindMode', 'string');
|
||||
$result = Set::extract($Aro->node(array('DbAroUserTest' => array('id' => '1', 'foreign_key' => '1'))), '{n}.DbAroTest.id');
|
||||
$result = Hash::extract($Aro->node(array('DbAroUserTest' => array('id' => '1', 'foreign_key' => '1'))), '{n}.DbAroTest.id');
|
||||
$expected = array(3, 2, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
Configure::write('DbAclbindMode', 'array');
|
||||
$result = Set::extract($Aro->node(array('DbAroUserTest' => array('id' => 4, 'foreign_key' => 2))), '{n}.DbAroTest.id');
|
||||
$result = Hash::extract($Aro->node(array('DbAroUserTest' => array('id' => 4, 'foreign_key' => 2))), '{n}.DbAroTest.id');
|
||||
$expected = array(4);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
@ -326,12 +324,12 @@ class AclNodeTest extends CakeTestCase {
|
|||
$Aro = new DbAroTest();
|
||||
$Model = new DbAroUserTest();
|
||||
$Model->id = 1;
|
||||
$result = Set::extract($Aro->node($Model), '{n}.DbAroTest.id');
|
||||
$result = Hash::extract($Aro->node($Model), '{n}.DbAroTest.id');
|
||||
$expected = array(3, 2, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$Model->id = 2;
|
||||
$result = Set::extract($Aro->node($Model), '{n}.DbAroTest.id');
|
||||
$result = Hash::extract($Aro->node($Model), '{n}.DbAroTest.id');
|
||||
$expected = array(4, 2, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
@ -379,7 +377,7 @@ class AclNodeTest extends CakeTestCase {
|
|||
$this->assertEquals($expected, $result);
|
||||
|
||||
$node = $Aro->node(array('TestPlugin.TestPluginAuthUser' => array('id' => 1, 'user' => 'mariano')));
|
||||
$result = Set::extract($node, '0.DbAroTest.id');
|
||||
$result = Hash::get($node, '0.DbAroTest.id');
|
||||
$expected = $Aro->id;
|
||||
$this->assertEquals($expected, $result);
|
||||
CakePlugin::unload('TestPlugin');
|
||||
|
|
|
@ -92,7 +92,7 @@ class ContainableBehaviorTest extends CakeTestCase {
|
|||
'Category' => 'name'
|
||||
)
|
||||
)));
|
||||
$this->assertEquals(array('id'), Set::extract('/ArticleFeatured/keep/Featured/fields', $r));
|
||||
$this->assertEquals(array('id'), Hash::extract($r, 'ArticleFeatured.keep.Featured.fields'));
|
||||
|
||||
$r = $this->_containments($this->Article, array(
|
||||
'Comment' => array(
|
||||
|
@ -109,29 +109,29 @@ class ContainableBehaviorTest extends CakeTestCase {
|
|||
$this->assertTrue(Set::matches('/User', $r));
|
||||
$this->assertTrue(Set::matches('/Article/keep/Comment', $r));
|
||||
$this->assertTrue(Set::matches('/Article/keep/User', $r));
|
||||
$this->assertEquals(array('comment', 'published'), Set::extract('/Article/keep/Comment/fields', $r));
|
||||
$this->assertEquals(array('user'), Set::extract('/Article/keep/User/fields', $r));
|
||||
$this->assertEquals(array('comment', 'published'), Hash::extract($r, 'Article.keep.Comment.fields'));
|
||||
$this->assertEquals(array('user'), Hash::extract($r, 'Article.keep.User.fields'));
|
||||
$this->assertTrue(Set::matches('/Comment/keep/Attachment', $r));
|
||||
$this->assertEquals(array('attachment'), Set::extract('/Comment/keep/Attachment/fields', $r));
|
||||
$this->assertEquals(array('attachment'), Hash::extract($r, 'Comment.keep.Attachment.fields'));
|
||||
|
||||
$r = $this->_containments($this->Article, array('Comment' => array('limit' => 1)));
|
||||
$this->assertEquals(array('Comment', 'Article'), array_keys($r));
|
||||
$result = Set::extract('/Comment/keep', $r);
|
||||
$result = Hash::extract($r, 'Comment[keep]');
|
||||
$this->assertEquals(array('keep' => array()), array_shift($result));
|
||||
$this->assertTrue(Set::matches('/Article/keep/Comment', $r));
|
||||
$result = Set::extract('/Article/keep/Comment/.', $r);
|
||||
$result = Hash::extract($r, 'Article.keep');
|
||||
$this->assertEquals(array('limit' => 1), array_shift($result));
|
||||
|
||||
$r = $this->_containments($this->Article, array('Comment.User'));
|
||||
$this->assertEquals(array('User', 'Comment', 'Article'), array_keys($r));
|
||||
|
||||
$result = Set::extract('/User/keep', $r);
|
||||
$result = Hash::extract($r, 'User[keep]');
|
||||
$this->assertEquals(array('keep' => array()), array_shift($result));
|
||||
|
||||
$result = Set::extract('/Comment/keep', $r);
|
||||
$result = Hash::extract($r, 'Comment[keep]');
|
||||
$this->assertEquals(array('keep' => array('User' => array())), array_shift($result));
|
||||
|
||||
$result = Set::extract('/Article/keep', $r);
|
||||
$result = Hash::extract($r, 'Article[keep]');
|
||||
$this->assertEquals(array('keep' => array('Comment' => array())), array_shift($result));
|
||||
|
||||
$r = $this->_containments($this->Tag, array('Article' => array('User' => array('Comment' => array(
|
||||
|
@ -192,7 +192,7 @@ class ContainableBehaviorTest extends CakeTestCase {
|
|||
$this->assertFalse(Set::matches('/Comment[user_id!=2]', $r));
|
||||
|
||||
$r = $this->Article->find('all', array('contain' => 'Comment.id DESC'));
|
||||
$ids = $descIds = Set::extract('/Comment[1]/id', $r);
|
||||
$ids = $descIds = Hash::extract($r, 'Comment[1].id');
|
||||
rsort($descIds);
|
||||
$this->assertEquals($ids, $descIds);
|
||||
|
||||
|
|
|
@ -631,8 +631,8 @@ class TranslateBehaviorTest extends CakeTestCase {
|
|||
$translations = array('title' => 'Title', 'content' => 'Content');
|
||||
$TestModel->bindTranslation($translations, false);
|
||||
$result = $TestModel->read(null, 1);
|
||||
$result['Title'] = Set::sort($result['Title'], '{n}.id', 'asc');
|
||||
$result['Content'] = Set::sort($result['Content'], '{n}.id', 'asc');
|
||||
$result['Title'] = Hash::sort($result['Title'], '{n}.id', 'asc');
|
||||
$result['Content'] = Hash::sort($result['Content'], '{n}.id', 'asc');
|
||||
$expected = array(
|
||||
'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated', 'locale' => 'cze', 'title' => 'Titulek #1', 'content' => 'Upraveny obsah #1'),
|
||||
'Title' => array(
|
||||
|
|
|
@ -140,8 +140,8 @@ class TreeBehaviorScopedTest extends CakeTestCase {
|
|||
|
||||
$this->Ad->id = 4;
|
||||
$result = $this->Ad->children();
|
||||
$this->assertEquals(array(6, 5), Set::extract('/Ad/id', $result));
|
||||
$this->assertEquals(array(2, 2), Set::extract('/Campaign/id', $result));
|
||||
$this->assertEquals(array(6, 5), Hash::extract($result, '{n}.Ad.id'));
|
||||
$this->assertEquals(array(2, 2), Hash::extract($result, '{n}.Campaign.id'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -156,8 +156,8 @@ class TreeBehaviorScopedTest extends CakeTestCase {
|
|||
|
||||
$this->Ad->id = 4;
|
||||
$result = $this->Ad->children();
|
||||
$this->assertEquals(array(5, 6), Set::extract('/Ad/id', $result));
|
||||
$this->assertEquals(array(2, 2), Set::extract('/Campaign/id', $result));
|
||||
$this->assertEquals(array(5, 6), Hash::extract($result, '{n}.Ad.id'));
|
||||
$this->assertEquals(array(2, 2), Hash::extract($result, '{n}.Campaign.id'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -102,7 +102,7 @@ class TestBehavior extends ModelBehavior {
|
|||
return null;
|
||||
break;
|
||||
case 'modify':
|
||||
return Set::extract($results, "{n}.{$model->alias}");
|
||||
return Hash::extract($results, "{n}.{$model->alias}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -839,7 +839,7 @@ class BehaviorCollectionTest extends CakeTestCase {
|
|||
$this->assertSame($expected, $result);
|
||||
|
||||
$Sample->Behaviors->attach('Test', array('beforeSave' => 'modify'));
|
||||
$expected = Set::insert($record, 'Sample.name', 'sample99 modified before');
|
||||
$expected = Hash::insert($record, 'Sample.name', 'sample99 modified before');
|
||||
$Sample->create();
|
||||
$result = $Sample->save($record);
|
||||
$expected['Sample']['id'] = $Sample->id;
|
||||
|
@ -849,14 +849,14 @@ class BehaviorCollectionTest extends CakeTestCase {
|
|||
$this->assertSame($record, $Sample->save($record));
|
||||
|
||||
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
|
||||
$expected = Set::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
|
||||
$expected = Hash::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
|
||||
$Sample->create();
|
||||
$result = $Sample->save($record);
|
||||
$expected['Sample']['id'] = $Sample->id;
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$Sample->Behaviors->attach('Test', array('beforeSave' => 'modify', 'afterSave' => 'modify'));
|
||||
$expected = Set::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create')));
|
||||
$expected = Hash::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create')));
|
||||
$Sample->create();
|
||||
$result = $Sample->save($record);
|
||||
$expected['Sample']['id'] = $Sample->id;
|
||||
|
@ -881,12 +881,12 @@ class BehaviorCollectionTest extends CakeTestCase {
|
|||
$record2 = $Sample->read(null, 1);
|
||||
|
||||
$Sample->Behaviors->attach('Test', array('afterSave' => 'on'));
|
||||
$expected = Set::merge($record2, array('Sample' => array('aftersave' => 'modified after')));
|
||||
$expected = Hash::merge($record2, array('Sample' => array('aftersave' => 'modified after')));
|
||||
$Sample->create();
|
||||
$this->assertSame($expected, $Sample->save($record2));
|
||||
|
||||
$Sample->Behaviors->attach('Test', array('afterSave' => 'modify'));
|
||||
$expected = Set::merge($record2, array('Sample' => array('name' => 'sample1 modified after')));
|
||||
$expected = Hash::merge($record2, array('Sample' => array('name' => 'sample1 modified after')));
|
||||
$Sample->create();
|
||||
$this->assertSame($expected, $Sample->save($record2));
|
||||
}
|
||||
|
|
|
@ -617,7 +617,7 @@ class DboSourceTest extends CakeTestCase {
|
|||
$this->testDb->logQuery('Query 2');
|
||||
|
||||
$log = $this->testDb->getLog(false, false);
|
||||
$result = Set::extract($log['log'], '/query');
|
||||
$result = Hash::extract($log['log'], '{n}.query');
|
||||
$expected = array('Query 1', 'Query 2');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
|
@ -900,4 +900,5 @@ class DboSourceTest extends CakeTestCase {
|
|||
$result = $db->buildJoinStatement($join);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -110,7 +110,7 @@ class ModelCrossSchemaHabtmTest extends BaseModelTest {
|
|||
),
|
||||
));
|
||||
$this->assertEquals(4, count($players));
|
||||
$wizards = Set::extract('/Guild[name=Wizards]', $players);
|
||||
$wizards = Hash::extract($players, '{n}.Guild.{n}[name=Wizards]');
|
||||
$this->assertEquals(1, count($wizards));
|
||||
|
||||
$players = $Player->find('all', array(
|
||||
|
@ -120,7 +120,7 @@ class ModelCrossSchemaHabtmTest extends BaseModelTest {
|
|||
),
|
||||
));
|
||||
$this->assertEquals(1, count($players));
|
||||
$wizards = Set::extract('/Guild', $players);
|
||||
$wizards = Hash::extract($players, '{n}.Guild.{n}');
|
||||
$this->assertEquals(2, count($wizards));
|
||||
}
|
||||
|
||||
|
@ -163,8 +163,7 @@ class ModelCrossSchemaHabtmTest extends BaseModelTest {
|
|||
),
|
||||
),
|
||||
));
|
||||
|
||||
$rangers = Set::extract('/Guild[name=Rangers]', $players);
|
||||
$rangers = Hash::extract($players, '{n}.Guild.{n}[name=Rangers]');
|
||||
$this->assertEquals(2, count($rangers));
|
||||
}
|
||||
|
||||
|
@ -227,7 +226,7 @@ class ModelCrossSchemaHabtmTest extends BaseModelTest {
|
|||
'Player.id' => 10,
|
||||
)
|
||||
));
|
||||
$spongeBobsArmors = Set::extract('/Armor', $spongebob);
|
||||
$spongeBobsArmors = Hash::extract($spongebob, '{n}.Armor.{n}');
|
||||
$this->assertEquals(4, count($spongeBobsArmors));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -456,7 +456,7 @@ class ModelIntegrationTest extends BaseModelTest {
|
|||
$result = $TestModel->find('all');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($TestModel->User->find('all'), '{n}.User.id');
|
||||
$result = Hash::extract($TestModel->User->find('all'), '{n}.User.id');
|
||||
$this->assertEquals(array('1', '2', '3', '4'), $result);
|
||||
$this->assertEquals($expected, $TestModel->find('all'));
|
||||
|
||||
|
@ -836,14 +836,14 @@ class ModelIntegrationTest extends BaseModelTest {
|
|||
|
||||
$players = $Player->find('all');
|
||||
$this->assertEquals(4 , count($players));
|
||||
$playersGuilds = Set::extract('/Guild/GuildsPlayer', $players);
|
||||
$playersGuilds = Hash::extract($players, '{n}.Guild.{n}.GuildsPlayer');
|
||||
$this->assertEquals(3 , count($playersGuilds));
|
||||
$playersArmors = Set::extract('/Armor/ArmorsPlayer', $players);
|
||||
$playersArmors = Hash::extract($players, '{n}.Armor.{n}.ArmorsPlayer');
|
||||
$this->assertEquals(3 , count($playersArmors));
|
||||
unset($players);
|
||||
|
||||
$larry = $Player->findByName('larry');
|
||||
$larrysArmor = Set::extract('/Armor/ArmorsPlayer', $larry);
|
||||
$larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer');
|
||||
$this->assertEquals(1 , count($larrysArmor));
|
||||
|
||||
$larry['Guild']['Guild'] = array(1, 3); // larry joins another guild
|
||||
|
@ -852,21 +852,20 @@ class ModelIntegrationTest extends BaseModelTest {
|
|||
unset($larry);
|
||||
|
||||
$larry = $Player->findByName('larry');
|
||||
$larrysGuild = Set::extract('/Guild/GuildsPlayer', $larry);
|
||||
$larrysGuild = Hash::extract($larry, 'Guild.{n}.GuildsPlayer');
|
||||
$this->assertEquals(2 , count($larrysGuild));
|
||||
$larrysArmor = Set::extract('/Armor/ArmorsPlayer', $larry);
|
||||
$larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer');
|
||||
$this->assertEquals(2 , count($larrysArmor));
|
||||
|
||||
$larrysArmorsPlayersIds = Set::extract('/Armor/ArmorsPlayer/id', $larry);
|
||||
$larrysArmorsPlayersIds = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer.id');
|
||||
|
||||
$Player->ArmorsPlayer->id = 3;
|
||||
$Player->ArmorsPlayer->saveField('broken', true); // larry's cloak broke
|
||||
|
||||
$larry = $Player->findByName('larry');
|
||||
$larrysArmor = Set::extract('/Armor/ArmorsPlayer', $larry);
|
||||
$larrysCloak = Set::extract('/ArmorsPlayer[armor_id=3]', $larrysArmor);
|
||||
$larrysCloak = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer[armor_id=3]', $larry);
|
||||
$this->assertNotEmpty($larrysCloak);
|
||||
$this->assertTrue($larrysCloak[0]['ArmorsPlayer']['broken']); // still broken
|
||||
$this->assertTrue($larrysCloak[0]['broken']); // still broken
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -898,7 +897,7 @@ class ModelIntegrationTest extends BaseModelTest {
|
|||
$this->assertEquals($columns, array_keys($result));
|
||||
|
||||
$types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime');
|
||||
$this->assertEquals(Set::extract(array_values($result), '{n}.type'), $types);
|
||||
$this->assertEquals(Hash::extract(array_values($result), '{n}.type'), $types);
|
||||
|
||||
$result = $Post->schema('body');
|
||||
$this->assertEquals('text', $result['type']);
|
||||
|
|
|
@ -80,7 +80,7 @@ class ModelReadTest extends BaseModelTest {
|
|||
|
||||
$this->assertEquals(2, count($result['SomethingElse']));
|
||||
|
||||
$doomed = Set::extract('/JoinThing/doomed', $result['SomethingElse']);
|
||||
$doomed = Hash::extract($result['SomethingElse'], '{n}.JoinThing.doomed');
|
||||
$this->assertTrue(in_array(true, $doomed));
|
||||
$this->assertTrue(in_array(false, $doomed));
|
||||
}
|
||||
|
@ -3015,7 +3015,7 @@ class ModelReadTest extends BaseModelTest {
|
|||
$this->loadFixtures('Apple', 'Sample');
|
||||
$Apple = new Apple();
|
||||
$result = $Apple->find('threaded');
|
||||
$result = Set::extract($result, '{n}.children');
|
||||
$result = Hash::extract($result, '{n}.children');
|
||||
$expected = array(array(), array(), array(), array(), array(), array(), array());
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
@ -3030,7 +3030,7 @@ class ModelReadTest extends BaseModelTest {
|
|||
$Model = new Person();
|
||||
$Model->recursive = -1;
|
||||
$result = $Model->find('threaded');
|
||||
$result = Set::extract($result, '{n}.children');
|
||||
$result = Hash::extract($result, '{n}.children');
|
||||
$expected = array(array(), array(), array(), array(), array(), array(), array());
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
|
@ -5061,19 +5061,19 @@ class ModelReadTest extends BaseModelTest {
|
|||
$this->loadFixtures('Author');
|
||||
$TestModel = new ModifiedAuthor();
|
||||
|
||||
$result = Set::extract($TestModel->find('all'), '/Author/user');
|
||||
$result = Hash::extract($TestModel->find('all'), '{n}.Author.user');
|
||||
$expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($TestModel->find('all', array('callbacks' => 'after')), '/Author/user');
|
||||
$result = Hash::extract($TestModel->find('all', array('callbacks' => 'after')), '{n}.Author.user');
|
||||
$expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($TestModel->find('all', array('callbacks' => 'before')), '/Author/user');
|
||||
$result = Hash::extract($TestModel->find('all', array('callbacks' => 'before')), '{n}.Author.user');
|
||||
$expected = array('mariano', 'nate', 'larry', 'garrett');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::extract($TestModel->find('all', array('callbacks' => false)), '/Author/user');
|
||||
$result = Hash::extract($TestModel->find('all', array('callbacks' => false)), '{n}.Author.user');
|
||||
$expected = array('mariano', 'nate', 'larry', 'garrett');
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
@ -6512,7 +6512,7 @@ class ModelReadTest extends BaseModelTest {
|
|||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
$result = Set::combine(
|
||||
$result = Hash::combine(
|
||||
$TestModel->find('all', array(
|
||||
'order' => 'Article.title ASC',
|
||||
'fields' => array('id', 'title')
|
||||
|
@ -6526,7 +6526,7 @@ class ModelReadTest extends BaseModelTest {
|
|||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::combine(
|
||||
$result = Hash::combine(
|
||||
$TestModel->find('all', array(
|
||||
'order' => 'Article.title ASC'
|
||||
)),
|
||||
|
@ -6563,7 +6563,7 @@ class ModelReadTest extends BaseModelTest {
|
|||
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::combine(
|
||||
$result = Hash::combine(
|
||||
$TestModel->find('all', array(
|
||||
'order' => 'Article.title ASC'
|
||||
)),
|
||||
|
@ -6602,7 +6602,7 @@ class ModelReadTest extends BaseModelTest {
|
|||
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Set::combine(
|
||||
$result = Hash::combine(
|
||||
$TestModel->find('all', array(
|
||||
'order' => 'Article.title ASC',
|
||||
'fields' => array('id', 'title', 'user_id')
|
||||
|
@ -7722,17 +7722,17 @@ class ModelReadTest extends BaseModelTest {
|
|||
|
||||
$Post->Author->virtualFields = array('joined' => 'Post.id * Author.id');
|
||||
$result = $Post->find('all');
|
||||
$result = Set::extract('{n}.Author.joined', $result);
|
||||
$result = Hash::extract($result, '{n}.Author.joined');
|
||||
$expected = array(1, 6, 3);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = $Post->find('all', array('order' => array('Author.joined' => 'ASC')));
|
||||
$result = Set::extract('{n}.Author.joined', $result);
|
||||
$result = Hash::extract($result, '{n}.Author.joined');
|
||||
$expected = array(1, 3, 6);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = $Post->find('all', array('order' => array('Author.joined' => 'DESC')));
|
||||
$result = Set::extract('{n}.Author.joined', $result);
|
||||
$result = Hash::extract($result, '{n}.Author.joined');
|
||||
$expected = array(6, 3, 1);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
|
|
@ -411,7 +411,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
$Category = new CategoryThread();
|
||||
$Category->belongsTo['ParentCategory']['counterCache'] = 'child_count';
|
||||
$Category->updateCounterCache(array('parent_id' => 5));
|
||||
$result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
|
||||
$result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count');
|
||||
$expected = array(1);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
@ -632,11 +632,11 @@ class ModelWriteTest extends BaseModelTest {
|
|||
$Node = new Node();
|
||||
$Node->set('id', 1);
|
||||
$result = $Node->read();
|
||||
$this->assertEquals(array('Second'), Set::extract('/ParentNode/name', $result));
|
||||
$this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
|
||||
|
||||
$Node->saveField('state', 10);
|
||||
$result = $Node->read();
|
||||
$this->assertEquals(array('Second'), Set::extract('/ParentNode/name', $result));
|
||||
$this->assertEquals(array('Second'), Hash::extract($result, 'ParentNode.{n}.name'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1570,9 +1570,9 @@ class ModelWriteTest extends BaseModelTest {
|
|||
$TestModel->save($data);
|
||||
$result = $TestModel->read(null, 1);
|
||||
$expected = array(4, 5);
|
||||
$this->assertEquals($expected, Set::extract('/JoinC/JoinAsJoinC/id', $result));
|
||||
$this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
|
||||
$expected = array('new record', 'new record');
|
||||
$this->assertEquals($expected, Set::extract('/JoinC/JoinAsJoinC/other', $result));
|
||||
$this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1680,7 +1680,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
$TestModel->id = 2;
|
||||
$TestModel->save($data);
|
||||
$result = $TestModel->findById(2);
|
||||
$result['Item'] = Set::sort($result['Item'], '{n}.id', 'asc');
|
||||
$result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
|
||||
$expected = array(
|
||||
'Portfolio' => array(
|
||||
'id' => 2,
|
||||
|
@ -2400,12 +2400,12 @@ class ModelWriteTest extends BaseModelTest {
|
|||
public function testUpdateMultiple() {
|
||||
$this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread');
|
||||
$TestModel = new Comment();
|
||||
$result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id');
|
||||
$result = Hash::extract($TestModel->find('all'), '{n}.Comment.user_id');
|
||||
$expected = array('2', '4', '1', '1', '1', '2');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2));
|
||||
$result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
|
||||
$result = Hash::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id');
|
||||
$expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
|
@ -2414,7 +2414,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
array('Comment.user_id' => 5)
|
||||
);
|
||||
$this->assertFalse(empty($result));
|
||||
$result = Set::extract(
|
||||
$result = Hash::extract(
|
||||
$TestModel->find('all', array(
|
||||
'conditions' => array(
|
||||
'Comment.user_id' => 5
|
||||
|
@ -3075,7 +3075,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
'First new comment',
|
||||
'Second new comment'
|
||||
);
|
||||
$result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = $TestModel->Comment->User->field('id', array('user' => 'newuser', 'password' => 'newuserpass'));
|
||||
|
@ -3098,7 +3098,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
'Third new comment',
|
||||
'Fourth new comment'
|
||||
);
|
||||
$result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = $TestModel->Comment->Attachment->field('id', array('attachment' => 'deepsaved'));
|
||||
|
@ -3850,7 +3850,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
'First new comment',
|
||||
'Second new comment'
|
||||
);
|
||||
$result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = $TestModel->saveAll(
|
||||
|
@ -3874,7 +3874,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
'Second new comment',
|
||||
'Third new comment'
|
||||
);
|
||||
$result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$TestModel->beforeSaveReturn = false;
|
||||
|
@ -3899,7 +3899,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
'Second new comment',
|
||||
'Third new comment'
|
||||
);
|
||||
$result = Set::extract(Set::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$result = Hash::extract(Hash::sort($result['Comment'], '{n}.id', 'ASC'), '{n}.comment');
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
|
@ -4492,7 +4492,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
* @return void
|
||||
*/
|
||||
public function testSaveAllValidateFirst() {
|
||||
$this->loadFixtures('Article', 'Comment', 'Attachment');
|
||||
$this->loadFixtures('Article', 'Comment', 'Attachment', 'User', 'ArticlesTag', 'Tag');
|
||||
$model = new Article();
|
||||
$model->deleteAll(true);
|
||||
|
||||
|
@ -4539,7 +4539,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
|
||||
$result = $model->Comment->find('all');
|
||||
$this->assertSame(count($result), 1);
|
||||
$result = Set::extract('/Comment/article_id', $result);
|
||||
$result = Hash::extract($result, '{n}.Comment.article_id');
|
||||
$this->assertEquals(4, $result[0]);
|
||||
|
||||
$model->deleteAll(true);
|
||||
|
@ -4570,6 +4570,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
* @return void
|
||||
*/
|
||||
public function testSaveAllValidateFirstAtomicFalse() {
|
||||
$this->loadFixtures('Something');
|
||||
$Something = new Something();
|
||||
$invalidData = array(
|
||||
array(
|
||||
|
@ -5177,7 +5178,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
'First new comment',
|
||||
'Second new comment'
|
||||
);
|
||||
$this->assertEquals($expected, Set::extract($result['Comment'], '{n}.comment'));
|
||||
$this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
|
||||
|
||||
$result = $TestModel->saveAssociated(
|
||||
array(
|
||||
|
@ -5200,7 +5201,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
'Second new comment',
|
||||
'Third new comment'
|
||||
);
|
||||
$this->assertEquals($expected, Set::extract($result['Comment'], '{n}.comment'));
|
||||
$this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
|
||||
|
||||
$TestModel->beforeSaveReturn = false;
|
||||
$result = $TestModel->saveAssociated(
|
||||
|
@ -5224,7 +5225,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
'Second new comment',
|
||||
'Third new comment'
|
||||
);
|
||||
$this->assertEquals($expected, Set::extract($result['Comment'], '{n}.comment'));
|
||||
$this->assertEquals($expected, Hash::extract($result['Comment'], '{n}.comment'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5840,7 +5841,7 @@ class ModelWriteTest extends BaseModelTest {
|
|||
|
||||
$result = $model->Comment->find('all');
|
||||
$this->assertSame(count($result), 1);
|
||||
$result = Set::extract('/Comment/article_id', $result);
|
||||
$result = Hash::extract($result, '{n}.Comment.article_id');
|
||||
$this->assertEquals(4, $result[0]);
|
||||
|
||||
$model->deleteAll(true);
|
||||
|
@ -6066,15 +6067,15 @@ class ModelWriteTest extends BaseModelTest {
|
|||
));
|
||||
$this->assertFalse(empty($result));
|
||||
|
||||
$result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
|
||||
$result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
|
||||
$this->assertEquals(array(5, 3, 4, 1), $result);
|
||||
|
||||
$this->assertTrue($model->updateAll(array('count' => 'count + 2')));
|
||||
$result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
|
||||
$result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
|
||||
$this->assertEquals(array(7, 5, 6, 3), $result);
|
||||
|
||||
$this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1')));
|
||||
$result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count')));
|
||||
$result = Hash::extract($model->find('all', array('fields' => 'count')), '{n}.DataTest.count');
|
||||
$this->assertEquals(array(6, 4, 5, 2), $result);
|
||||
}
|
||||
|
||||
|
|
|
@ -531,13 +531,13 @@ class HttpSocketTest extends CakeTestCase {
|
|||
foreach ($tests as $i => $test) {
|
||||
if (strpos($i, 'reset') === 0) {
|
||||
foreach ($test as $path => $val) {
|
||||
$expectation = Set::insert($expectation, $path, $val);
|
||||
$expectation = Hash::insert($expectation, $path, $val);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($test['expectation'])) {
|
||||
$expectation = Set::merge($expectation, $test['expectation']);
|
||||
$expectation = Hash::merge($expectation, $test['expectation']);
|
||||
}
|
||||
$this->Socket->request($test['request']);
|
||||
|
||||
|
|
2128
lib/Cake/Test/Case/Utility/HashTest.php
Normal file
2128
lib/Cake/Test/Case/Utility/HashTest.php
Normal file
File diff suppressed because it is too large
Load diff
|
@ -6937,7 +6937,7 @@ class FormHelperTest extends CakeTestCase {
|
|||
|
||||
$result = $this->Form->input('Contact.non_existing');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text required'),
|
||||
'div' => array('class' => 'input text'),
|
||||
'label' => array('for' => 'ContactNonExisting'),
|
||||
'Non Existing',
|
||||
'/label',
|
||||
|
|
|
@ -142,7 +142,7 @@ class CakeTestFixture {
|
|||
$records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
|
||||
|
||||
if ($records !== false && !empty($records)) {
|
||||
$this->records = Set::extract($records, '{n}.' . $model->alias);
|
||||
$this->records = Hash::extract($records, '{n}.' . $model->alias);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -288,7 +288,7 @@ class Debugger {
|
|||
'scope' => null,
|
||||
'exclude' => array('call_user_func_array', 'trigger_error')
|
||||
);
|
||||
$options = Set::merge($defaults, $options);
|
||||
$options = Hash::merge($defaults, $options);
|
||||
|
||||
$backtrace = debug_backtrace();
|
||||
$count = count($backtrace);
|
||||
|
|
980
lib/Cake/Utility/Hash.php
Normal file
980
lib/Cake/Utility/Hash.php
Normal file
|
@ -0,0 +1,980 @@
|
|||
<?php
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.Utility
|
||||
* @since CakePHP(tm) v 2.2.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::uses('String', 'Utility');
|
||||
|
||||
/**
|
||||
* Library of array functions for manipulating and extracting data
|
||||
* from arrays or 'sets' of data.
|
||||
*
|
||||
* `Hash` provides an improved interface, more consistent and
|
||||
* predictable set of features over `Set`. While it lacks the spotty
|
||||
* support for pseudo Xpath, its more fully featured dot notation provides
|
||||
* similar features in a more consistent implementation.
|
||||
*
|
||||
* @package Cake.Utility
|
||||
*/
|
||||
class Hash {
|
||||
|
||||
/**
|
||||
* Get a single value specified by $path out of $data.
|
||||
* Does not support the full dot notation feature set,
|
||||
* but is faster for simple read operations.
|
||||
*
|
||||
* @param array $data Array of data to operate on.
|
||||
* @param mixed $path The path being searched for. Either a dot
|
||||
* separated string, or an array of path segments.
|
||||
* @return mixed The value fetched from the array, or null.
|
||||
*/
|
||||
public static function get(array $data, $path) {
|
||||
if (empty($data) || empty($path)) {
|
||||
return null;
|
||||
}
|
||||
if (is_string($path)) {
|
||||
$parts = explode('.', $path);
|
||||
} else {
|
||||
$parts = $path;
|
||||
}
|
||||
while (($key = array_shift($parts)) !== null) {
|
||||
if (is_array($data) && isset($data[$key])) {
|
||||
$data =& $data[$key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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:
|
||||
*
|
||||
* - `{n}` Matches any numeric key, or integer.
|
||||
* - `{s}` Matches any string key.
|
||||
* - `Foo` Matches any key with the exact same value.
|
||||
*
|
||||
* There are a number of attribute operators:
|
||||
*
|
||||
* - `=`, `!=` Equality.
|
||||
* - `>`, `<`, `>=`, `<=` Value comparison.
|
||||
* - `=/.../` Regular expression pattern match.
|
||||
*
|
||||
* 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.
|
||||
* - `{n}.User[id]` Get the name of every user with an id key.
|
||||
* - `{n}.User[id>=2]` Get the name of every user with an id key greater than or equal to 2.
|
||||
* - `{n}.User[username=/^paul/]` Get User elements with username matching `^paul`.
|
||||
*
|
||||
* @param array $data The data to extract from.
|
||||
* @param string $path The path to extract.
|
||||
* @return array An array of the extracted values. Returns an empty array
|
||||
* if there are no matches.
|
||||
*/
|
||||
public static function extract(array $data, $path) {
|
||||
if (empty($path)) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
// Simple paths.
|
||||
if (!preg_match('/[{\[]/', $path)) {
|
||||
return (array)self::get($data, $path);
|
||||
}
|
||||
|
||||
if (strpos('[', $path) === false) {
|
||||
$tokens = explode('.', $path);
|
||||
} else {
|
||||
$tokens = String::tokenize($path, '.', '[', ']');
|
||||
}
|
||||
|
||||
$_key = '__set_item__';
|
||||
|
||||
$context = array($_key => array($data));
|
||||
|
||||
do {
|
||||
$token = array_shift($tokens);
|
||||
$next = array();
|
||||
|
||||
$conditions = false;
|
||||
$position = strpos($token, '[');
|
||||
if ($position !== false) {
|
||||
$conditions = substr($token, $position);
|
||||
$token = substr($token, 0, $position);
|
||||
}
|
||||
|
||||
foreach ($context[$_key] as $item) {
|
||||
foreach ($item as $k => $v) {
|
||||
if (self::_matchToken($k, $token)) {
|
||||
$next[] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Filter for attributes.
|
||||
if ($conditions) {
|
||||
$filter = array();
|
||||
foreach ($next as $item) {
|
||||
if (self::_matches($item, $conditions)) {
|
||||
$filter[] = $item;
|
||||
}
|
||||
}
|
||||
$next = $filter;
|
||||
}
|
||||
$context = array($_key => $next);
|
||||
|
||||
} while (!empty($tokens));
|
||||
return $context[$_key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check a key against a token.
|
||||
*
|
||||
* @param string $key The key in the array being searched.
|
||||
* @param string $token The token being matched.
|
||||
* @return boolean
|
||||
*/
|
||||
protected static function _matchToken($key, $token) {
|
||||
if ($token === '{n}') {
|
||||
return is_numeric($key);
|
||||
}
|
||||
if ($token === '{s}') {
|
||||
return is_string($key);
|
||||
}
|
||||
if (is_numeric($token)) {
|
||||
return ($key == $token);
|
||||
}
|
||||
return ($key === $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not $data matches the attribute patterns
|
||||
*
|
||||
* @param array $data Array of data to match.
|
||||
* @param string $selector The patterns to match.
|
||||
* @return boolean Fitness of expression.
|
||||
*/
|
||||
protected static function _matches(array $data, $selector) {
|
||||
preg_match_all(
|
||||
'/(\[ (?<attr>[^=><!]+?) (\s* (?<op>[><!]?[=]|[><]) \s* (?<val>[^\]]+) )? \])/x',
|
||||
$selector,
|
||||
$conditions,
|
||||
PREG_SET_ORDER
|
||||
);
|
||||
|
||||
$ok = true;
|
||||
while ($ok) {
|
||||
if (empty($conditions)) {
|
||||
break;
|
||||
}
|
||||
$cond = array_shift($conditions);
|
||||
$attr = $cond['attr'];
|
||||
$op = isset($cond['op']) ? $cond['op'] : null;
|
||||
$val = isset($cond['val']) ? $cond['val'] : null;
|
||||
|
||||
// Presence test.
|
||||
if (empty($op) && empty($val) && !isset($data[$attr])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Empty attribute = fail.
|
||||
if (!isset($data[$attr])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$prop = isset($data[$attr]) ? $data[$attr] : null;
|
||||
|
||||
// Pattern matches and other operators.
|
||||
if ($op === '=' && $val && $val[0] === '/') {
|
||||
if (!preg_match($val, $prop)) {
|
||||
return false;
|
||||
}
|
||||
} elseif (
|
||||
($op === '=' && $prop != $val) ||
|
||||
($op === '!=' && $prop == $val) ||
|
||||
($op === '>' && $prop <= $val) ||
|
||||
($op === '<' && $prop >= $val) ||
|
||||
($op === '>=' && $prop < $val) ||
|
||||
($op === '<=' && $prop > $val)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert $values into an array with the given $path. You can use
|
||||
* `{n}` and `{s}` elements to insert $data multiple times.
|
||||
*
|
||||
* @param array $data The data to insert into.
|
||||
* @param string $path The path to insert at.
|
||||
* @param mixed $values The values to insert.
|
||||
* @return array The data with $values inserted.
|
||||
*/
|
||||
public static function insert(array $data, $path, $values = null) {
|
||||
$tokens = explode('.', $path);
|
||||
if (strpos($path, '{') === false) {
|
||||
return self::_simpleOp('insert', $data, $tokens, $values);
|
||||
}
|
||||
|
||||
$token = array_shift($tokens);
|
||||
$nextPath = implode('.', $tokens);
|
||||
foreach ($data as $k => $v) {
|
||||
if (self::_matchToken($k, $token)) {
|
||||
$data[$k] = self::insert($v, $nextPath, $values);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a simple insert/remove operation.
|
||||
*
|
||||
* @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.
|
||||
* @return array $data.
|
||||
*/
|
||||
protected static function _simpleOp($op, $data, $path, $values = null) {
|
||||
$_list =& $data;
|
||||
|
||||
$count = count($path);
|
||||
$last = $count - 1;
|
||||
foreach ($path as $i => $key) {
|
||||
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
|
||||
$key = intval($key);
|
||||
}
|
||||
if ($op === 'insert') {
|
||||
if ($i === $last) {
|
||||
$_list[$key] = $values;
|
||||
return $data;
|
||||
}
|
||||
if (!isset($_list[$key])) {
|
||||
$_list[$key] = array();
|
||||
}
|
||||
$_list =& $_list[$key];
|
||||
if (!is_array($_list)) {
|
||||
$_list = array();
|
||||
}
|
||||
} elseif ($op === 'remove') {
|
||||
if ($i === $last) {
|
||||
unset($_list[$key]);
|
||||
return $data;
|
||||
}
|
||||
if (!isset($_list[$key])) {
|
||||
return $data;
|
||||
}
|
||||
$_list =& $_list[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove data matching $path from the $data array.
|
||||
* You can use `{n}` and `{s}` to remove multiple elements
|
||||
* from $data.
|
||||
*
|
||||
* @param array $data The data to operate on
|
||||
* @param string $path A path expression to use to remove.
|
||||
* @return array The modified array.
|
||||
*/
|
||||
public static function remove(array $data, $path) {
|
||||
$tokens = explode('.', $path);
|
||||
if (strpos($path, '{') === false) {
|
||||
return self::_simpleOp('remove', $data, $tokens);
|
||||
}
|
||||
|
||||
$token = array_shift($tokens);
|
||||
$nextPath = implode('.', $tokens);
|
||||
foreach ($data as $k => $v) {
|
||||
$match = self::_matchToken($k, $token);
|
||||
if ($match && is_array($v)) {
|
||||
$data[$k] = self::remove($v, $nextPath);
|
||||
} elseif ($match) {
|
||||
unset($data[$k]);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* to null (useful for Hash::merge). You can optionally group the values by what is obtained when
|
||||
* 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
|
||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::combine
|
||||
*/
|
||||
public static function combine(array $data, $keyPath, $valuePath = null, $groupPath = null) {
|
||||
if (empty($data)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
if (is_array($keyPath)) {
|
||||
$format = array_shift($keyPath);
|
||||
$keys = self::format($data, $keyPath, $format);
|
||||
} else {
|
||||
$keys = self::extract($data, $keyPath);
|
||||
}
|
||||
if (empty($keys)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
if (!empty($valuePath) && is_array($valuePath)) {
|
||||
$format = array_shift($valuePath);
|
||||
$vals = self::format($data, $valuePath, $format);
|
||||
} elseif (!empty($valuePath)) {
|
||||
$vals = self::extract($data, $valuePath);
|
||||
}
|
||||
|
||||
$count = count($keys);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$vals[$i] = isset($vals[$i]) ? $vals[$i] : null;
|
||||
}
|
||||
|
||||
if ($groupPath !== null) {
|
||||
$group = self::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;
|
||||
}
|
||||
}
|
||||
if (empty($vals)) {
|
||||
return array();
|
||||
}
|
||||
return array_combine($keys, $vals);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a formated series of values extracted from `$data`, using
|
||||
* `$format` as the format and `$paths` as the values to extract.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @param array $data Source array from which to extract the data
|
||||
* @param string $paths An array containing one or more Hash::extract()-style key paths
|
||||
* @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`
|
||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::format
|
||||
* @see sprintf()
|
||||
* @see Hash::extract()
|
||||
*/
|
||||
public static function format(array $data, array $paths, $format) {
|
||||
$extracted = array();
|
||||
$count = count($paths);
|
||||
|
||||
if (!$count) {
|
||||
return;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$extracted[] = self::extract($data, $paths[$i]);
|
||||
}
|
||||
$out = array();
|
||||
$data = $extracted;
|
||||
$count = count($data[0]);
|
||||
|
||||
$countTwo = count($data);
|
||||
for ($j = 0; $j < $count; $j++) {
|
||||
$args = array();
|
||||
for ($i = 0; $i < $countTwo; $i++) {
|
||||
if (array_key_exists($j, $data[$i])) {
|
||||
$args[] = $data[$i][$j];
|
||||
}
|
||||
}
|
||||
$out[] = vsprintf($format, $args);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return boolean 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) {
|
||||
if (empty($data) || empty($needle)) {
|
||||
return false;
|
||||
}
|
||||
$stack = array();
|
||||
|
||||
$i = 1;
|
||||
while (!empty($needle)) {
|
||||
$key = key($needle);
|
||||
$val = $needle[$key];
|
||||
unset($needle[$key]);
|
||||
|
||||
if (isset($data[$key]) && is_array($val)) {
|
||||
$next = $data[$key];
|
||||
unset($data[$key]);
|
||||
|
||||
if (!empty($val)) {
|
||||
$stack[] = array($val, $next);
|
||||
}
|
||||
} elseif (!isset($data[$key]) || $data[$key] != $val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($needle) && !empty($stack)) {
|
||||
list($needle, $data) = array_pop($stack);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether or not a given path exists in $data.
|
||||
* This method uses the same path syntax as Hash::extract()
|
||||
*
|
||||
* 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.
|
||||
* @return boolean Existence of path.
|
||||
* @see Hash::extract()
|
||||
*/
|
||||
public static function check(array $data, $path) {
|
||||
$results = self::extract($data, $path);
|
||||
if (!is_array($results)) {
|
||||
return false;
|
||||
}
|
||||
return count($results) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively filters a data set.
|
||||
*
|
||||
* @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.
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
return array_filter($data, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function for filtering.
|
||||
*
|
||||
* @param array $var Array to filter.
|
||||
* @return boolean
|
||||
*/
|
||||
protected static function _filter($var) {
|
||||
if ($var === 0 || $var === '0' || !empty($var)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::flatten
|
||||
*/
|
||||
public static function flatten(array $data, $separator = '.') {
|
||||
$result = array();
|
||||
$stack = array();
|
||||
$path = null;
|
||||
|
||||
reset($data);
|
||||
while (!empty($data)) {
|
||||
$key = key($data);
|
||||
$element = $data[$key];
|
||||
unset($data[$key]);
|
||||
|
||||
if (is_array($element)) {
|
||||
if (!empty($data)) {
|
||||
$stack[] = array($data, $path);
|
||||
}
|
||||
$data = $element;
|
||||
$path .= $key . $separator;
|
||||
} else {
|
||||
$result[$path . $key] = $element;
|
||||
}
|
||||
|
||||
if (empty($data) && !empty($stack)) {
|
||||
list($data, $path) = array_pop($stack);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand/unflattens an string to an array
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
public static function expand($data, $separator = '.') {
|
||||
$result = array();
|
||||
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
|
||||
);
|
||||
}
|
||||
$result = self::merge($result, $child);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function can be thought of as a hybrid between PHP's `array_merge` and `array_merge_recursive`.
|
||||
*
|
||||
* The difference between this method and the built-in ones, is that if an array key contains another array, then
|
||||
* Hash::merge() will behave in a recursive fashion (unlike `array_merge`). But it will not act recursively for
|
||||
* 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
|
||||
* @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);
|
||||
|
||||
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)) {
|
||||
$return[] = $val;
|
||||
} else {
|
||||
$return[$key] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::numeric
|
||||
*/
|
||||
public static function numeric(array $data) {
|
||||
if (empty($data)) {
|
||||
return false;
|
||||
}
|
||||
$values = array_values($data);
|
||||
$str = implode('', $values);
|
||||
return (bool)ctype_digit($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the dimensions of an array.
|
||||
* Only considers the dimension of the first element in the array.
|
||||
*
|
||||
* If you have an un-even or hetrogenous 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
|
||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::dimensions
|
||||
*/
|
||||
public static function dimensions(array $data) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the dimensions of *all* array elements. Useful for finding the maximum
|
||||
* number of dimensions in a mixed array.
|
||||
*
|
||||
* @param array $data Array to count dimensions on
|
||||
* @return integer 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) {
|
||||
$depth = array();
|
||||
if (is_array($data) && reset($data) !== false) {
|
||||
foreach ($data as $value) {
|
||||
$depth[] = self::dimensions((array)$value) + 1;
|
||||
}
|
||||
}
|
||||
return max($depth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a callback across all elements in a set.
|
||||
* Can be provided a path to only modify slices of the set.
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
public static function map(array $data, $path, $function) {
|
||||
$values = (array)self::extract($data, $path);
|
||||
return array_map($function, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduce a set of extracted values using `$function`.
|
||||
*
|
||||
* @param array $data The data to reduce.
|
||||
* @param string $path The path to extract from $data.
|
||||
* @return mixed The reduced value.
|
||||
*/
|
||||
public static function reduce(array $data, $path, $function) {
|
||||
$values = (array)self::extract($data, $path);
|
||||
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.
|
||||
*
|
||||
* @param array $data The data to reduce.
|
||||
* @param string $path The path to extract from $data.
|
||||
* @return mixed The results of the applied method.
|
||||
*/
|
||||
public static function apply(array $data, $path, $function) {
|
||||
$values = (array)self::extract($data, $path);
|
||||
return call_user_func($function, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts an array by any value, determined by a Set-compatible path
|
||||
*
|
||||
* ### Sort directions
|
||||
*
|
||||
* - `asc` Sort ascending.
|
||||
* - `desc` Sort descending.
|
||||
*
|
||||
* ## Sort types
|
||||
*
|
||||
* - `numeric` Sort by numeric value.
|
||||
* - `regular` Sort by numeric value.
|
||||
* - `string` Sort by numeric value.
|
||||
* - `natural` Sort by natural order. Requires PHP 5.4 or greater.
|
||||
*
|
||||
* @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'.
|
||||
* @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') {
|
||||
$originalKeys = array_keys($data);
|
||||
$numeric = is_numeric(implode('', $originalKeys));
|
||||
if ($numeric) {
|
||||
$data = array_values($data);
|
||||
}
|
||||
$sortValues = self::extract($data, $path);
|
||||
$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);
|
||||
}
|
||||
$result = self::_squash($sortValues);
|
||||
$keys = self::extract($result, '{n}.id');
|
||||
$values = self::extract($result, '{n}.value');
|
||||
|
||||
$dir = strtolower($dir);
|
||||
$type = strtolower($type);
|
||||
if ($type == 'natural' && version_compare(PHP_VERSION, '5.4.0', '<')) {
|
||||
$type == 'regular';
|
||||
}
|
||||
if ($dir === 'asc') {
|
||||
$dir = SORT_ASC;
|
||||
} else {
|
||||
$dir = SORT_DESC;
|
||||
}
|
||||
if ($type === 'numeric') {
|
||||
$type = SORT_NUMERIC;
|
||||
} elseif ($type === 'string') {
|
||||
$type = SORT_STRING;
|
||||
} elseif ($type === 'natural') {
|
||||
$type = SORT_NATURAL;
|
||||
} else {
|
||||
$type = SORT_REGULAR;
|
||||
}
|
||||
array_multisort($values, $dir, $type, $keys, $dir, $type);
|
||||
$sorted = array();
|
||||
$keys = array_unique($keys);
|
||||
|
||||
foreach ($keys as $k) {
|
||||
if ($numeric) {
|
||||
$sorted[] = $data[$k];
|
||||
continue;
|
||||
}
|
||||
if (isset($originalKeys[$k])) {
|
||||
$sorted[$originalKeys[$k]] = $data[$originalKeys[$k]];
|
||||
} else {
|
||||
$sorted[$k] = $data[$k];
|
||||
}
|
||||
}
|
||||
return $sorted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for sort()
|
||||
* Sqaushes an array to a single hash so it can be sorted.
|
||||
*
|
||||
* @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;
|
||||
if (!is_null($key)) {
|
||||
$id = $key;
|
||||
}
|
||||
if (is_array($r) && !empty($r)) {
|
||||
$stack = array_merge($stack, self::_squash($r, $id));
|
||||
} else {
|
||||
$stack[] = array('id' => $id, 'value' => $r);
|
||||
}
|
||||
}
|
||||
return $stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param mixed $data First value
|
||||
* @param mixed $compare Second value
|
||||
* @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))
|
||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::diff
|
||||
*/
|
||||
public static function diff(array $data, $compare) {
|
||||
if (empty($data)) {
|
||||
return (array)$compare;
|
||||
}
|
||||
if (empty($compare)) {
|
||||
return (array)$data;
|
||||
}
|
||||
$intersection = array_intersect_key($data, $compare);
|
||||
while (($key = key($intersection)) !== null) {
|
||||
if ($data[$key] == $compare[$key]) {
|
||||
unset($data[$key]);
|
||||
unset($compare[$key]);
|
||||
}
|
||||
next($intersection);
|
||||
}
|
||||
return $data + $compare;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the difference between $data and $push onto $data.
|
||||
*
|
||||
* @param array $data The data to append onto.
|
||||
* @param array $compare The data to compare and append onto.
|
||||
* @return array The merged array.
|
||||
*/
|
||||
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)) {
|
||||
$data[$key] = self::mergeDiff($data[$key], $compare[$key]);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes an array, and converts it to a standard format.
|
||||
*
|
||||
* @param mixed $data List to normalize
|
||||
* @param boolean $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
|
||||
*/
|
||||
public static function normalize(array $data, $assoc = true) {
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes in a flat array and returns a nested array
|
||||
*
|
||||
* ### Options:
|
||||
*
|
||||
* - `children` The key name to use in the resultset for children.
|
||||
* - `idPath` The path to a key that identifies each entry. Should be
|
||||
* compatible with Hash::extract(). Defaults to `{n}.$alias.id`
|
||||
* - `parentPath` The path to a key that identifies the parent of each entry.
|
||||
* Should be compatible with Hash::extract(). Defaults to `{n}.$alias.parent_id`
|
||||
* - `root` The id of the desired top-most result.
|
||||
*
|
||||
* @param mixed $data The data to nest.
|
||||
* @param array $options Options are:
|
||||
* @return array of results, nested
|
||||
* @see Hash::extract()
|
||||
*/
|
||||
public static function nest(array $data, $options = array()) {
|
||||
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();
|
||||
$ids = self::extract($data, $options['idPath']);
|
||||
|
||||
$idKeys = explode('.', $options['idPath']);
|
||||
array_shift($idKeys);
|
||||
|
||||
$parentKeys = explode('.', $options['parentPath']);
|
||||
array_shift($parentKeys);
|
||||
|
||||
foreach ($data as $result) {
|
||||
$result[$options['children']] = array();
|
||||
|
||||
$id = self::get($result, $idKeys);
|
||||
$parentId = self::get($result, $parentKeys);
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
if ($options['root']) {
|
||||
$root = $options['root'];
|
||||
} else {
|
||||
$root = self::get($return[0], $parentKeys);
|
||||
}
|
||||
|
||||
foreach ($return as $i => $result) {
|
||||
$id = self::get($result, $idKeys);
|
||||
$parentId = self::get($result, $parentKeys);
|
||||
if ($id !== $root && $parentId != $root) {
|
||||
unset($return[$i]);
|
||||
}
|
||||
}
|
||||
return array_values($return);
|
||||
}
|
||||
|
||||
}
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
|
||||
App::uses('String', 'Utility');
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* Class used for manipulation of arrays.
|
||||
|
@ -44,20 +45,13 @@ class Set {
|
|||
*/
|
||||
public static function merge($arr1, $arr2 = null) {
|
||||
$args = func_get_args();
|
||||
|
||||
$r = (array)current($args);
|
||||
while (($arg = next($args)) !== false) {
|
||||
foreach ((array)$arg as $key => $val) {
|
||||
if (!empty($r[$key]) && is_array($r[$key]) && is_array($val)) {
|
||||
$r[$key] = Set::merge($r[$key], $val);
|
||||
} elseif (is_int($key)) {
|
||||
$r[] = $val;
|
||||
} else {
|
||||
$r[$key] = $val;
|
||||
if (empty($args[1])) {
|
||||
return (array)$args[0];
|
||||
}
|
||||
if (!is_array($args[0])) {
|
||||
$args[0] = (array)$args[0];
|
||||
}
|
||||
}
|
||||
return $r;
|
||||
return call_user_func_array('Hash::merge', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,25 +62,7 @@ class Set {
|
|||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::filter
|
||||
*/
|
||||
public static function filter(array $var) {
|
||||
foreach ($var as $k => $v) {
|
||||
if (is_array($v)) {
|
||||
$var[$k] = Set::filter($v);
|
||||
}
|
||||
}
|
||||
return array_filter($var, array('Set', '_filter'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set::filter callback function
|
||||
*
|
||||
* @param array $var Array to filter.
|
||||
* @return boolean
|
||||
*/
|
||||
protected static function _filter($var) {
|
||||
if ($var === 0 || $var === '0' || !empty($var)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return Hash::filter($var);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -214,25 +190,7 @@ class Set {
|
|||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::numeric
|
||||
*/
|
||||
public static function numeric($array = null) {
|
||||
if (empty($array)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($array === range(0, count($array) - 1)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$numeric = true;
|
||||
$keys = array_keys($array);
|
||||
$count = count($keys);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
if (!is_numeric($array[$keys[$i]])) {
|
||||
$numeric = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $numeric;
|
||||
return Hash::numeric($array);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -667,29 +625,7 @@ class Set {
|
|||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::insert
|
||||
*/
|
||||
public static function insert($list, $path, $data = null) {
|
||||
if (!is_array($path)) {
|
||||
$path = explode('.', $path);
|
||||
}
|
||||
$_list =& $list;
|
||||
|
||||
$count = count($path);
|
||||
foreach ($path as $i => $key) {
|
||||
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
|
||||
$key = intval($key);
|
||||
}
|
||||
if ($i === $count - 1 && is_array($_list)) {
|
||||
$_list[$key] = $data;
|
||||
} else {
|
||||
if (!isset($_list[$key])) {
|
||||
$_list[$key] = array();
|
||||
}
|
||||
$_list =& $_list[$key];
|
||||
}
|
||||
if (!is_array($_list)) {
|
||||
$_list = array();
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
return Hash::insert($list, $path, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -701,28 +637,7 @@ class Set {
|
|||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::remove
|
||||
*/
|
||||
public static function remove($list, $path = null) {
|
||||
if (empty($path)) {
|
||||
return $list;
|
||||
}
|
||||
if (!is_array($path)) {
|
||||
$path = explode('.', $path);
|
||||
}
|
||||
$_list =& $list;
|
||||
|
||||
foreach ($path as $i => $key) {
|
||||
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
|
||||
$key = intval($key);
|
||||
}
|
||||
if ($i === count($path) - 1) {
|
||||
unset($_list[$key]);
|
||||
} else {
|
||||
if (!isset($_list[$key])) {
|
||||
return $list;
|
||||
}
|
||||
$_list =& $_list[$key];
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
return Hash::remove($list, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -858,32 +773,10 @@ class Set {
|
|||
}
|
||||
}
|
||||
if ($assoc) {
|
||||
return Set::normalize($list);
|
||||
return Hash::normalize($list);
|
||||
}
|
||||
} elseif (is_array($list)) {
|
||||
$keys = array_keys($list);
|
||||
$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[$list[$keys[$i]]] = null;
|
||||
} else {
|
||||
$newList[$keys[$i]] = $list[$keys[$i]];
|
||||
}
|
||||
}
|
||||
$list = $newList;
|
||||
}
|
||||
$list = Hash::normalize($list, $assoc);
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
@ -1012,28 +905,7 @@ class Set {
|
|||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::flatten
|
||||
*/
|
||||
public static function flatten($data, $separator = '.') {
|
||||
$result = array();
|
||||
$path = null;
|
||||
|
||||
if (is_array($separator)) {
|
||||
extract($separator, EXTR_OVERWRITE);
|
||||
}
|
||||
|
||||
if (!is_null($path)) {
|
||||
$path .= $separator;
|
||||
}
|
||||
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_array($val)) {
|
||||
$result += (array)Set::flatten($val, array(
|
||||
'separator' => $separator,
|
||||
'path' => $path . $key
|
||||
));
|
||||
} else {
|
||||
$result[$path . $key] = $val;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
return Hash::flatten($data, $separator);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1048,22 +920,7 @@ class Set {
|
|||
* @return array
|
||||
*/
|
||||
public static function expand($data, $separator = '.') {
|
||||
$result = array();
|
||||
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
|
||||
);
|
||||
}
|
||||
$result = Set::merge($result, $child);
|
||||
}
|
||||
return $result;
|
||||
return Hash::expand($data, $separator);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1245,18 +1102,7 @@ class Set {
|
|||
} else {
|
||||
$keys = $path;
|
||||
}
|
||||
if (!$keys) {
|
||||
return $input;
|
||||
}
|
||||
|
||||
$return = $input;
|
||||
foreach ($keys as $key) {
|
||||
if (!isset($return[$key])) {
|
||||
return null;
|
||||
}
|
||||
$return = $return[$key];
|
||||
}
|
||||
return $return;
|
||||
return Hash::get($input, $keys);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -471,7 +471,7 @@ class Helper extends Object {
|
|||
if ($setScope === true) {
|
||||
$this->_modelScope = $entity;
|
||||
}
|
||||
$parts = array_values(Set::filter(explode('.', $entity), true));
|
||||
$parts = array_values(Hash::filter(explode('.', $entity)));
|
||||
if (empty($parts)) {
|
||||
return;
|
||||
}
|
||||
|
@ -673,7 +673,7 @@ class Helper extends Object {
|
|||
|
||||
$entity = $this->entity();
|
||||
if (!empty($data) && !empty($entity)) {
|
||||
$result = Set::extract(implode('.', $entity), $data);
|
||||
$result = Hash::get($data, implode('.', $entity));
|
||||
}
|
||||
|
||||
$habtmKey = $this->field();
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
*/
|
||||
App::uses('ClassRegistry', 'Utility');
|
||||
App::uses('AppHelper', 'View/Helper');
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* Form helper library.
|
||||
|
@ -243,7 +244,7 @@ class FormHelper extends AppHelper {
|
|||
return true;
|
||||
} elseif (is_array($validateProperties)) {
|
||||
|
||||
$dims = Set::countDim($validateProperties);
|
||||
$dims = Hash::dimensions($validateProperties);
|
||||
if ($dims == 1 || ($dims == 2 && isset($validateProperties['rule']))) {
|
||||
$validateProperties = array($validateProperties);
|
||||
}
|
||||
|
@ -312,7 +313,7 @@ class FormHelper extends AppHelper {
|
|||
if (empty($errors)) {
|
||||
return false;
|
||||
}
|
||||
$errors = Set::classicExtract($errors, join('.', $entity));
|
||||
$errors = Hash::get($errors, join('.', $entity));
|
||||
return $errors === null ? false : $errors;
|
||||
}
|
||||
|
||||
|
@ -623,7 +624,7 @@ class FormHelper extends AppHelper {
|
|||
if (!$field) {
|
||||
$field = $this->entity();
|
||||
} elseif (is_string($field)) {
|
||||
$field = Set::filter(explode('.', $field), true);
|
||||
$field = Hash::filter(explode('.', $field));
|
||||
}
|
||||
|
||||
foreach ($this->_unlockedFields as $unlockField) {
|
||||
|
|
|
@ -53,7 +53,7 @@ class NumberHelper extends AppHelper {
|
|||
* @throws CakeException When the engine class could not be found.
|
||||
*/
|
||||
public function __construct(View $View, $settings = array()) {
|
||||
$settings = Set::merge(array('engine' => 'CakeNumber'), $settings);
|
||||
$settings = Hash::merge(array('engine' => 'CakeNumber'), $settings);
|
||||
parent::__construct($View, $settings);
|
||||
list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
|
||||
App::uses($engineClass, $plugin . 'Utility');
|
||||
|
|
|
@ -66,7 +66,7 @@ class TextHelper extends AppHelper {
|
|||
* @throws CakeException when the engine class could not be found.
|
||||
*/
|
||||
public function __construct(View $View, $settings = array()) {
|
||||
$settings = Set::merge(array('engine' => 'String'), $settings);
|
||||
$settings = Hash::merge(array('engine' => 'String'), $settings);
|
||||
parent::__construct($View, $settings);
|
||||
list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
|
||||
App::uses($engineClass, $plugin . 'Utility');
|
||||
|
|
|
@ -50,7 +50,7 @@ class TimeHelper extends AppHelper {
|
|||
* @throws CakeException When the engine class could not be found.
|
||||
*/
|
||||
public function __construct(View $View, $settings = array()) {
|
||||
$settings = Set::merge(array('engine' => 'CakeTime'), $settings);
|
||||
$settings = Hash::merge(array('engine' => 'CakeTime'), $settings);
|
||||
parent::__construct($View, $settings);
|
||||
list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
|
||||
App::uses($engineClass, $plugin . 'Utility');
|
||||
|
|
|
@ -141,6 +141,7 @@ App::$bootstrapping = true;
|
|||
|
||||
Configure::bootstrap(isset($boot) ? $boot : true);
|
||||
|
||||
|
||||
/**
|
||||
* Full url prefix
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue