Merge pull request #6464 from cakephp/issue-6447

Add the {*} matcher to Hash::extract()
This commit is contained in:
José Lorenzo Rodríguez 2015-05-02 09:23:57 +02:00
commit 1cc7fced2f
2 changed files with 36 additions and 9 deletions

View file

@ -836,6 +836,32 @@ class HashTest extends CakeTestCase {
$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'),
123 => array('name' => 'Mr. Number'),
true => array('name' => 'Ms. Bool'),
);
$result = Hash::extract($data, '{*}.name');
$expected = array(
'Mr. Alphanumeric',
'Mr. Numeric',
'Mrs. Alphanumeric',
'Ms. Word',
'Mr. Number',
'Ms. Bool',
);
$this->assertEquals($expected, $result);
}
/**
* Test the attribute presense selector.
*

View file

@ -76,6 +76,7 @@ class Hash {
*
* - `{n}` Matches any numeric key, or integer.
* - `{s}` Matches any string key.
* - `{*}` Matches any value.
* - `Foo` Matches any key with the exact same value.
*
* There are a number of attribute operators:
@ -171,16 +172,16 @@ class Hash {
* @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);
}
/**