Creates a Cookie instance from a Set-Cookie header value.
public static fromString ( string $cookie, string $url = null ) : |
||
$cookie | string | A Set-Cookie header value |
$url | string | The base URL |
Résultat | A Cookie instance |
/** * Parse the standard `Header-name: value' headers into * individual header name/value pairs * * @param string $header */ private function parseHeader($header) { if (empty($header)) { return; } $pos = strpos($header, ": "); if (false !== $pos) { $name = trim(substr($header, 0, $pos)); $value = substr($header, $pos + 2); if (strtolower($name) == "set-cookie") { $cookie = CookieParser::fromString($value); $this->cookies[] = new Cookie($cookie->getName(), $cookie->getRawValue(), (int) $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly()); } else { $this->headers[$name] = $value; } } }
/** * {@inheritdoc} */ public function updateFromResponse(Response $response, $uri = null) { foreach ($response->getHeader('Set-Cookie', false) as $cookie) { $this->set(Cookie::fromString(urldecode($cookie), $uri)); } }
/** * @dataProvider getExpireCookieStrings */ public function testFromStringAcceptsSeveralExpiresDateFormats($cookie) { $this->assertEquals(1596185377, Cookie::fromString($cookie)->getExpiresTime()); }
public function testFromStringWithCapitalization() { $this->assertEquals('Foo=Bar; path=/', (string) Cookie::fromString('Foo=Bar')); $this->assertEquals('foo=bar; expires=Fri, 31 Dec 2010 23:59:59 GMT; path=/', (string) Cookie::fromString('foo=bar; Expires=Fri, 31 Dec 2010 23:59:59 GMT')); $this->assertEquals('foo=bar; domain=www.example.org; path=/; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly')); }
public function testFromStringThrowsAnExceptionIfUrlIsNotValid() { $this->setExpectedException('InvalidArgumentException'); Cookie::fromString('foo=bar', 'foobar'); }
/** * @dataProvider getTestsForToFromString */ public function testToFromString($cookie) { $this->assertEquals($cookie, (string) Cookie::fromString($cookie)); }