Icicle\Http\Message\Cookie\SetCookie::fromHeader PHP Метод

fromHeader() публичный статический Метод

public static fromHeader ( string $string ) : Icicle\Http\Message\Cookie\Cookie
$string string Valid Set-Cookie header line.
Результат Icicle\Http\Message\Cookie\Cookie
    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);
    }

Usage Example

Пример #1
0
 /**
  * Sets cookies based on headers.
  *
  * @throws \Icicle\Http\Exception\InvalidValueException
  */
 private function setCookiesFromHeaders()
 {
     $this->cookies = [];
     $headers = $this->getHeaderAsArray('Set-Cookie');
     foreach ($headers as $line) {
         $cookie = SetCookie::fromHeader($line);
         $this->cookies[$cookie->getName()] = $cookie;
     }
 }