Skip to content

Commit 74e1c4f

Browse files
author
Robert Pustułka
committed
Moves Cookie::toArray() to Client\CookieCollection::convertCookie()
as it's only a backcompat method for http client.
1 parent c3c8516 commit 74e1c4f

File tree

5 files changed

+52
-57
lines changed

5 files changed

+52
-57
lines changed

src/Http/Client/CookieCollection.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Cake\Http\Client;
1515

1616
use Cake\Http\Cookie\CookieCollection as BaseCollection;
17+
use Cake\Http\Cookie\CookieInterface;
1718

1819
/**
1920
* Container class for cookies used in Http\Client.
@@ -32,7 +33,7 @@ class CookieCollection extends BaseCollection
3233
* Store the cookies that haven't expired. If a cookie has been expired
3334
* and is currently stored, it will be removed.
3435
*
35-
* @param \Cake\Http\Client\Response $response The response to read cookies from
36+
* @param Response $response The response to read cookies from
3637
* @param string $url The request URL used for default host/path values.
3738
* @return void
3839
*/
@@ -78,11 +79,32 @@ public function getAll()
7879
{
7980
$out = [];
8081
foreach ($this->cookies as $cookie) {
81-
$out[] = $cookie->toArray();
82+
$out[] = $this->convertCookie($cookie);
8283
}
8384

8485
return $out;
8586
}
87+
88+
/**
89+
* Convert the cookie into an array of its properties.
90+
*
91+
* Primarily useful where backwards compatibility is needed.
92+
*
93+
* @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
94+
* @return array
95+
*/
96+
public function convertCookie(CookieInterface $cookie)
97+
{
98+
return [
99+
'name' => $cookie->getName(),
100+
'value' => $cookie->getValue(),
101+
'path' => $cookie->getPath(),
102+
'domain' => $cookie->getDomain(),
103+
'secure' => $cookie->isSecure(),
104+
'httponly' => $cookie->isHttpOnly(),
105+
'expires' => $cookie->getExpiresTimestamp()
106+
];
107+
}
86108
}
87109

88110
// @deprecated Add backwards compat alias.

src/Http/Cookie/Cookie.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -599,26 +599,6 @@ public function isExpanded()
599599
return $this->isExpanded;
600600
}
601601

602-
/**
603-
* Convert the cookie into an array of its properties.
604-
*
605-
* Primarily useful where backwards compatibility is needed.
606-
*
607-
* @return array
608-
*/
609-
public function toArray()
610-
{
611-
return [
612-
'name' => $this->getName(),
613-
'value' => $this->getValue(),
614-
'path' => $this->getPath(),
615-
'domain' => $this->getDomain(),
616-
'secure' => $this->isSecure(),
617-
'httponly' => $this->isHttpOnly(),
618-
'expires' => $this->getExpiresTimestamp()
619-
];
620-
}
621-
622602
/**
623603
* Implode method to keep keys are multidimensional arrays
624604
*

src/Http/Cookie/CookieInterface.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,4 @@ public function withSecure($secure);
191191
* @return string
192192
*/
193193
public function toHeaderValue();
194-
195-
/**
196-
* Convert the cookie into an array of its properties.
197-
*
198-
* Primarily useful where backwards compatibility is needed.
199-
*
200-
* @return array
201-
*/
202-
public function toArray();
203194
}

tests/TestCase/Http/Client/CookieCollectionTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
*/
1414
namespace Cake\Test\TestCase\Http\Client;
1515

16+
use Cake\Chronos\Chronos;
1617
use Cake\Http\Client\CookieCollection;
1718
use Cake\Http\Client\Response;
19+
use Cake\Http\Cookie\Cookie;
1820
use Cake\TestSuite\TestCase;
1921

2022
/**
@@ -301,4 +303,30 @@ public function testGetMatchingDomainWithDot()
301303
$expected = [];
302304
$this->assertEquals($expected, $result);
303305
}
306+
307+
/**
308+
* Test convertCookie
309+
*
310+
* @return void
311+
*/
312+
public function testConvertCookie()
313+
{
314+
$date = Chronos::parse('2017-03-31 12:34:56');
315+
$cookie = new Cookie('cakephp', 'cakephp-rocks');
316+
$cookie = $cookie->withDomain('cakephp.org')
317+
->withPath('/api')
318+
->withExpiry($date)
319+
->withHttpOnly(true)
320+
->withSecure(true);
321+
$expected = [
322+
'name' => 'cakephp',
323+
'value' => 'cakephp-rocks',
324+
'path' => '/api',
325+
'domain' => 'cakephp.org',
326+
'expires' => $date->format('U'),
327+
'secure' => true,
328+
'httponly' => true
329+
];
330+
$this->assertEquals($expected, $this->cookies->convertCookie($cookie));
331+
}
304332
}

tests/TestCase/Http/Cookie/CookieTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -542,30 +542,4 @@ public function testGetId()
542542
$cookie = new Cookie('test', 'val', null, '/path', 'example.com');
543543
$this->assertEquals('test;example.com;/path', $cookie->getId());
544544
}
545-
546-
/**
547-
* Test toArray
548-
*
549-
* @return void
550-
*/
551-
public function testToArray()
552-
{
553-
$date = Chronos::parse('2017-03-31 12:34:56');
554-
$cookie = new Cookie('cakephp', 'cakephp-rocks');
555-
$cookie = $cookie->withDomain('cakephp.org')
556-
->withPath('/api')
557-
->withExpiry($date)
558-
->withHttpOnly(true)
559-
->withSecure(true);
560-
$expected = [
561-
'name' => 'cakephp',
562-
'value' => 'cakephp-rocks',
563-
'path' => '/api',
564-
'domain' => 'cakephp.org',
565-
'expires' => $date->format('U'),
566-
'secure' => true,
567-
'httponly' => true
568-
];
569-
$this->assertEquals($expected, $cookie->toArray());
570-
}
571545
}

0 commit comments

Comments
 (0)