Permalink
Browse files

Simplify CookieComponent::read()

Also, this commit fixes an issue of when the second level key is empty.
Previously, read('foo.0') returned incorrect result.
  • Loading branch information...
chinpei215 committed Oct 16, 2017
1 parent bbea910 commit 19bbb7da17dd97ae3daa4d8a4987a333516a9e34
@@ -261,22 +261,7 @@ public function read($key = null) {
if ($key === null) {
return $this->_values[$this->name];
}
if (strpos($key, '.') !== false) {
$names = explode('.', $key, 2);
$key = $names[0];
}
if (!isset($this->_values[$this->name][$key])) {
return null;
}
if (!empty($names[1])) {
if (is_array($this->_values[$this->name][$key])) {
return Hash::get($this->_values[$this->name][$key], $names[1]);
}
return null;
}
return $this->_values[$this->name][$key];
return Hash::get($this->_values[$this->name], $key);
}
/**
@@ -714,6 +714,20 @@ public function testReadEmpty() {
$this->assertEquals(array(), $this->Cookie->read('Array'));
}
/**
* Test reading empty key
*
* @return void
*/
public function testReadEmptyKey() {
$_COOKIE['CakeTestCookie'] = array(
'0' => '{"name":"value"}',
'foo' => array('bar'),
);
$this->assertEquals('value', $this->Cookie->read('0.name'));
$this->assertEquals('bar', $this->Cookie->read('foo.0'));
}
/**
* test that no error is issued for non array data.
*

0 comments on commit 19bbb7d

Please sign in to comment.