public static function fromHeader(string $string)
    { /* : ?self */
        $parts = \array_map("trim", \explode(";", $string));
        $nameValue = \explode("=", \array_shift($parts), 2);

        if (\count($nameValue) !== 2) {
            return null;
        }

        list($name, $value) = $nameValue;

        $name = \trim($name);
        $value = \trim($value, " \t\n\r\0\x0B\"");

        if ($name === "") {
            return null;
        }

        // httpOnly must default to false for parsing
        $meta = CookieAttributes::empty();

        foreach ($parts as $part) {
            $pieces = \array_map('trim', \explode('=', $part, 2));
            $key = \strtolower($pieces[0]);

            if (1 === \count($pieces)) {
                switch ($key) {
                    case 'secure':
                        $meta = $meta->withSecure();
                        break;

                    case 'httponly':
                        $meta = $meta->withHttpOnly();
                        break;
                }
            } else {
                switch ($key) {
                    case 'expires':
                        $time = \DateTime::createFromFormat('D, j M Y G:i:s T', $pieces[1]);

                        if ($time === false) {
                            break; // break is correct, see https://tools.ietf.org/html/rfc6265#section-5.2.1
                        }

                        $meta = $meta->withExpiry($time);
                        break;

                    case 'max-age':
                        $maxAge = \trim($pieces[1]);

                        // This also allows +1.42, but avoids a more complicated manual check
                        if (!\is_numeric($maxAge)) {
                            break; // break is correct, see https://tools.ietf.org/html/rfc6265#section-5.2.2
                        }

                        $meta = $meta->withMaxAge($maxAge);
                        break;

                    case 'path':
                        $meta = $meta->withPath($pieces[1]);
                        break;

                    case 'domain':
                        $meta = $meta->withDomain($pieces[1]);
                        break;
                }
            }
        }

        try {
            return new self($name, $value, $meta);
        } catch (InvalidCookieException $e) {
            return null;
        }
    }
public static function fromHeader(string $string)
    { /* : ?self */
        $parts = \array_map("trim", \explode(";", $string));
        $nameValue = \explode("=", \array_shift($parts), 2);

        if (\count($nameValue) !== 2) {
            return null;
        }

        list($name, $value) = $nameValue;

        $name = \trim($name);
        $value = \trim($value, " \t\n\r\0\x0B\"");

        if ($name === "") {
            return null;
        }

        // httpOnly must default to false for parsing
        $meta = CookieAttributes::empty();

        foreach ($parts as $part) {
            $pieces = \array_map('trim', \explode('=', $part, 2));
            $key = \strtolower($pieces[0]);

            if (1 === \count($pieces)) {
                switch ($key) {
                    case 'secure':
                        $meta = $meta->withSecure();
                        break;

                    case 'httponly':
                        $meta = $meta->withHttpOnly();
                        break;
                }
            } else {
                switch ($key) {
                    case 'expires':
                        $time = \DateTime::createFromFormat('D, j M Y G:i:s T', $pieces[1]);

                        if ($time === false) {
                            break; // break is correct, see https://tools.ietf.org/html/rfc6265#section-5.2.1
                        }

                        $meta = $meta->withExpiry($time);
                        break;

                    case 'max-age':
                        $maxAge = \trim($pieces[1]);

                        // This also allows +1.42, but avoids a more complicated manual check
                        if (!\is_numeric($maxAge)) {
                            break; // break is correct, see https://tools.ietf.org/html/rfc6265#section-5.2.2
                        }

                        $meta = $meta->withMaxAge($maxAge);
                        break;

                    case 'path':
                        $meta = $meta->withPath($pieces[1]);
                        break;

                    case 'domain':
                        $meta = $meta->withDomain($pieces[1]);
                        break;
                }
            }
        }

        try {
            return new self($name, $value, $meta);
        } catch (InvalidCookieException $e) {
            return null;
        }
    }
public static function fromHeader ( string $ string ) { /* : ?self */ $ parts = \ array_map ( "trim" , \ explode ( ";" , $ string ) ) ; $ nameValue = \ explode ( "=" , \ array_shift ( $ parts ) , 2 ) ; if ( \ count ( $ nameValue ) !== 2 ) { return null ; } list ( $ name , $ value ) = $ nameValue ; $ name = \ trim ( $ name ) ; $ value = \ trim ( $ value , " \t\n\r\0\x0B\"" ) ; if ( $ name === "" ) { return null ; } // httpOnly must default to false for parsing $ meta = CookieAttributes :: empty ( ) ; foreach ( $ parts as $ part ) { $ pieces = \ array_map ( 'trim' , \ explode ( '=' , $ part , 2 ) ) ; $ key = \ strtolower ( $ pieces [ 0 ] ) ; if ( 1 === \ count ( $ pieces ) ) { switch ( $ key ) { case 'secure' : $ meta = $ meta -> withSecure ( ) ; break ; case 'httponly' : $ meta = $ meta -> withHttpOnly ( ) ; break ; } } else { switch ( $ key ) { case 'expires' : $ time = \ DateTime :: createFromFormat ( 'D, j M Y G:i:s T' , $ pieces [ 1 ] ) ; if ( $ time === false ) { break ; // break is correct, see https://tools.ietf.org/html/rfc6265#section-5.2.1 } $ meta = $ meta -> withExpiry ( $ time ) ; break ; case 'max-age' : $ maxAge = \ trim ( $ pieces [ 1 ] ) ; // This also allows +1.42, but avoids a more complicated manual check if ( ! \ is_numeric ( $ maxAge ) ) { break ; // break is correct, see https://tools.ietf.org/html/rfc6265#section-5.2.2 } $ meta = $ meta -> withMaxAge ( $ maxAge ) ; break ; case 'path' : $ meta = $ meta -> withPath ( $ pieces [ 1 ] ) ; break ; case 'domain' : $ meta = $ meta -> withDomain ( $ pieces [ 1 ] ) ; break ; } } } try { return new self ( $ name , $ value , $ meta ) ; } catch ( InvalidCookieException $ e ) { return null ; } }
Parses a cookie from a 'set-cookie' header.

@param string $string Valid 'set-cookie' header line.

@return self|null Returns a `ResponseCookie` instance on success and `null` on failure.
Parses a cookie from a set - cookie header .