Skip to content

Commit

Permalink
Allow false/true to be read as keys in Hash::get().
Browse files Browse the repository at this point in the history
While these are not values within the documented types, there exist use
cases in CakeSession that necessitate these to be supported types.

Refs #10196
  • Loading branch information
markstory committed Feb 14, 2017
1 parent aa8d708 commit 3f10a02
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/Cake/Test/Case/Utility/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,13 @@ public function testGet() {
*/
public function testGetEmptyKey() {
$data = array(
'' => 'some value'
true => 'true value',
false => 'false value',
'' => 'some value',
);
$result = Hash::get($data, '');
$this->assertSame($data[''], $result);
$this->assertSame($data[''], Hash::get($data, ''));
$this->assertSame($data[false], Hash::get($data, false));
$this->assertSame($data[true], Hash::get($data, true));
}

/**
Expand All @@ -249,7 +252,7 @@ public function testGetEmptyKey() {
* @return void
*/
public function testGetInvalidPath() {
Hash::get(array('one' => 'two'), true);
Hash::get(array('one' => 'two'), new StdClass());
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/Cake/Utility/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public static function get(array $data, $path, $default = null) {
}
if (is_string($path) || is_numeric($path)) {
$parts = explode('.', $path);
} elseif (is_bool($path) || $path === null) {
$parts = [$path];
} else {
if (!is_array($path)) {
throw new InvalidArgumentException(__d('cake_dev',
'Invalid Parameter %s, should be dot separated path or array.',
$path
'Invalid path parameter: %s, should be dot separated path or array.',
var_export($path, true)
));
}
$parts = $path;
Expand Down

0 comments on commit 3f10a02

Please sign in to comment.