Class CookieCollection
Cookie Collection
Provides an immutable collection of cookies objects. Adding or removing to a collection returns a new collection that you must retain.
Property Summary
-
$cookies protected
array<string, \Cake\Http\Cookie\CookieInterface>Cookie objects
-
$hostOnlyDomains protected
array<string, string>Domains of cookies received without an explicit Domain attribute, indexed by cookie ID.
Method Summary
-
__construct() public
Constructor
-
__get() public
Get the first cookie by name if cookie with provided name exists
-
__isset() public
Check if a cookie with the given name exists
-
add() public
Add a cookie and get an updated collection.
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.
-
addFromResponse() public
Create a new collection that includes cookies from the response.
-
addToRequest() public
Add cookies that match the path/domain/expiration to the request.
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.
-
checkCookies() protected
Checks if only valid cookie objects are in the array
-
count() public
Get the number of cookies in the collection.
-
createFromHeader() public static
Create a Cookie Collection from an array of Set-Cookie Headers
Only cookies with matching domains will be retained. Protection for public suffixes is not implemented.
-
createFromServerRequest() public static
Create a new collection from the cookies in a ServerRequest
-
findMatchingCookies() protected
Find cookies matching the scheme, host, and path
-
get() public
Get the first cookie by name.
-
getIterator() public
Gets the iterator
-
has() public
Check if a cookie with the given name exists
-
normalizeDomain() protected static
Use the same ASCII representation for cookie storage and lookup.
-
remove() public
Create a new collection with all cookies matching $name removed.
If the cookie is not in the collection, this method will do nothing.
-
removeExpiredCookies() protected
Remove expired cookies from the collection.
Method Detail
__construct() ¶ public
__construct(array<Cake\Http\Cookie\CookieInterface> $cookies = [])
Constructor
Parameters
-
array<Cake\Http\Cookie\CookieInterface>$cookies optional Array of cookie objects
__get() ¶ public
__get(string $name): Cake\Http\Cookie\CookieInterface|null
Get the first cookie by name if cookie with provided name exists
Parameters
-
string$name The name of the cookie.
Returns
Cake\Http\Cookie\CookieInterface|null__isset() ¶ public
__isset(string $name): bool
Check if a cookie with the given name exists
Parameters
-
string$name The cookie name to check.
Returns
boolTrue if the cookie exists, otherwise false.
add() ¶ public
add(Cake\Http\Cookie\CookieInterface $cookie): static
Add a cookie and get an updated collection.
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
-
Cake\Http\Cookie\CookieInterface$cookie Cookie instance to add.
Returns
staticaddFromResponse() ¶ public
addFromResponse(Psr\Http\Message\ResponseInterface $response, Psr\Http\Message\RequestInterface $request): static
Create a new collection that includes cookies from the response.
Parameters
-
Psr\Http\Message\ResponseInterface$response Response to extract cookies from.
-
Psr\Http\Message\RequestInterface$request Request to get cookie context from.
Returns
staticaddToRequest() ¶ public
addToRequest(Psr\Http\Message\RequestInterface $request, array $extraCookies = []): Psr\Http\Message\RequestInterface
Add cookies that match the path/domain/expiration to the request.
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
-
Psr\Http\Message\RequestInterface$request The request to update.
-
array$extraCookies optional 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.
Returns
Psr\Http\Message\RequestInterfaceAn updated request.
checkCookies() ¶ protected
checkCookies(array<Cake\Http\Cookie\CookieInterface> $cookies): void
Checks if only valid cookie objects are in the array
Parameters
-
array<Cake\Http\Cookie\CookieInterface>$cookies Array of cookie objects
Returns
voidThrows
InvalidArgumentExceptioncreateFromHeader() ¶ public static
createFromHeader(array<string> $header, array<string, mixed> $defaults = []): static
Create a Cookie Collection from an array of Set-Cookie Headers
Only cookies with matching domains will be retained. Protection for public suffixes is not implemented.
Parameters
-
array<string>$header The array of set-cookie header values.
-
array<string, mixed>$defaults optional The defaults attributes.
Returns
staticcreateFromServerRequest() ¶ public static
createFromServerRequest(Psr\Http\Message\ServerRequestInterface $request): static
Create a new collection from the cookies in a ServerRequest
Parameters
-
Psr\Http\Message\ServerRequestInterface$request The request to extract cookie data from
Returns
staticfindMatchingCookies() ¶ protected
findMatchingCookies(string $scheme, string $host, string $path): array<string, mixed>
Find cookies matching the scheme, host, and path
Parameters
-
string$scheme The http scheme to match
-
string$host The host to match.
-
string$path The path to match
Returns
array<string, mixed>An array of cookie name/value pairs
get() ¶ public
get(string $name): Cake\Http\Cookie\CookieInterface
Get the first cookie by name.
Parameters
-
string$name The name of the cookie.
Returns
Cake\Http\Cookie\CookieInterfaceThrows
InvalidArgumentExceptionIf cookie not found.
getIterator() ¶ public
getIterator(): Traversable<string, \Cake\Http\Cookie\CookieInterface>
Gets the iterator
Returns
Traversable<string, \Cake\Http\Cookie\CookieInterface>has() ¶ public
has(string $name): bool
Check if a cookie with the given name exists
Parameters
-
string$name The cookie name to check.
Returns
boolTrue if the cookie exists, otherwise false.
normalizeDomain() ¶ protected static
normalizeDomain(string $domain): string
Use the same ASCII representation for cookie storage and lookup.
Parameters
-
string$domain The URI host or cookie domain.
Returns
stringremove() ¶ public
remove(string $name): static
Create a new collection with all cookies matching $name removed.
If the cookie is not in the collection, this method will do nothing.
Parameters
-
string$name The name of the cookie to remove.
Returns
staticremoveExpiredCookies() ¶ protected
removeExpiredCookies(string $host, string $path): void
Remove expired cookies from the collection.
Parameters
-
string$host The host to check for expired cookies on.
-
string$path The path to check for expired cookies on.
Returns
void