public function add(Cookie $cookie) { $result = $cookie->validate(); if ($result !== true) { if ($this->strictMode) { throw new InvalidCookieException($result); } else { return false; } } foreach ($this->cookies as $i => $c) { if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getPorts() != $cookie->getPorts() || $c->getName() != $cookie->getName()) { continue; } if (!$cookie->getDiscard() && $c->getDiscard()) { unset($this->cookies[$i]); continue; } if ($cookie->getExpires() > $c->getExpires()) { unset($this->cookies[$i]); continue; } if ($cookie->getValue() !== $c->getValue()) { unset($this->cookies[$i]); continue; } return false; } $this->cookies[] = $cookie; return true; }
public function add(Cookie $cookie) { // Only allow cookies with set and valid domain, name, value $result = $cookie->validate(); if ($result !== true) { if ($this->strictMode) { throw new InvalidCookieException($result); } else { $this->removeCookieIfEmpty($cookie); return false; } } // Resolve conflicts with previously set cookies foreach ($this->cookies as $i => $c) { // Two cookies are identical, when their path, domain, port and name are identical if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getPorts() != $cookie->getPorts() || $c->getName() != $cookie->getName()) { continue; } // The previously set cookie is a discard cookie and this one is not so allow the new cookie to be set if (!$cookie->getDiscard() && $c->getDiscard()) { unset($this->cookies[$i]); continue; } // If the new cookie's expiration is further into the future, then replace the old cookie if ($cookie->getExpires() > $c->getExpires()) { unset($this->cookies[$i]); continue; } // If the value has changed, we better change it if ($cookie->getValue() !== $c->getValue()) { unset($this->cookies[$i]); continue; } // The cookie exists, so no need to continue return false; } $this->cookies[] = $cookie; return true; }
/** * @dataProvider cookieValidateProvider */ public function testValidatesCookies($name, $value, $domain, $result) { $cookie = new Cookie(array('name' => $name, 'value' => $value, 'domain' => $domain)); $this->assertSame($result, $cookie->validate()); }