diff --git a/src/Http/Client/Response.php b/src/Http/Client/Response.php index dd1449684a4..ac68b761dcb 100644 --- a/src/Http/Client/Response.php +++ b/src/Http/Client/Response.php @@ -442,7 +442,7 @@ public function getCookieData($name) return null; } - return $this->cookies->get($name)->toArrayCompat(); + return $this->cookies->get($name)->toArrayClient(); } /** @@ -469,7 +469,7 @@ protected function _getCookies() $cookies = []; foreach ($this->cookies as $cookie) { - $cookies[$cookie->getName()] = $cookie->toArrayCompat(); + $cookies[$cookie->getName()] = $cookie->toArrayClient(); } return $cookies; diff --git a/src/Http/Cookie/Cookie.php b/src/Http/Cookie/Cookie.php index cee7a63e66e..98f3268d364 100644 --- a/src/Http/Cookie/Cookie.php +++ b/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 @@ public function __construct( $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 @@ public function toArray() * * @return array */ - public function toArrayCompat() + public function toArrayClient() { return [ 'name' => $this->getName(), @@ -644,6 +647,27 @@ public function toArrayCompat() ]; } + /** + * 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 * diff --git a/tests/TestCase/Http/Cookie/CookieTest.php b/tests/TestCase/Http/Cookie/CookieTest.php index 9ff079cf029..96dab5c9998 100644 --- a/tests/TestCase/Http/Cookie/CookieTest.php +++ b/tests/TestCase/Http/Cookie/CookieTest.php @@ -549,11 +549,11 @@ public function testToArray() } /** - * 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 @@ public function testToArrayCompat() '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()); } }