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 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;
}
}
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;
}
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;
}
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);
}
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);
}
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 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();
}
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
));
}
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;
}
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;
}
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);
}
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);
}
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;
}
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);
}
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]);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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;
}
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);
}
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;
}
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);
}
Signup or Login in order to comment.