Skip to content

Commit

Permalink
Add wildcard matcher to Hash::extract()
Browse files Browse the repository at this point in the history
Port changes from #6447 from 2.7 into 3.0
  • Loading branch information
markstory committed May 3, 2015
1 parent bc2d222 commit 7c8c540
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Utility/Hash.php
Expand Up @@ -89,6 +89,7 @@ public static function get(array $data, $path, $default = null)
*
* - `{n}` Matches any numeric key, or integer.
* - `{s}` Matches any string key.
* - `{*}` Matches any value.
* - `Foo` Matches any key with the exact same value.
*
* There are a number of attribute operators:
Expand Down Expand Up @@ -191,16 +192,16 @@ protected static function _splitConditions($token)
*/
protected static function _matchToken($key, $token)
{
if ($token === '{n}') {
return is_numeric($key);
switch ($token) {
case '{n}':
return is_numeric($key);
case '{s}':
return is_string($key);
case '{*}':
return true;
default:
return is_numeric($token) ? ($key == $token) : $key === $token;
}
if ($token === '{s}') {
return is_string($key);
}
if (is_numeric($token)) {
return ($key == $token);
}
return ($key === $token);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/TestCase/Utility/HashTest.php
Expand Up @@ -845,6 +845,33 @@ public function testExtractStringKey()
$this->assertEquals(['foo'], $result);
}

/**
* Test wildcard matcher
*
* @return void
*/
public function testExtractWildcard()
{
$data = [
'02000009C5560001' => ['name' => 'Mr. Alphanumeric'],
'2300000918020101' => ['name' => 'Mr. Numeric'],
'390000096AB30001' => ['name' => 'Mrs. Alphanumeric'],
'stuff' => ['name' => 'Ms. Word'],
123 => ['name' => 'Mr. Number'],
true => ['name' => 'Ms. Bool'],
];
$result = Hash::extract($data, '{*}.name');
$expected = [
'Mr. Alphanumeric',
'Mr. Numeric',
'Mrs. Alphanumeric',
'Ms. Word',
'Mr. Number',
'Ms. Bool',
];
$this->assertEquals($expected, $result);
}

/**
* Test the attribute presense selector.
*
Expand Down

0 comments on commit 7c8c540

Please sign in to comment.