diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index 43f40e5d601..0212885eb1a 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -836,6 +836,28 @@ public function testExtractStringKey() { $this->assertEquals(array('foo'), $result); } +/** + * Test wildcard matcher + * + * @return void + */ + public function testExtractWildcard() { + $data = array( + '02000009C5560001' => array('name' => 'Mr. Alphanumeric'), + '2300000918020101' => array('name' => 'Mr. Numeric'), + '390000096AB30001' => array('name' => 'Mrs. Alphanumeric'), + 'stuff' => array('name' => 'Ms. Word'), + ); + $result = Hash::extract($data, '{*}.name'); + $expected = array( + 'Mr. Alphanumeric', + 'Mr. Numeric', + 'Mrs. Alphanumeric', + 'Ms. Word', + ); + $this->assertEquals($expected, $result); + } + /** * Test the attribute presense selector. * diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index 897ab64c614..142c72b0a26 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -76,6 +76,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: @@ -171,16 +172,16 @@ protected static function _splitConditions($token) { * @return bool */ protected static function _matchToken($key, $token) { - if ($token === '{n}') { - return is_numeric($key); + switch ($token) { + case '{n}': + return is_numeric($key); + case '{s}': + return is_string($key); + case '{*}': + return true; + default: + return is_numeric($token) ? ($key == $token) : $key === $token; } - if ($token === '{s}') { - return is_string($key); - } - if (is_numeric($token)) { - return ($key == $token); - } - return ($key === $token); } /**