Fix Hash::extract() not matching 1/0 to true/false.

Historically [prop=1] has matched prop=true as well. This restores that
and also fixes [prop=0] not finding falsey values.

This makes the typing less strict in Hash::extract() but I don't think
it is unreasonable given PHP's type juggling.

Refs #3288
This commit is contained in:
mark_story 2014-06-12 11:30:48 -04:00
parent d8b4c3967c
commit fa19c34580
2 changed files with 13 additions and 1 deletions

View file

@ -868,10 +868,19 @@ class HashTest extends CakeTestCase {
'active' => false
),
);
$result = Hash::extract($users, '{n}[active=0]');
$this->assertCount(1, $result);
$this->assertEquals($users[2], $result[0]);
$result = Hash::extract($users, '{n}[active=false]');
$this->assertCount(1, $result);
$this->assertEquals($users[2], $result[0]);
$result = Hash::extract($users, '{n}[active=1]');
$this->assertCount(2, $result);
$this->assertEquals($users[0], $result[0]);
$this->assertEquals($users[1], $result[1]);
$result = Hash::extract($users, '{n}[active=true]');
$this->assertCount(2, $result);
$this->assertEquals($users[0], $result[0]);

View file

@ -208,7 +208,10 @@ class Hash {
if (isset($data[$attr])) {
$prop = $data[$attr];
}
if ($prop === true || $prop === false) {
$isBool = is_bool($prop);
if ($isBool && is_numeric($val)) {
$prop = $prop ? '1' : '0';
} elseif ($isBool) {
$prop = $prop ? 'true' : 'false';
}