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
Methods |
public
static
|
setDefaults(array<string, mixed>|Cookie $config = []): array<string, mixed>
Set the default attributes to a Cookie instance by injecting
the values from the `CookieConfig` config or an array.
Set the default attributes to a Cookie instance by injecting
the values from the CookieConfig config or an array.
This method is called from Response::__construct().
Returns
The old defaults array. Useful for resetting.
|
#
|
public
static
|
fromHeaderString(string $cookie, bool $raw = false): static
Create a new Cookie instance from a `Set-Cookie` header.
Create a new Cookie instance from a Set-Cookie header.
Throws
|
#
|
final
public
|
__construct(string $name, string $value = '', array<string, mixed> $options = [])
Construct a new Cookie instance.
Construct a new Cookie instance.
Parameters
$name |
The cookie's name
|
$value |
The cookie's value
|
$options |
The cookie's options
|
Throws
|
#
|
public
|
getId(): string
{@inheritDoc}
|
#
|
public
|
getPrefix(): string
{@inheritDoc}
|
#
|
public
|
getName(): string
{@inheritDoc}
|
#
|
public
|
getPrefixedName(): string
{@inheritDoc}
|
#
|
public
|
getValue(): string
{@inheritDoc}
|
#
|
public
|
getExpiresTimestamp(): int
{@inheritDoc}
|
#
|
public
|
getExpiresString(): string
{@inheritDoc}
|
#
|
public
|
isExpired(): bool
{@inheritDoc}
|
#
|
public
|
getMaxAge(): int
{@inheritDoc}
|
#
|
public
|
getPath(): string
{@inheritDoc}
|
#
|
public
|
getDomain(): string
{@inheritDoc}
|
#
|
public
|
isSecure(): bool
{@inheritDoc}
|
#
|
public
|
isHTTPOnly(): bool
{@inheritDoc}
|
#
|
public
|
getSameSite(): string
{@inheritDoc}
|
#
|
public
|
isRaw(): bool
{@inheritDoc}
|
#
|
public
|
getOptions(): array
{@inheritDoc}
|
#
|
public
|
withPrefix(string $prefix = '')
{@inheritDoc}
|
#
|
public
|
withName(string $name)
{@inheritDoc}
|
#
|
public
|
withValue(string $value)
{@inheritDoc}
|
#
|
public
|
withExpires($expires)
{@inheritDoc}
|
#
|
public
|
withExpired()
{@inheritDoc}
|
#
|
public
|
withNeverExpiring()
Creates a new Cookie that will virtually never expire from the browser.
Creates a new Cookie that will virtually never expire from the browser.
Deprecated
Implements
|
#
|
public
|
withPath(?string $path)
{@inheritDoc}
|
#
|
public
|
withDomain(?string $domain)
{@inheritDoc}
|
#
|
public
|
withSecure(bool $secure = true)
{@inheritDoc}
|
#
|
public
|
withHTTPOnly(bool $httponly = true)
{@inheritDoc}
|
#
|
public
|
withSameSite(string $samesite)
{@inheritDoc}
|
#
|
public
|
withRaw(bool $raw = true)
{@inheritDoc}
|
#
|
public
|
offsetExists(string $offset): bool
Whether an offset exists.
Whether an offset exists.
Implements
|
#
|
public
|
offsetGet(string $offset): bool|int|string
Offset to retrieve.
|
#
|
public
|
offsetSet(string $offset, mixed $value): void
Offset to set.
|
#
|
public
|
offsetUnset(string $offset): void
Offset to unset.
|
#
|
public
|
toHeaderString(): string
{@inheritDoc}
|
#
|
public
|
__toString()
{@inheritDoc}
|
#
|
public
|
toArray(): array
{@inheritDoc}
|
#
|
protected
static
|
convertExpiresTimestamp(DateTimeInterface|int|string $expires = 0): int
Converts expires time to Unix format.
Converts expires time to Unix format.
|
#
|
protected
|
validateName(string $name, bool $raw): void
Validates the cookie name per RFC 2616.
Validates the cookie name per RFC 2616.
If $raw is true, names should not contain invalid characters
as setrawcookie() will reject this.
Throws
|
#
|
protected
|
validatePrefix(string $prefix, bool $secure, string $path, string $domain): void
Validates the special prefixes if some attribute requirements are met.
Validates the special prefixes if some attribute requirements are met.
Throws
|
#
|
protected
|
validateSameSite(string $samesite, bool $secure): void
Validates the `SameSite` to be within the allowed types.
Validates the SameSite to be within the allowed types.
Throws
|
#
|