Zend Framework  3.0
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Cookies Class Reference

A Zend object is designed to contain and maintain HTTP cookies, and should be used along with Zend in order to manage cookies across HTTP requests and responses. More...

Public Member Functions

 addCookie ($cookie, $refUri=null)
 Add a cookie to the class.
 
 addCookiesFromResponse (Response $response, $refUri)
 Parse an HTTP response, adding all the cookies set in that response.
 
 getAllCookies ($retAs=self::COOKIE_OBJECT)
 Get all cookies in the cookie jar as an array.
 
 getMatchingCookies ($uri, $matchSessionCookies=true, $retAs=self::COOKIE_OBJECT, $now=null)
 Return an array of all cookies matching a specific request according to the request URI, whether session cookies should be sent or not, and the time to consider as "now" when checking cookie expiry time.
 
 getCookie ($uri, $cookieName, $retAs=self::COOKIE_OBJECT)
 Get a specific cookie according to a URI and name.
 
 isEmpty ()
 Tells if the array of cookies is empty.
 
 reset ()
 Empties the cookieJar of any cookie.
 
- Public Member Functions inherited from Headers
 setPluginClassLoader (PluginClassLocator $pluginClassLoader)
 Set an alternate implementation for the PluginClassLoader.
 
 getPluginClassLoader ()
 Return an instance of a PluginClassLocator, lazyload and inject map if necessary.
 
 addHeaders ($headers)
 Add many headers at once.
 
 addHeaderLine ($headerFieldNameOrLine, $fieldValue=null)
 Add a raw header line, either in name => value, or as a single string 'name: value'.
 
 addHeader (Header\HeaderInterface $header)
 Add a Header to this container, for raw values.
 
 removeHeader (Header\HeaderInterface $header)
 Remove a Header from the container.
 
 clearHeaders ()
 Clear all headers.
 
 get ($name)
 Get all headers of a certain name/type.
 
 has ($name)
 Test for existence of a type of header.
 
 next ()
 Advance the pointer for this object as an iterator.
 
 key ()
 Return the current key for this object as an iterator.
 
 valid ()
 Is this iterator still valid?
 
 rewind ()
 Reset the internal pointer for this object as an iterator.
 
 current ()
 Return the current value for this iterator, lazy loading it if need be.
 
 count ()
 Return the number of headers in this contain, if all headers have not been parsed, actual count could increase if MultipleHeader objects exist in the Request/Response.
 
 toString ()
 Render all headers at once.
 
 toArray ()
 Return the headers container as an array.
 
 forceLoading ()
 By calling this, it will force parsing and loading of all headers, after this count() will be accurate.
 

Static Public Member Functions

static fromString ($string)
 
static fromResponse (Response $response, $refUri)
 Create a new Cookies object and automatically load into it all the cookies set in a Response object.
 
- Static Public Member Functions inherited from Headers
static fromString ($string)
 Populates headers from string representation.
 

Public Attributes

const COOKIE_OBJECT = 0
 Return cookie(s) as a Zend object.
 
const COOKIE_STRING_ARRAY = 1
 Return cookie(s) as a string (suitable for sending in an HTTP request)
 
const COOKIE_STRING_CONCAT = 2
 Return all cookies as one long string (suitable for sending in an HTTP request)
 
const COOKIE_STRING_CONCAT_STRICT = 3
 Return all cookies as one long string (strict mode)
 

Protected Member Functions

 _flattenCookiesArray ($ptr, $retAs=self::COOKIE_OBJECT)
 Helper function to recursively flatten an array.
 
 _matchDomain ($domain)
 Return a subset of the cookies array matching a specific domain.
 
 _matchPath ($domains, $path)
 Return a subset of a domain-matching cookies that also match a specified path.
 
- Protected Member Functions inherited from Headers
 lazyLoadHeader ($index)
 

Protected Attributes

 $cookies = []
 
 $headers = null
 
 $rawCookies
 
- Protected Attributes inherited from Headers
 $pluginClassLoader = null
 
 $headersKeys = []
 
 $headers = []
 

Additional Inherited Members

- Static Protected Member Functions inherited from Headers
static createKey ($name)
 Create array key from header name.
 

Detailed Description

A Zend object is designed to contain and maintain HTTP cookies, and should be used along with Zend in order to manage cookies across HTTP requests and responses.

