Fr3nch13/CakePHP Utilities

CookieInterface

Cookie Interface

Table of Contents

Constants

EXPIRES_FORMAT  = 'D, d-M-Y H:i:s T'
Expires attribute format.
SAMESITE_LAX  = 'Lax'
SameSite attribute value: Lax
SAMESITE_NONE  = 'None'
SameSite attribute value: None
SAMESITE_STRICT  = 'Strict'
SameSite attribute value: Strict
SAMESITE_VALUES  = [self::SAMESITE_LAX, self::SAMESITE_STRICT, self::SAMESITE_NONE]
Valid values for "SameSite" attribute.

Methods

getDomain()  : string
Get the domain attribute.
getExpiresTimestamp()  : int|null
Get the timestamp from the expiration time
getExpiry()  : DateTime|DateTimeImmutable|null
Get the current expiry time
getFormattedExpires()  : string
Builds the expiration value part of the header string
getId()  : string
Get the id for a cookie
getName()  : string
Gets the cookie name
getOptions()  : array<string, mixed>
Get cookie options
getPath()  : string
Get the path attribute.
getSameSite()  : string|null
Get the SameSite attribute.
getScalarValue()  : mixed
Gets the cookie value as scalar.
getValue()  : array<string|int, mixed>|string
Gets the cookie value
isExpired()  : bool
Check if a cookie is expired when compared to $time
isHttpOnly()  : bool
Check if the cookie is HTTP only
isSecure()  : bool
Check if the cookie is secure
toArray()  : array<string, mixed>
Get cookie data as array.
toHeaderValue()  : string
Returns the cookie as header value
withDomain()  : static
Create a cookie with an updated domain
withExpired()  : static
Create a new cookie that will expire/delete the cookie from the browser.
withExpiry()  : static
Create a cookie with an updated expiration date
withHttpOnly()  : static
Create a cookie with HTTP Only updated
withName()  : static
Sets the cookie name
withNeverExpire()  : static
Create a new cookie that will virtually never expire.
withPath()  : static
Create a new cookie with an updated path
withSameSite()  : static
Create a cookie with an updated SameSite option.
withSecure()  : static
Create a cookie with Secure updated
withValue()  : static
Create a cookie with an updated value.

Constants

EXPIRES_FORMAT

Expires attribute format.

public string EXPIRES_FORMAT = 'D, d-M-Y H:i:s T'

SAMESITE_LAX

SameSite attribute value: Lax

public string SAMESITE_LAX = 'Lax'

SAMESITE_NONE

SameSite attribute value: None

public string SAMESITE_NONE = 'None'

SAMESITE_STRICT

SameSite attribute value: Strict

public string SAMESITE_STRICT = 'Strict'

SAMESITE_VALUES

Valid values for "SameSite" attribute.

public array<string|int, string> SAMESITE_VALUES = [self::SAMESITE_LAX, self::SAMESITE_STRICT, self::SAMESITE_NONE]

Methods

getDomain()

Get the domain attribute.

public getDomain() : string
Return values
string

getExpiresTimestamp()

Get the timestamp from the expiration time

public getExpiresTimestamp() : int|null
Return values
int|null

The expiry time as an integer.

getExpiry()

Get the current expiry time

public getExpiry() : DateTime|DateTimeImmutable|null
Return values
DateTime|DateTimeImmutable|null

Timestamp of expiry or null

getFormattedExpires()

Builds the expiration value part of the header string

public getFormattedExpires() : string
Return values
string

getId()

Get the id for a cookie

public getId() : string

Cookies are unique across name, domain, path tuples.

Return values
string

getName()

Gets the cookie name

public getName() : string
Return values
string

getOptions()

Get cookie options

public getOptions() : array<string, mixed>
Return values
array<string, mixed>

getPath()

Get the path attribute.

public getPath() : string
Return values
string

getSameSite()

Get the SameSite attribute.

public getSameSite() : string|null
Return values
string|null

getScalarValue()

Gets the cookie value as scalar.

public getScalarValue() : mixed

This will collapse any complex data in the cookie with json_encode()

getValue()

Gets the cookie value

public getValue() : array<string|int, mixed>|string
Return values
array<string|int, mixed>|string

isExpired()

Check if a cookie is expired when compared to $time

public isExpired([DateTime|DateTimeImmutable $time = null ]) : bool

Cookies without an expiration date always return false.

Parameters
$time : DateTime|DateTimeImmutable = null

The time to test against. Defaults to 'now' in UTC.

Return values
bool

isHttpOnly()

Check if the cookie is HTTP only

public isHttpOnly() : bool
Return values
bool

isSecure()

Check if the cookie is secure

public isSecure() : bool
Return values
bool

toArray()

Get cookie data as array.

public toArray() : array<string, mixed>
Return values
array<string, mixed>

With keys name, value, expires etc. options.

toHeaderValue()

Returns the cookie as header value

public toHeaderValue() : string
Return values
string

withDomain()

Create a cookie with an updated domain

public withDomain(string $domain) : static
Parameters
$domain : string

Domain to set

Return values
static

withExpired()

Create a new cookie that will expire/delete the cookie from the browser.

public withExpired() : static

This is done by setting the expiration time to 1 year ago

Return values
static

withExpiry()

Create a cookie with an updated expiration date

public withExpiry(DateTime|DateTimeImmutable $dateTime) : static
Parameters
$dateTime : DateTime|DateTimeImmutable

Date time object

Return values
static

withHttpOnly()

Create a cookie with HTTP Only updated

public withHttpOnly(bool $httpOnly) : static
Parameters
$httpOnly : bool

HTTP Only

Return values
static

withName()

Sets the cookie name

public withName(string $name) : static
Parameters
$name : string

Name of the cookie

Return values
static

withNeverExpire()

Create a new cookie that will virtually never expire.

public withNeverExpire() : static
Return values
static

withPath()

Create a new cookie with an updated path

public withPath(string $path) : static
Parameters
$path : string

Sets the path

Return values
static

withSameSite()

Create a cookie with an updated SameSite option.

public withSameSite(string|null $sameSite) : static
Parameters
$sameSite : string|null

Value for to set for Samesite option. One of CookieInterface::SAMESITE_* constants.

Return values
static

withSecure()

Create a cookie with Secure updated

public withSecure(bool $secure) : static
Parameters
$secure : bool

Secure attribute value

Return values
static

withValue()

Create a cookie with an updated value.

public withValue(array<string|int, mixed>|string $value) : static
Parameters
$value : array<string|int, mixed>|string

Value of the cookie to set

Return values
static

        
On this page

Search results