Skip to content

CookieSerializer

Turns the cookie definitions queued on a response into Set-Cookie header lines.

Pure translation: given a cookie’s declared attributes it produces the normalized form and the header string, and nothing else. It reaches for no context, no routing and no response state — the default path a cookie inherits when it declares none is passed in, so the same definition always serializes the same way and can be asserted on directly.

final class CookieSerializer

Since3.2.0
SourceResponse/CookieSerializer.php

public function __construct(string $defaultPath = '/', ClockInterface $clock = new SystemClock(…)): mixed

Path for a cookie that declares none. An empty string is treated as ”/”, so a cookie is never scoped to the empty path.

ParameterTypeDescription
$defaultPathstringPath for a cookie that declares none. An empty string is treated as ”/”, so a cookie is never scoped to the empty path.
$clockClockInterface

Returns mixed

MethodDescription
header(string $name, array{value: string, expires: ?int, max_age: ?int, path: string, domain: ?string, secure: bool, httponly: bool, samesite: ?string} $normalized): stringThe Set-Cookie value for an already-normalized cookie.
headers(array<string, array<array-key, mixed>> $cookies): list<string>Header lines for every queued cookie, in queue order.
normalize(string $name, array<array-key, mixed> $cookie): array{value: string, expires: ?int, max_age: ?int, path: string, domain: ?string, secure: bool, httponly: bool, samesite: ?string}Resolve a cookie definition into the concrete attribute set to be sent.

public function header(string $name, array{value: string, expires: ?int, max_age: ?int, path: string, domain: ?string, secure: bool, httponly: bool, samesite: ?string} $normalized): string

The Set-Cookie value for an already-normalized cookie.

ParameterTypeDescription
$namestring
$normalizedarray{value: string, expires: ?int, max_age: ?int, path: string, domain: ?string, secure: bool, httponly: bool, samesite: ?string}

Returns string

public function headers(array<string, array<array-key, mixed>> $cookies): list<string>

Header lines for every queued cookie, in queue order.

Name => definition.

ParameterTypeDescription
$cookiesarray``<``string``, ``array``<``array-key``, ``mixed``>``>Name => definition.

Returns list``<``string``>

public function normalize(string $name, array<array-key, mixed> $cookie): array{value: string, expires: ?int, max_age: ?int, path: string, domain: ?string, secure: bool, httponly: bool, samesite: ?string}

Resolve a cookie definition into the concrete attribute set to be sent.

Keys: value, lifetime, path, domain, secure, httponly, encode_callback, samesite — all optional.

ParameterTypeDescription
$namestring
$cookiearray``<``array-key``, ``mixed``>Keys: value, lifetime, path, domain, secure, httponly, encode_callback, samesite — all optional.

Returns array{value: string, expires: ?int, max_age: ?int, path: string, domain: ?string, secure: bool, httponly: bool, samesite: ?string}