Skip to content

Commit f46432f

Browse files
author
Robert Pustułka
committed
Move toArrayResponse() method to Http\Response.
1 parent d5d6664 commit f46432f

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/Http/Response.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Cake\Filesystem\File;
1919
use Cake\Http\Cookie\Cookie;
2020
use Cake\Http\Cookie\CookieCollection;
21+
use Cake\Http\Cookie\CookieInterface;
2122
use Cake\Log\Log;
2223
use Cake\Network\CorsBuilder;
2324
use Cake\Network\Exception\NotFoundException;
@@ -1938,7 +1939,9 @@ public function cookie($options = null)
19381939
return null;
19391940
}
19401941

1941-
return $this->_cookies->get($options)->toArrayResponse();
1942+
$cookie = $this->_cookies->get($options);
1943+
1944+
return $this->toArrayResponse($cookie);
19421945
}
19431946

19441947
$options += [
@@ -2042,7 +2045,9 @@ public function getCookie($name)
20422045
return null;
20432046
}
20442047

2045-
return $this->_cookies->get($name)->toArrayResponse();
2048+
$cookie = $this->_cookies->get($name);
2049+
2050+
return $this->toArrayResponse($cookie);
20462051
}
20472052

20482053
/**
@@ -2056,12 +2061,42 @@ public function getCookies()
20562061
{
20572062
$out = [];
20582063
foreach ($this->_cookies as $cookie) {
2059-
$out[$cookie->getName()] = $cookie->toArrayResponse();
2064+
$out[$cookie->getName()] = $this->toArrayResponse($cookie);
20602065
}
20612066

20622067
return $out;
20632068
}
20642069

2070+
/**
2071+
* Convert the cookie into an array of its properties.
2072+
*
2073+
* This method is compatible with the historical behavior of Cake\Http\Response,
2074+
* where `httponly` is `httpOnly` and `expires` is `expire`
2075+
*
2076+
* @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
2077+
* @return array
2078+
*/
2079+
protected function toArrayResponse(CookieInterface $cookie)
2080+
{
2081+
if ($cookie instanceof Cookie) {
2082+
return $cookie->toArrayResponse();
2083+
} elseif ($cookie->getExpiry()) {
2084+
$expires = $cookie->getExpiry()->format('U');
2085+
} else {
2086+
$expires = '';
2087+
}
2088+
2089+
return [
2090+
'name' => $cookie->getName(),
2091+
'value' => $cookie->getValue(),
2092+
'path' => $cookie->getPath(),
2093+
'domain' => $cookie->getDomain(),
2094+
'secure' => $cookie->isSecure(),
2095+
'httpOnly' => $cookie->isHttpOnly(),
2096+
'expire' => $expires
2097+
];
2098+
}
2099+
20652100
/**
20662101
* Get the CookieCollection from the response
20672102
*

0 commit comments

Comments
 (0)