Skip to content

Commit e489778

Browse files
committed
Add toArrayCompat()
This method is necessary as the existing Http\Client code expects the original date format to be returned. Having this method allows us to have less duplication across the framework.
1 parent f4d5c01 commit e489778

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/Http/Cookie/Cookie.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class Cookie implements CookieInterface
4848

4949
use CookieCryptTrait;
5050

51+
/**
52+
* Expires attribute format.
53+
*
54+
* @var string
55+
*/
56+
const EXPIRES_FORMAT = 'D, d-M-Y H:i:s T';
57+
5158
/**
5259
* Cookie name
5360
*
@@ -160,7 +167,7 @@ protected function _buildExpirationValue()
160167
{
161168
return sprintf(
162169
'expires=%s',
163-
gmdate('D, d-M-Y H:i:s T', $this->expiresAt)
170+
gmdate(static::EXPIRES_FORMAT, $this->expiresAt)
164171
);
165172
}
166173

@@ -616,6 +623,27 @@ public function toArray()
616623
];
617624
}
618625

626+
/**
627+
* Convert the cookie into an array of its properties.
628+
*
629+
* This method is compatible with older client code that
630+
* expects date strings instead of timestamps.
631+
*
632+
* @return array
633+
*/
634+
public function toArrayCompat()
635+
{
636+
return [
637+
'name' => $this->getName(),
638+
'value' => $this->getValue(),
639+
'path' => $this->getPath(),
640+
'domain' => $this->getDomain(),
641+
'secure' => $this->isSecure(),
642+
'httponly' => $this->isHttpOnly(),
643+
'expires' => gmdate(static::EXPIRES_FORMAT, $this->expiresAt)
644+
];
645+
}
646+
619647
/**
620648
* Implode method to keep keys are multidimensional arrays
621649
*

tests/TestCase/Http/Cookie/CookieTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,30 @@ public function testToArray()
547547
];
548548
$this->assertEquals($expected, $cookie->toArray());
549549
}
550+
551+
/**
552+
* Test toArrayCompat
553+
*
554+
* @return void
555+
*/
556+
public function testToArrayCompat()
557+
{
558+
$date = Chronos::parse('2017-03-31 12:34:56');
559+
$cookie = new Cookie('cakephp', 'cakephp-rocks');
560+
$cookie = $cookie->withDomain('cakephp.org')
561+
->withPath('/api')
562+
->withExpiry($date)
563+
->withHttpOnly(true)
564+
->withSecure(true);
565+
$expected = [
566+
'name' => 'cakephp',
567+
'value' => 'cakephp-rocks',
568+
'path' => '/api',
569+
'domain' => 'cakephp.org',
570+
'expires' => $date->format(DATE_COOKIE),
571+
'secure' => true,
572+
'httponly' => true
573+
];
574+
$this->assertEquals($expected, $cookie->toArrayCompat());
575+
}
550576
}

0 commit comments

Comments
 (0)