0 follower

Final Class Yiisoft\Cookies\CookieCollection

InheritanceYiisoft\Cookies\CookieCollection
ImplementsArrayAccess, Countable, IteratorAggregate

A CookieCollection helps to work with many cookies at once and to read / modify response cookies.

See also Yiisoft\Cookies\Cookie.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() CookieCollection constructor. Yiisoft\Cookies\CookieCollection
add() Adds a cookie to the collection. Yiisoft\Cookies\CookieCollection
addToResponse() Adds the cookies in the collection to response and returns it. Yiisoft\Cookies\CookieCollection
clear() Removes all cookies. Yiisoft\Cookies\CookieCollection
contains() Returns whether the collection already contains the cookie. Yiisoft\Cookies\CookieCollection
count() Returns the number of cookies in the collection. Yiisoft\Cookies\CookieCollection
exists() Tests for the existence of the cookie that satisfies the given predicate. Yiisoft\Cookies\CookieCollection
expire() Expire the cookie with the specified name. Yiisoft\Cookies\CookieCollection
fromArray() Populates the cookie collection from an array of 'name' => 'value' pairs. Yiisoft\Cookies\CookieCollection
fromResponse() Populates the cookie collection from a ResponseInterface. Yiisoft\Cookies\CookieCollection
get() Returns the cookie with the specified name. Yiisoft\Cookies\CookieCollection
getIterator() Returns an iterator for traversing the cookies in the collection. Yiisoft\Cookies\CookieCollection
getKeys() Gets all keys/indices of the collection. Yiisoft\Cookies\CookieCollection
getValue() Returns the value of the named cookie. Yiisoft\Cookies\CookieCollection
getValues() Gets all cookies of the collection as an indexed array. Yiisoft\Cookies\CookieCollection
has() Returns whether there is a cookie with the specified name. Yiisoft\Cookies\CookieCollection
isEmpty() Checks whether the collection is empty (contains no cookies). Yiisoft\Cookies\CookieCollection
offsetExists() Returns whether there is a cookie with the specified name. Yiisoft\Cookies\CookieCollection
offsetGet() Returns the cookie with the specified name. Yiisoft\Cookies\CookieCollection
offsetSet() Adds the cookie to the collection. Yiisoft\Cookies\CookieCollection
offsetUnset() Removes the named cookie. Yiisoft\Cookies\CookieCollection
remove() Removes a cookie. Yiisoft\Cookies\CookieCollection
setToResponse() Creates a copy of the response with cookies set from the collection. Yiisoft\Cookies\CookieCollection
toArray() Returns the collection as a PHP array. Yiisoft\Cookies\CookieCollection
walk() Apply user supplied function to every cookie in the collection. Yiisoft\Cookies\CookieCollection

Method Details

Hide inherited methods

__construct() public method

CookieCollection constructor.

public mixed __construct ( array|Yiisoft\Cookies\Cookie[] $cookies = [] )
$cookies array|Yiisoft\Cookies\Cookie[]

The cookies that this collection initially contains.

                public function __construct(array $cookies = [])
{
    foreach ($cookies as $cookie) {
        if (!($cookie instanceof Cookie)) {
            throw new InvalidArgumentException('CookieCollection can contain only Cookie instances.');
        }
        $this->cookies[$cookie->getName()] = $cookie;
    }
}

            
add() public method

Adds a cookie to the collection.

If there is already a cookie with the same name in the collection, it will be removed first.

public void add ( Yiisoft\Cookies\Cookie $cookie )
$cookie Yiisoft\Cookies\Cookie

The cookie to be added.

                public function add(Cookie $cookie): void
{
    $this->cookies[$cookie->getName()] = $cookie;
}

            
addToResponse() public method

Adds the cookies in the collection to response and returns it.

public \Psr\Http\Message\ResponseInterface addToResponse ( \Psr\Http\Message\ResponseInterface $response )
$response \Psr\Http\Message\ResponseInterface

Response to add cookies to.

return \Psr\Http\Message\ResponseInterface

Response with added cookies.

                public function addToResponse(ResponseInterface $response): ResponseInterface
{
    foreach ($this->cookies as $cookie) {
        $response = $cookie->addToResponse($response);
    }
    return $response;
}

            
clear() public method

Removes all cookies.

