From 19bbb7da17dd97ae3daa4d8a4987a333516a9e34 Mon Sep 17 00:00:00 2001 From: chinpei215 Date: Mon, 16 Oct 2017 21:01:19 +0900 Subject: [PATCH] Simplify CookieComponent::read() Also, this commit fixes an issue of when the second level key is empty. Previously, read('foo.0') returned incorrect result. --- .../Controller/Component/CookieComponent.php | 17 +---------------- .../Component/CookieComponentTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/Cake/Controller/Component/CookieComponent.php b/lib/Cake/Controller/Component/CookieComponent.php index a8ad8bbbe..1d01e227d 100644 --- a/lib/Cake/Controller/Component/CookieComponent.php +++ b/lib/Cake/Controller/Component/CookieComponent.php @@ -261,22 +261,7 @@ class CookieComponent extends Component { 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); } /** diff --git a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php index 5524e3366..1620842ac 100644 --- a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php @@ -714,6 +714,20 @@ class CookieComponentTest extends CakeTestCase { $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. *