Luminova Framework

PHP Luminova: Client Side Cookie Management

Last updated: 2024-12-07 04:12:00

The cookie class enhanced client-side data storage without Default JavaScript or HTTP-only restrictions, allowing you to configure your restrictions as needed.

Luminova's Cookie manager provides a convenient way to store data on the client-side, offering greater flexibility in managing cookie data compared to the SessionManager and CookieManager classes. With the Cookie class, you have the ability to configure the security access of the stored information according to your specific requirements.

The Cookie class stores data directly on the client-side, within cookies. This approach is particularly useful for scenarios where client-side storage is preferred or necessary, such as persisting user preferences, session tokens, or other application-specific data.


Class Definition


Properties

The default cookie options.

protected array $default = [
    'prefix' => '',
    'expires'  => 0,
    'path'     => '/',
    'domain'   => '',
    'secure'   => false,
    'httponly' => true,
    'samesite' => 'Lax',
    'raw'      => false,
];

Constants

These constants define common cookie attributes and formatting options used for setting and managing cookies.

ConstantTypeValueDescription
NONEstring'none'Represents the SameSite=None policy, allowing cookies to be sent in all contexts, including cross-site requests.
LAXstring'lax'Represents the SameSite=Lax policy, allowing cookies in top-level navigations and some cross-site requests.
STRICTstring'strict'Represents the SameSite=Strict policy, restricting cookies to the same site only.

Methods

constructor

Cookie class initialization. This class cannot be extended.

new Cookie(string $name, mixed $value = '', array $options = []): mixed

Parameters:

ParameterTypeDescription
$namestringCookie name
$valuemixedcookie value
$optionsarrayOptional cookie options if not passed the default will be used

Throws:


newFromString

Create a new Cookie instance from a Set-Cookie header.

public newFromString(string $cookie, bool $raw = false, Cookie|array $options = []): \Luminova\Interface\CookieInterface

Parameters:

ParameterTypeDescription
$cookiestringThe cookie header string.
$rawboolIndicates if the cookie is raw.
$optionsApp\Config\Cookie|array<string,mixed>An array of default cookie options or cookie config class object.

Return Value:

Luminova\Interface\CookieInterface - Return a new Cookie instance.


newFromArray

Create a new Cookie instance from an array or json object.

public newFromArray(array|object $cookies, Cookie|array $options = []): \Luminova\Interface\CookieInterface

Parameters:

ParameterTypeDescription
$cookiearray<string,mixed>|objectAn associative array or json object of cookies.
$optionsApp\Config\Cookie|array<string,mixed>An array of default cookie options or cookie config class object.

Return Value:

Luminova\Interface\CookieInterface - Return a new Cookie instance.


set

Set a cookie.

public set(mixed $name, mixed $value, array $options = []): \Luminova\Interface\CookieInterface

Parameters:

ParameterTypeDescription
$namemixedThe name of the cookie.
$valuemixedThe value of the cookie.
$optionsarrayAn array of cookie options.

Return Value:

CookieInterface - Return new Cookie instance.


setValue

Set the value of a cookie.

public setValue(mixed $value): self

Parameters:

ParameterTypeDescription
$valuemixedThe value to set.

Return Value:

self - Return the instance of Cookie class.


setOptions

Set cookie options.

public setOptions(Cookie|array $options): \Luminova\Interface\CookieInterface

Parameters:

ParameterTypeDescription
$optionsApp\Config\Cooke|array<string,mixed>An array of cookie options or cookie config class object.

Return Value:

CookieInterface - Return instance of Cookie class.


toString

Convert the cookie object to a string.

public toString(): string

Return Value:

string - The string representation of the Cookie object.


toArray

Convert the cookie to an array.

public toArray(): array<string, mixed>

Return Value:

array<string, mixed> - The array representation of the Cookie object.


get

Retrieve the value of a cookie, if key is specified it will return value of the key .

public get(?string $key = null): mixed

Parameters:

ParameterTypeDescription
$keystring|nullOptional key of the cookie to retrieve.

Return Value:

mixed - The value of the cookie.


getName

Get the name of the cookie.

public getName(): string

Return Value:

string - The name of the cookie.


getOptions

Get the options associated with the cookie.

public getOptions(): array

Return Value:

array - The options associated with the cookie.


getValue

Get the value of the cookie.

public getValue(): mixed

Return Value:

mixed - The value of the cookie.


getDomain

Get the domain associated with the cookie.

public getDomain(): string

Return Value:

string - The domain associated with the cookie.


getPrefix

Get the prefix associated with the cookie.

public getPrefix(): string

Return Value:

string - The prefix associated with the cookie.


getMaxAge

Get the maximum age of the cookie.

public getMaxAge(): int

Return Value:

int - The maximum age of the cookie.


getPath

Get the path associated with the cookie.

public getPath(): string

Return Value:

string - The path associated with the cookie.


getSameSite

Get the SameSite attribute of the cookie.

public getSameSite(): string

Return Value:

string - The SameSite attribute of the cookie.


getId

Get the ID of the cookie.

public getId(): string

Return Value:

string - The ID of the cookie.


getPrefixedName

Get the prefixed name of the cookie.

public getPrefixedName(): string

Return Value:

string - The prepended prefix name of the cookie.


getExpiry

Get the cookie expiration time.

public getExpiry(bool $return_string = false): string|int

Parameters:

ParameterTypeDescription
$return_stringboolReturn cookie expiration timestamp or string presentation.

Return Value:

string|int - The expiration time.


isSecure

Check if the cookie is secure.

public isSecure(): bool

Return Value:

bool - True if the cookie is secure, false otherwise.


isHttpOnly

Check if the cookie is HTTP-only.

public isHttpOnly(): bool

Return Value:

bool - True if the cookie is HTTP-only, false otherwise.


isRaw

Check if the cookie value is raw.

public isRaw(): bool

Return Value:

bool - True if the cookie value is raw, false otherwise.


isExpired

Check if the cookie has expired.

public isExpired(): bool

Return Value:

bool - Return true if the cookie has expired otherwise false.


hasPrefix

Check if a cookie name has a prefix.

public hasPrefix(?string $name = null): bool

Parameters:

ParameterTypeDescription
$namestring|nullThe name of the cookie.

Return Value:

bool - True if the cookie name has a prefix, false otherwise.


has

Check if a cookie exists, if key is passed NULL, it checks if the current cookie object name exists.

public has(?string $key = null): bool

Parameters:

ParameterTypeDescription
$keystring|nullThe key of the cookie to check.

Return Value:

bool - True if the cookie exists, false otherwise.


delete

Remove a cookie, If key is null or empty, delete the entire cookie.

public delete(?string $key = null): bool

Parameters:

ParameterTypeDescription
$keystring|nullThe key of the cookie to remove.

Return Value:

bool - Return true if the cookie was removed, false otherwise.