CookieCollection
in package
implements
IteratorAggregate, Countable
Cookie Collection
Provides an immutable collection of cookies objects. Adding or removing to a collection returns a new collection that you must retain.
Table of Contents
Interfaces
- IteratorAggregate
- Countable
Properties
- $cookies : array<string|int, CookieInterface>
- Cookie objects
Methods
- __construct() : mixed
- Constructor
- add() : static
- Add a cookie and get an updated collection.
- addFromResponse() : static
- Create a new collection that includes cookies from the response.
- addToRequest() : RequestInterface
- Add cookies that match the path/domain/expiration to the request.
- count() : int
- Get the number of cookies in the collection.
- createFromHeader() : static
- Create a Cookie Collection from an array of Set-Cookie Headers
- createFromServerRequest() : static
- Create a new collection from the cookies in a ServerRequest
- get() : CookieInterface
- Get the first cookie by name.
- getIterator() : Traversable<string, CookieInterface>
- Gets the iterator
- has() : bool
- Check if a cookie with the given name exists
- remove() : static
- Create a new collection with all cookies matching $name removed.
- checkCookies() : void
- Checks if only valid cookie objects are in the array
- findMatchingCookies() : array<string, mixed>
- Find cookies matching the scheme, host, and path
- removeExpiredCookies() : void
- Remove expired cookies from the collection.
Properties
$cookies
Cookie objects
protected
array<string|int, CookieInterface>
$cookies
= []
Methods
__construct()
Constructor
public
__construct([array<string|int, CookieInterface> $cookies = [] ]) : mixed
Parameters
- $cookies : array<string|int, CookieInterface> = []
-
Array of cookie objects
add()
Add a cookie and get an updated collection.
public
add(CookieInterface $cookie) : static
Cookies are stored by id. This means that there can be duplicate cookies if a cookie collection is used for cookies across multiple domains. This can impact how get(), has() and remove() behave.
Parameters
- $cookie : CookieInterface
-
Cookie instance to add.
Return values
staticaddFromResponse()
Create a new collection that includes cookies from the response.
public
addFromResponse(ResponseInterface $response, RequestInterface $request) : static
Parameters
- $response : ResponseInterface
-
Response to extract cookies from.
- $request : RequestInterface
-
Request to get cookie context from.
Return values
staticaddToRequest()
Add cookies that match the path/domain/expiration to the request.
public
addToRequest(RequestInterface $request[, array<string|int, mixed> $extraCookies = [] ]) : RequestInterface
This allows CookieCollections to be used as a 'cookie jar' in an HTTP client situation. Cookies that match the request's domain + path that are not expired when this method is called will be applied to the request.
Parameters
- $request : RequestInterface
-
The request to update.
- $extraCookies : array<string|int, mixed> = []
-
Associative array of additional cookies to add into the request. This is useful when you have cookie data from outside the collection you want to send.
Return values
RequestInterface —An updated request.
count()
Get the number of cookies in the collection.
public
count() : int
Return values
intcreateFromHeader()
Create a Cookie Collection from an array of Set-Cookie Headers
public
static createFromHeader(array<string|int, string> $header[, array<string, mixed> $defaults = [] ]) : static
Parameters
- $header : array<string|int, string>
-
The array of set-cookie header values.
- $defaults : array<string, mixed> = []
-
The defaults attributes.
Return values
staticcreateFromServerRequest()
Create a new collection from the cookies in a ServerRequest
public
static createFromServerRequest(ServerRequestInterface $request) : static
Parameters
- $request : ServerRequestInterface
-
The request to extract cookie data from
Return values
staticget()
Get the first cookie by name.
public
get(string $name) : CookieInterface
Parameters
- $name : string
-
The name of the cookie.
Tags
Return values
CookieInterfacegetIterator()
Gets the iterator
public
getIterator() : Traversable<string, CookieInterface>
Return values
Traversable<string, CookieInterface>has()
Check if a cookie with the given name exists
public
has(string $name) : bool
Parameters
- $name : string
-
The cookie name to check.
Return values
bool —True if the cookie exists, otherwise false.
remove()
Create a new collection with all cookies matching $name removed.
public
remove(string $name) : static
If the cookie is not in the collection, this method will do nothing.
Parameters
- $name : string
-
The name of the cookie to remove.
Return values
staticcheckCookies()
Checks if only valid cookie objects are in the array
protected
checkCookies(array<string|int, CookieInterface> $cookies) : void
Parameters
- $cookies : array<string|int, CookieInterface>
-
Array of cookie objects
Tags
findMatchingCookies()
Find cookies matching the scheme, host, and path
protected
findMatchingCookies(string $scheme, string $host, string $path) : array<string, mixed>
Parameters
- $scheme : string
-
The http scheme to match
- $host : string
-
The host to match.
- $path : string
-
The path to match
Return values
array<string, mixed> —An array of cookie name/value pairs
removeExpiredCookies()
Remove expired cookies from the collection.
protected
removeExpiredCookies(string $host, string $path) : void
Parameters
- $host : string
-
The host to check for expired cookies on.
- $path : string
-
The path to check for expired cookies on.