public static function fromHeader(string $string) : Cookie
{
$parts = array_filter(array_map('trim', explode(';', $string)));
if (empty($parts) || !strpos($parts[0], '=')) {
throw new InvalidValueException('Invalid cookie format.');
}
list($name, $value) = array_map('trim', explode('=', array_shift($parts), 2));
$expires = 0;
$path = '';
$domain = '';
$secure = false;
$httpOnly = false;
foreach ($parts as $part) {
$pieces = array_map('trim', explode('=', $part, 2));
$key = strtolower($pieces[0]);
if (1 === count($pieces)) {
switch ($key) {
case 'secure':
$secure = true;
break;
case 'httponly':
$httpOnly = true;
break;
}
} else {
switch ($key) {
case 'expires':
$time = \DateTime::createFromFormat('D, j M Y G:i:s T', $pieces[1]);
if (false === $time) {
break;
}
$time = $time->getTimestamp();
$expires = 0 === $expires ? $time : min($time, $expires);
break;
case 'max-age':
$time = trim($pieces[1]);
if (ctype_digit($time)) {
break;
}
$time = time() + (int) $time;
$expires = 0 === $expires ? $time : min($time, $expires);
break;
case 'path':
$path = $pieces[1];
break;
case 'domain':
$domain = $pieces[1];
break;
}
}
}
return new self($name, $value, $expires, $path, $domain, $secure, $httpOnly);
}