Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Mar 31, 2017
1 parent f4d5c01 commit e489778
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Http/Cookie/Cookie.php
Expand Up @@ -48,6 +48,13 @@ class Cookie implements CookieInterface

use CookieCryptTrait;

/**
* Expires attribute format.
*
* @var string
*/
const EXPIRES_FORMAT = 'D, d-M-Y H:i:s T';

/**
* Cookie name
*
Expand Down Expand Up @@ -160,7 +167,7 @@ protected function _buildExpirationValue()
{
return sprintf(
'expires=%s',
gmdate('D, d-M-Y H:i:s T', $this->expiresAt)
gmdate(static::EXPIRES_FORMAT, $this->expiresAt)
);
}

Expand Down Expand Up @@ -616,6 +623,27 @@ public function toArray()
];
}

/**
* Convert the cookie into an array of its properties.
*
* This method is compatible with older client code that
* expects date strings instead of timestamps.
*
* @return array
*/
public function toArrayCompat()
{
return [
'name' => $this->getName(),
'value' => $this->getValue(),
'path' => $this->getPath(),
'domain' => $this->getDomain(),
'secure' => $this->isSecure(),
'httponly' => $this->isHttpOnly(),
'expires' => gmdate(static::EXPIRES_FORMAT, $this->expiresAt)
];
}

/**
* Implode method to keep keys are multidimensional arrays
*
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -547,4 +547,30 @@ public function testToArray()
];
$this->assertEquals($expected, $cookie->toArray());
}

/**
* Test toArrayCompat
*
* @return void
*/
public function testToArrayCompat()
{
$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',
'expires' => $date->format(DATE_COOKIE),
'secure' => true,
'httponly' => true
];
$this->assertEquals($expected, $cookie->toArrayCompat());
}
}

0 comments on commit e489778

Please sign in to comment.