ソースを参照

Add getCookie() and replace cookie()

This method is more consistent with getData() and getQuery() in that it
supports dotted paths and default values.
Mark Story 9 年 前
コミット
3e6954ccc0
2 ファイル変更28 行追加7 行削除
  1. 14 1
      src/Http/ServerRequest.php
  2. 14 6
      tests/TestCase/Network/RequestTest.php

+ 14 - 1
src/Http/ServerRequest.php

@@ -1389,7 +1389,7 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
      *
      * @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 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
      *
      * @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)
     {
@@ -1553,6 +1554,18 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
     }
 
     /**
+     * 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.
      *
      * @return array An array of cookie data.

+ 14 - 6
tests/TestCase/Network/RequestTest.php

@@ -3009,18 +3009,26 @@ XML;
      *
      * @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'));
     }
 
     /**