The class contains an array of Zend objects. Cookies can be added automatically from a request or manually. Then, the Cookies class can find and return the cookies needed for a specific HTTP request.

A special parameter can be passed to all methods of this class that return cookies: Cookies can be returned either in their native form (as Zend objects) or as strings - the later is suitable for sending as the value of the "Cookie" header in an HTTP request. You can also choose, when returning more than one cookie, whether to get an array of strings (by passing Zend::COOKIE_STRING_ARRAY) or one unified string for all cookies (by passing Zend::COOKIE_STRING_CONCAT).

for some specs.

Member Function Documentation

_flattenCookiesArray (   $ptr,
  $retAs = self::COOKIE_OBJECT 
)
protected

Helper function to recursively flatten an array.

Should be used when exporting the cookies array (or parts of it)

Parameters
\Zend\Http\Header\SetCookie | array$ptr
int$retAsWhat value to return
Returns
array|string
_matchDomain (   $domain)
protected

Return a subset of the cookies array matching a specific domain.

Parameters
string$domain
Returns
array
_matchPath (   $domains,
  $path 
)
protected

Return a subset of a domain-matching cookies that also match a specified path.

Parameters
array$domains
string$path
Returns
array
addCookie (   $cookie,
  $refUri = null 
)

Add a cookie to the class.

Cookie should be passed either as a Zend object or as a string - in which case an object is created from the string.

Parameters
SetCookie | string$cookie
Uri\Uri | string$refUriOptional reference URI (for domain, path, secure)
Exceptions
Exception\InvalidArgumentException
addCookiesFromResponse ( Response  $response,
  $refUri 
)

Parse an HTTP response, adding all the cookies set in that response.

Parameters
Response$response
Uri\Uri | string$refUriRequested URI
static fromResponse ( Response  $response,
  $refUri 
)
static

Create a new Cookies object and automatically load into it all the cookies set in a Response object.

If $uri is set, it will be considered as the requested URI for setting default domain and path of the cookie.

Parameters
Response$responseHTTP Response object
Uri\Uri | string$refUriThe requested URI
Returns
Cookies
Todo:
Add the $uri functionality.
static fromString (   $string)
static
Exceptions
Exception\RuntimeException
Parameters
$string
Returns
void
getAllCookies (   $retAs = self::COOKIE_OBJECT)

Get all cookies in the cookie jar as an array.

Parameters
int$retAsWhether to return cookies as objects of or as strings
Returns
array|string
getCookie (   $uri,
  $cookieName,
  $retAs = self::COOKIE_OBJECT 
)

Get a specific cookie according to a URI and name.

Parameters
Uri\Uri | string$uriThe uri (domain and path) to match
string$cookieNameThe cookie's name
int$retAsWhether to return cookies as objects of or as strings
Exceptions
Exception\InvalidArgumentExceptionif invalid URI specified or invalid $retAs value
Returns
SetCookie|string
getMatchingCookies (   $uri,
  $matchSessionCookies = true,
  $retAs = self::COOKIE_OBJECT,
  $now = null 
)

Return an array of all cookies matching a specific request according to the request URI, whether session cookies should be sent or not, and the time to consider as "now" when checking cookie expiry time.

Parameters
string | Uri\Uri$uriURI to check against (secure, domain, path)
bool$matchSessionCookiesWhether to send session cookies
int$retAsWhether to return cookies as objects of or as strings
int$nowOverride the current time when checking for expiry time
Exceptions
Exception\InvalidArgumentExceptionif invalid URI specified
Returns
array|string
isEmpty ( )

Tells if the array of cookies is empty.

Returns
bool
reset ( )

Empties the cookieJar of any cookie.

Returns
Cookies

Member Data Documentation

$cookies = []
protected
$headers = null
protected
$rawCookies
protected
const COOKIE_OBJECT = 0

Return cookie(s) as a Zend object.

const COOKIE_STRING_ARRAY = 1

Return cookie(s) as a string (suitable for sending in an HTTP request)

const COOKIE_STRING_CONCAT = 2

Return all cookies as one long string (suitable for sending in an HTTP request)

const COOKIE_STRING_CONCAT_STRICT = 3

Return all cookies as one long string (strict mode)

  • Single space after the semi-colon separating each cookie
  • Remove trailing semi-colon, if any