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.

Cake\Http\Cookie\CookieCollection implements IteratorAggregate, Countable

Direct Subclasses

Properties summary

Method Summary

  • __construct() public
    Constructor
  • add() public
    Add a cookie and get an updated collection.
  • Create a new collection that includes cookies from the response.
  • Add cookies that match the path/domain/expiration 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
  • Create a new collection from the cookies in a ServerRequest
  • 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
  • parseSetCookieHeader() protected static
    Parse Set-Cookie headers into array
  • remove() public
    Create a new collection with all cookies matching $name removed.
  • Remove expired cookies from the collection.
  • Apply path and host to the set of cookies if they are not set.

Method Detail

__construct()source public

__construct( array $cookies [] )

Constructor

Parameters

array $cookies optional []
Array of cookie objects

add()source public

add( Cake\Http\Cookie\CookieInterface $cookie )

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

Cake\Http\Cookie\CookieCollection

addFromResponse()source public

addFromResponse( Psr\Http\Message\ResponseInterface $response , Psr\Http\Message\RequestInterface $request )

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

Cake\Http\Cookie\CookieCollection

addToRequest()source public

addToRequest( Psr\Http\Message\RequestInterface $request , array $extraCookies [] )

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\RequestInterface
An updated request.

checkCookies()source protected

checkCookies( array $cookies )

Checks if only valid cookie objects are in the array

Parameters

array $cookies
Array of cookie objects

Throws

InvalidArgumentException

count()source public

count( )

Get the number of cookies in the collection.

Returns

integer

Implementation of

Countable::count()

createFromHeader()source public static

createFromHeader( array $header )

Create a Cookie Collection from an array of Set-Cookie Headers

Parameters

array $header
The array of set-cookie header values.

Returns

Cake\Http\Cookie\CookieCollection

createFromServerRequest()source public static

createFromServerRequest( Psr\Http\Message\ServerRequestInterface $request )

Create a new collection from the cookies in a ServerRequest

Parameters

Psr\Http\Message\ServerRequestInterface $request
The request to extract cookie data from

Returns

Cake\Http\Cookie\CookieCollection

findMatchingCookies()source protected

findMatchingCookies( string $scheme , string $host , string $path )

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
An array of cookie name/value pairs

get()source public

get( string $name )

Get the first cookie by name.

Parameters

string $name
The name of the cookie.

Returns

Cake\Http\Cookie\CookieInterface|null

getIterator()source public

getIterator( )

Gets the iterator

Returns

ArrayIterator

Implementation of

IteratorAggregate::getIterator()

has()source public

has( string $name )

Check if a cookie with the given name exists

Parameters

string $name
The cookie name to check.

Returns

boolean
True if the cookie exists, otherwise false.

parseSetCookieHeader()source protected static

parseSetCookieHeader( array $values )

Parse Set-Cookie headers into array

Parameters

array $values
List of Set-Cookie Header values.

Returns

Cake\Http\Cookie\Cookie[]
An array of cookie objects

remove()source public

remove( string $name )

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

Cake\Http\Cookie\CookieCollection

removeExpiredCookies()source protected

removeExpiredCookies( string $host , string $path )

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.

setRequestDefaults()source protected

setRequestDefaults( array $cookies , string $host , string $path )

Apply path and host to the set of cookies if they are not set.

Parameters

array $cookies
An array of cookies to update.
string $host
The host to set.
string $path
The path to set.

Returns

array
An array of updated cookies.

Properties detail

$cookiessource

protected Cake\Http\Cookie\CookieInterface[]

Cookie objects

[]

© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Http.Cookie.CookieCollection.html