/** * Factory create CookieHeader from string representation. * * @param string $headerLine * * @return $this */ public static function fromString($headerLine) { list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine); GenericHeader::assertHeaderFieldName('Cookie', $fieldName); $cookies = array(); foreach (explode(';', $fieldValue) as $item) { $parts = explode('=', $item, 2); if (2 !== count($parts)) { throw new InvalidHeaderValueException(sprintf('Malformed Cookie header found on "%s".', $item)); } list($key, $value) = $parts; $cookies[$key] = urldecode($value); } return new static($cookies); }
/** * Factory create ContentTypeHeader from string representation. * * @param string $headerLine * * @return $this */ public static function fromString($headerLine) { $headerLine = (string) $headerLine; $pos = strpos($headerLine, ':'); $fieldName = substr($headerLine, 0, $pos); $fieldValue = null; $parameters = array(); $items = array(); GenericHeader::assertHeaderFieldName('Content-Type', $fieldName); preg_match_all('#((("[^"\']++")|(\'[^"\']++\'))|[^;"\'])+#', trim(substr($headerLine, $pos + 1)), $items); foreach ($items[0] as $index => $item) { if (0 === $index) { $fieldValue = $item; continue; } $pos = strpos($item, '='); if (false === $pos) { $parameters[$item] = null; continue; } $parameters[substr($item, 0, $pos)] = substr($item, $pos + 1); } return new static($fieldValue, $parameters); }
/** * Factory create AllowHeader from string representation. * * @param string $headerLine * * @return $this */ public static function fromString($headerLine) { list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine); GenericHeader::assertHeaderFieldName('Allow', $fieldName); if ($fieldValue !== null) { $fieldValue = array_filter(explode(',', $fieldValue), 'trim'); $fieldValue = array_map('trim', $fieldValue); } return new static($fieldValue); }
/** * Factory create header. * * @param string $fieldName * @param string $fieldValue * * @return $this */ public static function create($fieldName, $fieldValue) { GenericHeader::assertHeaderFieldName(static::getHeaderName(), $fieldName); return new static($fieldValue); }
/** * Factory create ContentLengthHeader from string representation. * * @param string $headerLine * * @return $this */ public static function fromString($headerLine) { list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine); GenericHeader::assertHeaderFieldName('Content-Length', $fieldName); return new static($fieldValue); }
/** * Factory create AgeHeader from string. * * @param string $headerLine * * @return $this */ public static function fromString($headerLine) { list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine); GenericHeader::assertHeaderFieldName('Age', $fieldName); return new static($fieldValue !== null ? $fieldValue : null); }
/** * Factory create SetCookieHeader from string representation. * * @param string $headerLine * * @return $this */ public static function fromString($headerLine) { $pos = strpos($headerLine, ':'); $fieldName = trim(substr($headerLine, 0, $pos)); $fieldValue = trim(substr($headerLine, $pos + 1)); GenericHeader::assertHeaderFieldName('Set-Cookie', $fieldName); return self::create($fieldValue); }