mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Add the {*} matcher to Hash::extract()
This matcher will match anything and is useful when you just want to traverse through data and you're not too picky. I've also refactored the conditions to use a case as it is slightly more readable and uses fewer lines of code. Refs #6447
This commit is contained in:
parent
345d8d8390
commit
5e9d4893a8
2 changed files with 32 additions and 9 deletions
|
@ -836,6 +836,28 @@ class HashTest extends CakeTestCase {
|
||||||
$this->assertEquals(array('foo'), $result);
|
$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.
|
* Test the attribute presense selector.
|
||||||
*
|
*
|
||||||
|
|
|
@ -76,6 +76,7 @@ class Hash {
|
||||||
*
|
*
|
||||||
* - `{n}` Matches any numeric key, or integer.
|
* - `{n}` Matches any numeric key, or integer.
|
||||||
* - `{s}` Matches any string key.
|
* - `{s}` Matches any string key.
|
||||||
|
* - `{*}` Matches any value.
|
||||||
* - `Foo` Matches any key with the exact same value.
|
* - `Foo` Matches any key with the exact same value.
|
||||||
*
|
*
|
||||||
* There are a number of attribute operators:
|
* There are a number of attribute operators:
|
||||||
|
@ -171,16 +172,16 @@ class Hash {
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected static function _matchToken($key, $token) {
|
protected static function _matchToken($key, $token) {
|
||||||
if ($token === '{n}') {
|
switch ($token) {
|
||||||
return is_numeric($key);
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue