Skip to content

Commit

Permalink
Add Cookie::toArray()
Browse files Browse the repository at this point in the history
This method belongs here as all the data is here.
  • Loading branch information
markstory committed Mar 31, 2017
1 parent 17c3cbd commit f4d5c01
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
10 changes: 1 addition & 9 deletions src/Http/Client/CookieCollection.php
Expand Up @@ -78,15 +78,7 @@ public function getAll()
{
$out = [];
foreach ($this->cookies as $cookie) {
$out[] = [
'name' => $cookie->getName(),
'value' => $cookie->getValue(),
'path' => $cookie->getPath(),
'domain' => $cookie->getDomain(),
'secure' => $cookie->isSecure(),
'httponly' => $cookie->isHttpOnly(),
'expires' => $cookie->getExpiry()
];
$out[] = $cookie->toArray();
}

return $out;
Expand Down
20 changes: 20 additions & 0 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -596,6 +596,26 @@ public function isExpanded()
return $this->isExpanded;
}

/**
* Convert the cookie into an array of its properties.
*
* Primarily useful where backwards compatibility is needed.
*
* @return array
*/
public function toArray()
{
return [
'name' => $this->getName(),
'value' => $this->getValue(),
'path' => $this->getPath(),
'domain' => $this->getDomain(),
'secure' => $this->isSecure(),
'httponly' => $this->isHttpOnly(),
'expires' => $this->getExpiry()
];
}

/**
* Implode method to keep keys are multidimensional arrays
*
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -521,4 +521,30 @@ public function testGetId()
$cookie = new Cookie('test', 'val', null, '/path', 'example.com');
$this->assertEquals('test;example.com;/path', $cookie->getId());
}

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

0 comments on commit f4d5c01

Please sign in to comment.