public void clear ( )

                public function clear(): void
{
    $this->cookies = [];
}

            
contains() public method

Returns whether the collection already contains the cookie.

See also has().

public boolean contains ( Yiisoft\Cookies\Cookie $cookie )
$cookie Yiisoft\Cookies\Cookie

The cookie to check for.

return boolean

Whether cookie exists.

                public function contains(Cookie $cookie): bool
{
    return in_array($cookie, $this->cookies, true);
}

            
count() public method

Returns the number of cookies in the collection.

This method is required by the SPL {@see \Countable} interface. It will be implicitly called when you use count($collection).

public integer count ( )
return integer

The number of cookies in the collection.

                public function count(): int
{
    return count($this->cookies);
}

            
exists() public method

Tests for the existence of the cookie that satisfies the given predicate.

public boolean exists ( callable $p )
$p callable

The predicate.

return boolean

Whether the predicate is true for at least on cookie.

                public function exists(callable $p): bool
{
    foreach ($this->cookies as $name => $cookie) {
        if ($p($cookie, $name)) {
            return true;
        }
    }
    return false;
}

            
expire() public method

Expire the cookie with the specified name.

public void expire ( string $name )
$name string

The cookie name.

                public function expire(string $name): void
{
    if (!isset($this->cookies[$name])) {
        return;
    }
    $this->cookies[$name] = $this->cookies[$name]->expire();
}

            
fromArray() public static method

Populates the cookie collection from an array of 'name' => 'value' pairs.

public static Yiisoft\Cookies\CookieCollection fromArray ( array $array )
$array array

The cookies 'name' => 'value' array to populate from.

return Yiisoft\Cookies\CookieCollection

Collection created from array.

                public static function fromArray(array $array): self
{
    if (empty($array)) {
        return new self();
    }
    // Check if associative array with 'name' => 'value' pairs is passed.
    if (count(array_filter(array_keys($array), 'is_string')) !== count($array)) {
        throw new InvalidArgumentException('Invalid array format. It must be "name" => "value" pairs.');
    }
    /** @psalm-var array<string,string> $array */
    return new self(array_map(
        static fn (string $name, string $value) => new Cookie($name, $value),
        array_keys($array),
        $array
    ));
}

            
fromResponse() public static method

Populates the cookie collection from a ResponseInterface.

public static Yiisoft\Cookies\CookieCollection fromResponse ( \Psr\Http\Message\ResponseInterface $response )
$response \Psr\Http\Message\ResponseInterface

The response object to populate from.

return Yiisoft\Cookies\CookieCollection

Collection created from response.

throws Exception

                public static function fromResponse(ResponseInterface $response): self
{
    $collection = new self();
    foreach ($response->getHeader(Header::SET_COOKIE) as $setCookieString) {
        $cookie = Cookie::fromCookieString($setCookieString);
        $collection->add($cookie);
    }
    return $collection;
}

            
get() public method

Returns the cookie with the specified name.

See also getValue().

public Yiisoft\Cookies\Cookie|null get ( string $name )
$name string

The cookie name.

return Yiisoft\Cookies\Cookie|null

The cookie with the specified name. Null if the named cookie does not exist.

                public function get(string $name): ?Cookie
{
    return $this->cookies[$name] ?? null;
}

            
getIterator() public method

Returns an iterator for traversing the cookies in the collection.

This method is required by the SPL interface {@see \IteratorAggregate}. It will be implicitly called when you use foreach to traverse the collection.

public ArrayIterator getIterator ( )

                public function getIterator(): ArrayIterator
{
    return new ArrayIterator($this->cookies);
}

            
getKeys() public method

Gets all keys/indices of the collection.

public string[] getKeys ( )
return string[]

The keys/indices of the collection.

                public function getKeys(): array
{
    return array_keys($this->cookies);
}

            
getValue() public method

Returns the value of the named cookie.

See also get().

public string|null getValue ( string $name, string|null $defaultValue null )
$name string

The cookie name.

$defaultValue string|null

The value that should be returned when the named cookie does not exist.

return string|null

The value of the named cookie or the default value if cookie is not set.

                public function getValue(string $name, ?string $defaultValue = null): ?string
{
    return isset($this->cookies[$name]) ? $this->cookies[$name]->getValue() : $defaultValue;
}

            
getValues() public method

