public static function createFromRawSetCookieHeader($header)
{
$nameValueAndUnparsedAttributes = explode(';', $header, 2);
$expectedNameValuePair = $nameValueAndUnparsedAttributes[0];
$unparsedAttributes = isset($nameValueAndUnparsedAttributes[1]) ? $nameValueAndUnparsedAttributes[1] : '';
if (strpos($expectedNameValuePair, '=') === false) {
return null;
}
$cookieNameAndValue = explode('=', $expectedNameValuePair, 2);
$cookieName = trim($cookieNameAndValue[0]);
$cookieValue = isset($cookieNameAndValue[1]) ? trim($cookieNameAndValue[1]) : '';
if ($cookieName === '') {
return null;
}
$expiresAttribute = 0;
$maxAgeAttribute = null;
$domainAttribute = null;
$pathAttribute = null;
$secureAttribute = false;
$httpOnlyAttribute = true;
if ($unparsedAttributes !== '') {
foreach (explode(';', $unparsedAttributes) as $cookieAttributeValueString) {
$attributeNameAndValue = explode('=', $cookieAttributeValueString, 2);
$attributeName = trim($attributeNameAndValue[0]);
$attributeValue = isset($attributeNameAndValue[1]) ? trim($attributeNameAndValue[1]) : '';
switch (strtoupper($attributeName)) {
case 'EXPIRES':
try {
$expiresAttribute = new \DateTime($attributeValue);
} catch (\Exception $exception) {
// as of RFC 6265 Section 5.2.1, a non parsable Expires date should result into
// ignoring, but since the Cookie constructor relies on it, we'll
// assume a Session cookie with an expiry date of 0.
$expiresAttribute = 0;
}
break;
case 'MAX-AGE':
if (preg_match(self::PATTERN_MAX_AGE, $attributeValue) === 1) {
$maxAgeAttribute = intval($attributeValue);
}
break;
case 'DOMAIN':
if ($attributeValue !== '') {
$domainAttribute = strtolower(ltrim($attributeValue, '.'));
}
break;
case 'PATH':
if ($attributeValue === '' || substr($attributeValue, 0, 1) !== '/') {
$pathAttribute = '/';
} else {
$pathAttribute = $attributeValue;
}
break;
case 'SECURE':
$secureAttribute = true;
break;
case 'HTTPONLY':
$httpOnlyAttribute = true;
break;
}
}
}
$cookie = new static($cookieName, $cookieValue, $expiresAttribute, $maxAgeAttribute, $domainAttribute, $pathAttribute, $secureAttribute, $httpOnlyAttribute);
return $cookie;
}