Skip to content
Permalink
Browse files

Allow using Hash::extract() with objects implementing ArrayAccess.

  • Loading branch information...
ADmad committed Sep 29, 2015
1 parent 7800c25 commit 49a910229bc5fc74fc78474e3720d721a17081c3
Showing with 304 additions and 48 deletions.
  1. +7 −5 src/Utility/Hash.php
  2. +297 −43 tests/TestCase/Utility/HashTest.php
@@ -114,13 +114,13 @@ public static function get($data, $path, $default = null)
* - `{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 array|\ArrayAccess $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.
* @link http://book.cakephp.org/3.0/en/core-libraries/hash.html#Hash::extract
*/
public static function extract(array $data, $path)
public static function extract($data, $path)
{
if (empty($path)) {
return $data;
@@ -161,7 +161,9 @@ public static function extract(array $data, $path)
if ($conditions) {
$filter = [];
foreach ($next as $item) {
if (is_array($item) && static::_matches($item, $conditions)) {
if ((is_array($item) || $item instanceof ArrayAccess) &&
static::_matches($item, $conditions)
) {
$filter[] = $item;
}
}
@@ -215,11 +217,11 @@ protected static function _matchToken($key, $token)
/**
* Checks whether or not $data matches the attribute patterns
*
* @param array $data Array of data to match.
* @param array|\ArrayAccess $data Array of data to match.
* @param string $selector The patterns to match.
* @return bool Fitness of expression.
*/
protected static function _matches(array $data, $selector)
protected static function _matches($data, $selector)
{
preg_match_all(
'/(\[ (?P<attr>[^=><!]+?) (\s* (?P<op>[><!]?[=]|[><]) \s* (?P<val>(?:\/.*?\/ | [^\]]+)) )? \])/x',
Oops, something went wrong.

0 comments on commit 49a9102

Please sign in to comment.
You can’t perform that action at this time.