Final Class Yiisoft\Cookies\CookieCollection
| Inheritance | Yiisoft\Cookies\CookieCollection |
|---|---|
| Implements | ArrayAccess, 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
| 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
CookieCollection constructor.
| public __construct( array|Yiisoft\Cookies\Cookie[] $cookies = [] ): mixed | ||
| $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;
}
}
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 add( Yiisoft\Cookies\Cookie $cookie ): void | ||
| $cookie | Yiisoft\Cookies\Cookie |
The cookie to be added. |
public function add(Cookie $cookie): void
{
$this->cookies[$cookie->getName()] = $cookie;
}
Adds the cookies in the collection to response and returns it.
| public addToResponse( \Psr\Http\Message\ResponseInterface $response ): \Psr\Http\Message\ResponseInterface | ||
| $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;
}
Returns whether the collection already contains the cookie.
See also has().
| public contains( Yiisoft\Cookies\Cookie $cookie ): boolean | ||
| $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);
}
Returns the number of cookies in the collection.
This method is required by the SPL Countable interface.
It will be implicitly called when you use count($collection).
| public count( ): integer | ||
| return | integer |
The number of cookies in the collection. |
|---|---|---|
public function count(): int
{
return count($this->cookies);
}
Tests for the existence of the cookie that satisfies the given predicate.
| public exists( callable $p ): boolean | ||
| $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 the cookie with the specified name.
| public expire( string $name ): void | ||
| $name | string |
The cookie name. |
public function expire(string $name): void
{
if (!isset($this->cookies[$name])) {
return;
}
$this->cookies[$name] = $this->cookies[$name]->expire();
}
Populates the cookie collection from an array of 'name' => 'value' pairs.
| public static fromArray( array $array ): Yiisoft\Cookies\CookieCollection | ||
| $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,
));
}
Populates the cookie collection from a ResponseInterface.
| public static fromResponse( \Psr\Http\Message\ResponseInterface $response ): Yiisoft\Cookies\CookieCollection | ||
| $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;
}
Returns the cookie with the specified name.
See also getValue().
| public get( string $name ): Yiisoft\Cookies\Cookie|null | ||
| $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;
}
Returns an iterator for traversing the cookies in the collection.
This method is required by the SPL interface IteratorAggregate.
It will be implicitly called when you use foreach to traverse the collection.
| public getIterator( ): ArrayIterator |
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->cookies);
}
Gets all keys/indices of the collection.
| public getKeys( ): string[] | ||
| return | string[] |
The keys/indices of the collection. |
|---|---|---|
public function getKeys(): array
{
return array_keys($this->cookies);
}
Returns the value of the named cookie.
See also get().
| public getValue( string $name, string|null $defaultValue = null ): string|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;
}
Gets all cookies of the collection as an indexed array.
| public getValues( ): Yiisoft\Cookies\Cookie[] | ||
| 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);
}
Returns whether there is a cookie with the specified name.
See also remove().
| public has( string $name ): boolean | ||
| $name | string |
The cookie name. |
| return | boolean |
Whether the named cookie exists. |
|---|---|---|
public function has(string $name): bool
{
return isset($this->cookies[$name]);
}
Checks whether the collection is empty (contains no cookies).
| public isEmpty( ): boolean | ||
| return | boolean |
Whether the collection is empty. |
|---|---|---|
public function isEmpty(): bool
{
return empty($this->cookies);
}
Returns whether there is a cookie with the specified name.
This method is required by the SPL interface ArrayAccess.
It is implicitly called when you use something like isset($collection[$name]).
This is equivalent to has().
| public offsetExists( string $name ): boolean | ||
| $name | string |
The cookie name. |
| return | boolean |
Whether the named cookie exists. |
|---|---|---|
public function offsetExists($name): bool
{
return $this->has($name);
}
Returns the cookie with the specified name.
This method is required by the SPL interface ArrayAccess.
It is implicitly called when you use something like $cookie = $collection[$name];.
This is equivalent to get().
| public offsetGet( string $name ): Yiisoft\Cookies\Cookie|null | ||
| $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);
}
Adds the cookie to the collection.
This method is required by the SPL interface ArrayAccess.
It is implicitly called when you use something like $collection[$name] = $cookie;.
This is equivalent to add().
| public offsetSet( string $name, Yiisoft\Cookies\Cookie $cookie ): void | ||
| $name | string |
The cookie name. |
| $cookie | Yiisoft\Cookies\Cookie |
The cookie to be added. |
public function offsetSet($name, $cookie): void
{
$this->add($cookie);
}
Removes the named cookie.
This method is required by the SPL interface ArrayAccess.
It is implicitly called when you use something like unset($collection[$name]).
This is equivalent to remove().
| public offsetUnset( string $name ): void | ||
| $name | string |
The cookie name. |
public function offsetUnset($name): void
{
$this->remove($name);
}
Removes a cookie.
| public remove( string $name ): Yiisoft\Cookies\Cookie|null | ||
| $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;
}
Creates a copy of the response with cookies set from the collection.
| public setToResponse( \Psr\Http\Message\ResponseInterface $response ): \Psr\Http\Message\ResponseInterface | ||
| $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);
}
Returns the collection as a PHP array.
The array keys are cookie names, and the array values are the corresponding cookie objects.
| public toArray( ): Yiisoft\Cookies\Cookie[] |
public function toArray(): array
{
return $this->cookies;
}
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 walk( callable $callback ): void | ||
| $callback | callable | |
public function walk(callable $callback): void
{
/** @psalm-suppress MixedPropertyTypeCoercion */
array_walk($this->cookies, $callback);
}
Signup or Login in order to comment.