13 private $cookies = [];
25 public function __construct($strictMode =
false, $cookieArray = [])
27 $this->strictMode = $strictMode;
29 foreach ($cookieArray as $cookie) {
45 public static function fromArray(array $cookies, $domain)
47 $cookieJar =
new self();
48 foreach ($cookies as $name => $value) {
49 $cookieJar->setCookie(
new SetCookie([
78 $allowSessionCookies =
false
80 if ($cookie->getExpires() || $allowSessionCookies) {
81 if (!$cookie->getDiscard()) {
98 if ($name ===
null || !is_scalar($name)) {
101 foreach ($this->cookies as $cookie) {
102 if ($cookie->getName() !==
null && strcasecmp($cookie->getName(), $name) === 0) {
112 return array_map(
function (SetCookie $cookie) {
113 return $cookie->toArray();
117 public function clear($domain =
null, $path =
null, $name =
null)
123 $this->cookies = array_filter(
125 function (
SetCookie $cookie) use ($domain) {
130 $this->cookies = array_filter(
132 function (SetCookie $cookie) use ($path, $domain) {
133 return !($cookie->matchesPath($path) &&
134 $cookie->matchesDomain($domain));
138 $this->cookies = array_filter(
140 function (SetCookie $cookie) use ($path, $domain, $name) {
141 return !($cookie->getName() == $name &&
142 $cookie->matchesPath($path) &&
143 $cookie->matchesDomain($domain));
151 $this->cookies = array_filter(
153 function (SetCookie $cookie) {
154 return !$cookie->getDiscard() && $cookie->getExpires();
159 public function setCookie(SetCookie $cookie)
163 $name = $cookie->getName();
164 if (!$name && $name !==
'0') {
169 $result = $cookie->validate();
170 if ($result !==
true) {
171 if ($this->strictMode) {
172 throw new \RuntimeException(
'Invalid cookie: ' . $result);
174 $this->removeCookieIfEmpty($cookie);
180 foreach ($this->cookies as $i => $c) {
184 if ($c->getPath() != $cookie->getPath() ||
185 $c->getDomain() != $cookie->getDomain() ||
186 $c->getName() != $cookie->getName()
193 if (!$cookie->getDiscard() && $c->getDiscard()) {
194 unset($this->cookies[$i]);
200 if ($cookie->getExpires() > $c->getExpires()) {
201 unset($this->cookies[$i]);
206 if ($cookie->getValue() !== $c->getValue()) {
207 unset($this->cookies[$i]);
215 $this->cookies[] = $cookie;
220 public function count()
222 return count($this->cookies);
227 return new \ArrayIterator(array_values($this->cookies));
234 if ($cookieHeader = $response->getHeader(
'Set-Cookie')) {
235 foreach ($cookieHeader as $cookie) {
237 if (!$sc->getDomain()) {
238 $sc->setDomain($request->
getUri()->getHost());
240 if (0 !== strpos($sc->getPath(),
'/')) {
241 $sc->setPath($this->getCookiePathFromRequest($request));
256 private function getCookiePathFromRequest(RequestInterface $request)
258 $uriPath = $request->getUri()->getPath();
259 if (
'' === $uriPath) {
262 if (0 !== strpos($uriPath,
'/')) {
265 if (
'/' === $uriPath) {
268 if (0 === $lastSlashPos = strrpos($uriPath,
'/')) {
272 return substr($uriPath, 0, $lastSlashPos);
278 $uri = $request->getUri();
279 $scheme = $uri->getScheme();
280 $host = $uri->getHost();
281 $path = $uri->getPath() ?:
'/';
283 foreach ($this->cookies as $cookie) {
284 if ($cookie->matchesPath($path) &&
285 $cookie->matchesDomain($host) &&
286 !$cookie->isExpired() &&
287 (!$cookie->getSecure() || $scheme ===
'https')
289 $values[] = $cookie->getName() .
'='
290 . $cookie->getValue();
295 ? $request->withHeader(
'Cookie', implode(
'; ', $values))
305 private function removeCookieIfEmpty(SetCookie $cookie)
307 $cookieValue = $cookie->getValue();
308 if ($cookieValue ===
null || $cookieValue ===
'') {
310 $cookie->getDomain(),