Gets all cookies of the collection as an indexed array.

public Yiisoft\Cookies\Cookie[] getValues ( )
return Yiisoft\Cookies\Cookie[]

The cookies in the collection, in the order they appear in the collection.

                public function getValues(): array
{
    return array_values($this->cookies);
}

            
has() public method

Returns whether there is a cookie with the specified name.

See also remove().

public boolean has ( string $name )
$name string

The cookie name.

return boolean

Whether the named cookie exists.

                public function has(string $name): bool
{
    return isset($this->cookies[$name]);
}

            
isEmpty() public method

Checks whether the collection is empty (contains no cookies).

public boolean isEmpty ( )
return boolean

Whether the collection is empty.

                public function isEmpty(): bool
{
    return empty($this->cookies);
}

            
offsetExists() public method

Returns whether there is a cookie with the specified name.

This method is required by the SPL interface {@see \ArrayAccess}. It is implicitly called when you use something like isset($collection[$name]). This is equivalent to {@see \Yiisoft\Cookies\has()}.

public boolean offsetExists ( string $name )
$name string

The cookie name.

return boolean

Whether the named cookie exists.

                public function offsetExists($name): bool
{
    return $this->has($name);
}

            
offsetGet() public method

Returns the cookie with the specified name.

This method is required by the SPL interface {@see \ArrayAccess}. It is implicitly called when you use something like $cookie = $collection[$name];. This is equivalent to {@see \Yiisoft\Cookies\get()}.

public Yiisoft\Cookies\Cookie|null offsetGet ( string $name )
$name string

The cookie name.

return Yiisoft\Cookies\Cookie|null

The cookie with the specified name, null if the named cookie does not exist.

                public function offsetGet($name): ?Cookie
{
    return $this->get($name);
}

            
offsetSet() public method

Adds the cookie to the collection.

This method is required by the SPL interface {@see \ArrayAccess}. It is implicitly called when you use something like $collection[$name] = $cookie;. This is equivalent to {@see \Yiisoft\Cookies\add()}.

public void offsetSet ( string $name, Yiisoft\Cookies\Cookie $cookie )
$name string

The cookie name.

$cookie Yiisoft\Cookies\Cookie

The cookie to be added.

                public function offsetSet($name, $cookie): void
{
    $this->add($cookie);
}

            
offsetUnset() public method

Removes the named cookie.

This method is required by the SPL interface {@see \ArrayAccess}. It is implicitly called when you use something like unset($collection[$name]). This is equivalent to {@see \Yiisoft\Cookies\remove()}.

public void offsetUnset ( string $name )
$name string

The cookie name.

                public function offsetUnset($name): void
{
    $this->remove($name);
}

            
remove() public method

Removes a cookie.

public Yiisoft\Cookies\Cookie|null remove ( string $name )
$name string

The name of the cookie to be removed.

return Yiisoft\Cookies\Cookie|null

Cookie that was removed.

                public function remove(string $name): ?Cookie
{
    if (!isset($this->cookies[$name])) {
        return null;
    }
    $removed = $this->cookies[$name];
    unset($this->cookies[$name]);
    return $removed;
}

            
setToResponse() public method

Creates a copy of the response with cookies set from the collection.

public \Psr\Http\Message\ResponseInterface setToResponse ( \Psr\Http\Message\ResponseInterface $response )
$response \Psr\Http\Message\ResponseInterface

Response to set cookies to.

return \Psr\Http\Message\ResponseInterface

Response with new cookies.

                public function setToResponse(ResponseInterface $response): ResponseInterface
{
    $response = $response->withoutHeader(Header::SET_COOKIE);
    return $this->addToResponse($response);
}

            
toArray() public method

Returns the collection as a PHP array.

The array keys are cookie names, and the array values are the corresponding cookie objects.

public Yiisoft\Cookies\Cookie[] toArray ( )

                public function toArray(): array
{
    return $this->cookies;
}

            
walk() public method

Apply user supplied function to every cookie in the collection.

Function signature is

function (Cookie $cookie, string $key): void

If you want to modify the cookie in the collection, specify the first parameter of the callback as reference.

public void walk ( callable $callback )
$callback callable

                public function walk(callable $callback): void
{
    /** @psalm-suppress MixedPropertyTypeCoercion */
    array_walk($this->cookies, $callback);
}