From fa19c3458063d16c1bcafb4328fd95b554f9841a Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 12 Jun 2014 11:30:48 -0400 Subject: [PATCH] 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 --- lib/Cake/Test/Case/Utility/HashTest.php | 9 +++++++++ lib/Cake/Utility/Hash.php | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index f85ac6a86..96476315a 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -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]); diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index 57531bcc3..359311a12 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -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'; }