Browse Source

Add another compatibility shim for cookies on the response.

Rename the existing compat method as there are two now.
Mark Story 9 years ago
parent
commit
5547d37045

+ 2 - 2
src/Http/Client/Response.php

@@ -442,7 +442,7 @@ class Response extends Message implements ResponseInterface
             return null;
         }
 
-        return $this->cookies->get($name)->toArrayCompat();
+        return $this->cookies->get($name)->toArrayClient();
     }
 
     /**
@@ -469,7 +469,7 @@ class Response extends Message implements ResponseInterface
 
         $cookies = [];
         foreach ($this->cookies as $cookie) {
-            $cookies[$cookie->getName()] = $cookie->toArrayCompat();
+            $cookies[$cookie->getName()] = $cookie->toArrayClient();
         }
 
         return $cookies;

+ 28 - 4
src/Http/Cookie/Cookie.php

@@ -121,7 +121,7 @@ class Cookie implements CookieInterface
      * @link http://php.net/manual/en/function.setcookie.php
      * @param string $name Cookie name
      * @param string|array $value Value of the cookie
-     * @param \DateTimeInterface|null $expiresAt Expiration time and date
+     * @param \DateTimeInterface|int|null $expiresAt Expiration time and date
      * @param string $path Path
      * @param string $domain Domain
      * @param bool $secure Is secure
@@ -130,7 +130,7 @@ class Cookie implements CookieInterface
     public function __construct(
         $name,
         $value = '',
-        DateTimeInterface $expiresAt = null,
+        $expiresAt = null,
         $path = '',
         $domain = '',
         $secure = false,
@@ -153,7 +153,10 @@ class Cookie implements CookieInterface
         $this->validateBool($secure);
         $this->secure = $secure;
 
-        if ($expiresAt !== null) {
+        if (is_int($expiresAt)) {
+            $this->expiresAt = $expiresAt;
+        }
+        if ($expiresAt instanceof DateTimeInterface) {
             $this->expiresAt = (int)$expiresAt->format('U');
         }
     }
@@ -631,7 +634,7 @@ class Cookie implements CookieInterface
      *
      * @return array
      */
-    public function toArrayCompat()
+    public function toArrayClient()
     {
         return [
             'name' => $this->getName(),
@@ -645,6 +648,27 @@ class Cookie implements CookieInterface
     }
 
     /**
+     * Convert the cookie into an array of its properties.
+     *
+     * This method is compatible with the historical behavior of Cake\Http\Response,
+     * where `httponly` is `httpOnly` and `expires` is `expire`
+     *
+     * @return array
+     */
+    public function toArrayResponse()
+    {
+        return [
+            'name' => $this->getName(),
+            'value' => $this->getValue(),
+            'path' => $this->getPath(),
+            'domain' => $this->getDomain(),
+            'secure' => $this->isSecure(),
+            'httpOnly' => $this->isHttpOnly(),
+            'expire' => $this->expiresAt
+        ];
+    }
+
+    /**
      * Implode method to keep keys are multidimensional arrays
      *
      * @param array $array Map of key and values

+ 29 - 3
tests/TestCase/Http/Cookie/CookieTest.php

@@ -549,11 +549,11 @@ class CookieTest extends TestCase
     }
 
     /**
-     * Test toArrayCompat
+     * Test toArrayClient
      *
      * @return void
      */
-    public function testToArrayCompat()
+    public function testToArrayClient()
     {
         $date = Chronos::parse('2017-03-31 12:34:56');
         $cookie = new Cookie('cakephp', 'cakephp-rocks');
@@ -571,6 +571,32 @@ class CookieTest extends TestCase
             'secure' => true,
             'httponly' => true
         ];
-        $this->assertEquals($expected, $cookie->toArrayCompat());
+        $this->assertEquals($expected, $cookie->toArrayClient());
+    }
+
+    /**
+     * Test toArrayResponse
+     *
+     * @return void
+     */
+    public function testToArrayResponse()
+    {
+        $date = Chronos::parse('2017-03-31 12:34:56');
+        $cookie = new Cookie('cakephp', 'cakephp-rocks');
+        $cookie = $cookie->withDomain('cakephp.org')
+            ->withPath('/api')
+            ->withExpiry($date)
+            ->withHttpOnly(true)
+            ->withSecure(true);
+        $expected = [
+            'name' => 'cakephp',
+            'value' => 'cakephp-rocks',
+            'path' => '/api',
+            'domain' => 'cakephp.org',
+            'expire' => $date->format('U'),
+            'secure' => true,
+            'httpOnly' => true
+        ];
+        $this->assertEquals($expected, $cookie->toArrayResponse());
     }
 }