Cookie
in package
implements
ArrayAccess, CloneableCookieInterface
A `Cookie` class represents an immutable HTTP cookie value object.
Being immutable, modifying one or more of its attributes will return
a new Cookie
instance, rather than modifying itself. Users should
reassign this new instance to a new variable to capture it.
$cookie = new Cookie('test_cookie', 'test_value');
$cookie->getName(); // test_cookie
$cookie->withName('prod_cookie');
$cookie->getName(); // test_cookie
$cookie2 = $cookie->withName('prod_cookie');
$cookie2->getName(); // prod_cookie
Interfaces, Classes and Traits
- ArrayAccess
- CloneableCookieInterface
- Interface for a fresh Cookie instance with selected attribute(s) only changed from the original instance.
Table of Contents
- $domain : string
- $expires : int
- $httponly : bool
- $name : string
- $path : string
- $prefix : string
- $raw : bool
- $samesite : string
- $secure : bool
- $value : string
- $defaults : array<string, mixed>
- Default attributes for a Cookie object. The keys here are the lowercase attribute names. Do not camelCase!
- $reservedCharsList : string
- A cookie name can be any US-ASCII characters, except control characters, spaces, tabs, or separator characters.
- __construct() : mixed
- Construct a new Cookie instance.
- __toString() : mixed
- {@inheritDoc}
- fromHeaderString() : static
- Create a new Cookie instance from a `Set-Cookie` header.
- getDomain() : string
- {@inheritDoc}
- getExpiresString() : string
- {@inheritDoc}
- getExpiresTimestamp() : int
- {@inheritDoc}
- getId() : string
- {@inheritDoc}
- getMaxAge() : int
- {@inheritDoc}
- getName() : string
- {@inheritDoc}
- getOptions() : array<string|int, mixed>
- {@inheritDoc}
- getPath() : string
- {@inheritDoc}
- getPrefix() : string
- {@inheritDoc}
- getPrefixedName() : string
- {@inheritDoc}
- getSameSite() : string
- {@inheritDoc}
- getValue() : string
- {@inheritDoc}
- isExpired() : bool
- {@inheritDoc}
- isHTTPOnly() : bool
- {@inheritDoc}
- isRaw() : bool
- {@inheritDoc}
- isSecure() : bool
- {@inheritDoc}
- offsetExists() : bool
- Whether an offset exists.
- offsetGet() : mixed
- Offset to retrieve.
- offsetSet() : void
- Offset to set.
- offsetUnset() : void
- Offset to unset.
- setDefaults() : array<string, mixed>
- Set the default attributes to a Cookie instance by injecting the values from the `CookieConfig` config or an array.
- toArray() : array<string|int, mixed>
- {@inheritDoc}
- toHeaderString() : string
- {@inheritDoc}
- withDomain() : static
- Creates a new Cookie with a new domain the cookie is available.
- withExpired() : static
- Creates a new Cookie that will expire the cookie from the browser.
- withExpires() : static
- Creates a new Cookie with a new cookie expires time.
- withHTTPOnly() : static
- Creates a new Cookie with a new "HttpOnly" attribute
- withName() : static
- Creates a new Cookie with a new name.
- withNeverExpiring() : static
- Creates a new Cookie that will virtually never expire from the browser.
- withPath() : static
- Creates a new Cookie with a new path on the server the cookie is available.
- withPrefix() : static
- Creates a new Cookie with a new cookie prefix.
- withRaw() : static
- Creates a new Cookie with URL encoding option updated.
- withSameSite() : static
- Creates a new Cookie with a new "SameSite" attribute.
- withSecure() : static
- Creates a new Cookie with a new "Secure" attribute.
- withValue() : static
- Creates a new Cookie with new value.
- convertExpiresTimestamp() : int
- Converts expires time to Unix format.
- validateName() : void
- Validates the cookie name per RFC 2616.
- validatePrefix() : void
- Validates the special prefixes if some attribute requirements are met.
- validateSameSite() : void
- Validates the `SameSite` to be within the allowed types.
Properties
$domain
protected
string
$domain
= ''
$expires
protected
int
$expires
$httponly
protected
bool
$httponly
= true
$name
protected
string
$name
$path
protected
string
$path
= '/'
$prefix
protected
string
$prefix
= ''
$raw
protected
bool
$raw
= false
$samesite
protected
string
$samesite
= self::SAMESITE_LAX
$secure
protected
bool
$secure
= false
$value
protected
string
$value
$defaults
Default attributes for a Cookie object. The keys here are the lowercase attribute names. Do not camelCase!
private
static array<string, mixed>
$defaults
= ['prefix' => '', 'expires' => 0, 'path' => '/', 'domain' => '', 'secure' => false, 'httponly' => true, 'samesite' => self::SAMESITE_LAX, 'raw' => false]
$reservedCharsList
A cookie name can be any US-ASCII characters, except control characters, spaces, tabs, or separator characters.
private
static string
$reservedCharsList
= "=,;
()<>@:\"/[]?{}"
Tags
Methods
__construct()
Construct a new Cookie instance.
public
final __construct(string $name[, string $value = '' ][, array<string, mixed> $options = [] ]) : mixed
Parameters
- $name : string
-
The cookie's name
- $value : string = ''
-
The cookie's value
- $options : array<string, mixed> = []
-
The cookie's options
Tags
Return values
mixed —__toString()
{@inheritDoc}
public
__toString() : mixed
Return values
mixed —fromHeaderString()
Create a new Cookie instance from a `Set-Cookie` header.
public
static fromHeaderString(string $cookie[, bool $raw = false ]) : static
Parameters
- $cookie : string
- $raw : bool = false
Tags
Return values
static —getDomain()
{@inheritDoc}
public
getDomain() : string
Return values
string —getExpiresString()
{@inheritDoc}
public
getExpiresString() : string
Return values
string —getExpiresTimestamp()
{@inheritDoc}
public
getExpiresTimestamp() : int
Return values
int —getId()
{@inheritDoc}
public
getId() : string
Return values
string —getMaxAge()
{@inheritDoc}
public
getMaxAge() : int
Return values
int —getName()
{@inheritDoc}
public
getName() : string
Return values
string —getOptions()
{@inheritDoc}
public
getOptions() : array<string|int, mixed>
Return values
array<string|int, mixed> —getPath()
{@inheritDoc}
public
getPath() : string
Return values
string —getPrefix()
{@inheritDoc}
public
getPrefix() : string
Return values
string —getPrefixedName()
{@inheritDoc}
public
getPrefixedName() : string
Return values
string —getSameSite()
{@inheritDoc}
public
getSameSite() : string
Return values
string —getValue()
{@inheritDoc}
public
getValue() : string
Return values
string —isExpired()
{@inheritDoc}
public
isExpired() : bool
Return values
bool —isHTTPOnly()
{@inheritDoc}
public
isHTTPOnly() : bool
Return values
bool —isRaw()
{@inheritDoc}
public
isRaw() : bool
Return values
bool —isSecure()
{@inheritDoc}
public
isSecure() : bool
Return values
bool —offsetExists()
Whether an offset exists.
public
offsetExists(string $offset) : bool
Parameters
- $offset : string
Return values
bool —offsetGet()
Offset to retrieve.
public
offsetGet(string $offset) : mixed
Parameters
- $offset : string
Tags
Return values
mixed —offsetSet()
Offset to set.
public
offsetSet(string $offset, mixed $value) : void
Parameters
- $offset : string
- $value : mixed
Tags
Return values
void —offsetUnset()
Offset to unset.
public
offsetUnset(string $offset) : void
Parameters
- $offset : string
Tags
Return values
void —setDefaults()
Set the default attributes to a Cookie instance by injecting the values from the `CookieConfig` config or an array.
public
static setDefaults([Cookie|array<string, mixed> $config = [] ]) : array<string, mixed>
Parameters
- $config : Cookie|array<string, mixed> = []
Return values
array<string, mixed> —The old defaults array. Useful for resetting.
toArray()
{@inheritDoc}
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed> —toHeaderString()
{@inheritDoc}
public
toHeaderString() : string
Return values
string —withDomain()
Creates a new Cookie with a new domain the cookie is available.
public
withDomain(string|null $domain) : static
Parameters
- $domain : string|null
Return values
static —withExpired()
Creates a new Cookie that will expire the cookie from the browser.
public
withExpired() : static
Return values
static —withExpires()
Creates a new Cookie with a new cookie expires time.
public
withExpires(mixed $expires) : static
Parameters
- $expires : mixed
Return values
static —withHTTPOnly()
Creates a new Cookie with a new "HttpOnly" attribute
public
withHTTPOnly([bool $httponly = true ]) : static
Parameters
- $httponly : bool = true
Return values
static —withName()
Creates a new Cookie with a new name.
public
withName(string $name) : static
Parameters
- $name : string
Return values
static —withNeverExpiring()
Creates a new Cookie that will virtually never expire from the browser.
public
withNeverExpiring() : static
Return values
static —withPath()
Creates a new Cookie with a new path on the server the cookie is available.
public
withPath(string|null $path) : static
Parameters
- $path : string|null
Return values
static —withPrefix()
Creates a new Cookie with a new cookie prefix.
public
withPrefix([string $prefix = '' ]) : static
Parameters
- $prefix : string = ''
Return values
static —withRaw()
Creates a new Cookie with URL encoding option updated.
public
withRaw([bool $raw = true ]) : static
Parameters
- $raw : bool = true
Return values
static —withSameSite()
Creates a new Cookie with a new "SameSite" attribute.
public
withSameSite(string $samesite) : static
Parameters
- $samesite : string
Return values
static —withSecure()
Creates a new Cookie with a new "Secure" attribute.
public
withSecure([bool $secure = true ]) : static
Parameters
- $secure : bool = true
Return values
static —withValue()
Creates a new Cookie with new value.
public
withValue(string $value) : static
Parameters
- $value : string
Return values
static —convertExpiresTimestamp()
Converts expires time to Unix format.
protected
static convertExpiresTimestamp(DateTimeInterface|int|string $expires) : int
Parameters
- $expires : DateTimeInterface|int|string
Return values
int —validateName()
Validates the cookie name per RFC 2616.
protected
validateName(string $name, bool $raw) : void
If $raw
is true, names should not contain invalid characters
as setrawcookie()
will reject this.
Parameters
- $name : string
- $raw : bool
Tags
Return values
void —validatePrefix()
Validates the special prefixes if some attribute requirements are met.
protected
validatePrefix(string $prefix, bool $secure, string $path, string $domain) : void
Parameters
- $prefix : string
- $secure : bool
- $path : string
- $domain : string
Tags
Return values
void —validateSameSite()
Validates the `SameSite` to be within the allowed types.
protected
validateSameSite(string $samesite, bool $secure) : void
Parameters
- $samesite : string
- $secure : bool