From 3e6954ccc0b0704101c52978a4cc3e10f4559202 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 10 Nov 2016 19:32:13 -0500 Subject: [PATCH] Add getCookie() and replace cookie() This method is more consistent with getData() and getQuery() in that it supports dotted paths and default values. --- src/Http/ServerRequest.php | 15 ++++++++++++++- tests/TestCase/Network/RequestTest.php | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Http/ServerRequest.php b/src/Http/ServerRequest.php index 3647c127eba..0bf526995ec 100644 --- a/src/Http/ServerRequest.php +++ b/src/Http/ServerRequest.php @@ -1389,7 +1389,7 @@ protected function _parseAcceptWithQualifier($header) * * @param string|null $name Query string variable name or null to read all. * @return string|array|null The value being read - * @deprecated 3.4.0 Use getQuery() instead. + * @deprecated 3.4.0 Use getQuery() and withQueryParams() instead. */ public function query($name = null) { @@ -1542,6 +1542,7 @@ public function input($callback = null, ...$args) * * @param string $key The key you want to read. * @return null|string Either the cookie value, or null if the value doesn't exist. + * @deprecated 3.4.0 Use getCookie() instead. */ public function cookie($key) { @@ -1552,6 +1553,18 @@ public function cookie($key) return null; } + /** + * Read cookie data from the request's cookie data. + * + * @param string $key The key you want to read. + * @param string $default The default value if the cookie is not set. + * @return null|string Either the cookie value, or null if the value doesn't exist. + */ + public function getCookie($key, $default = null) + { + return Hash::get($this->cookies, $key, $default); + } + /** * Get all the cookie data from the request. * diff --git a/tests/TestCase/Network/RequestTest.php b/tests/TestCase/Network/RequestTest.php index 96cdb7aacd2..f4b547f14fc 100644 --- a/tests/TestCase/Network/RequestTest.php +++ b/tests/TestCase/Network/RequestTest.php @@ -3009,18 +3009,26 @@ public function testIsRequested() * * @return void */ - public function testCookie() + public function testGetCookie() { $request = new Request([ 'cookies' => [ - 'testing' => 'A value in the cookie' + 'testing' => 'A value in the cookie', + 'user' => [ + 'remember' => '1' + ] ] ]); - $result = $request->cookie('testing'); - $this->assertEquals('A value in the cookie', $result); + $this->assertEquals('A value in the cookie', $request->cookie('testing')); + $this->assertEquals('A value in the cookie', $request->getCookie('testing')); + + $this->assertNull($request->cookie('not there')); + $this->assertNull($request->getCookie('not there')); + $this->assertSame('default', $request->getCookie('not there', 'default')); - $result = $request->cookie('not there'); - $this->assertNull($result); + $this->assertSame('1', $request->getCookie('user.remember')); + $this->assertSame('1', $request->getCookie('user.remember', 'default')); + $this->assertSame('default', $request->getCookie('user.not there', 'default')); } /**