Class ContentTypeNegotiation
Negotiates the prefered content type from what the application provides and what the request has in its Accept header.
Method Summary
-
acceptLanguage() public
Check if the request accepts a given language code.
-
acceptedLanguages() public
Get the normalized list of accepted languages
-
parseAccept() public
Parse Accept* headers with qualifier options.
-
parseAcceptLanguage() public
Parse the Accept-Language header
-
parseQualifiers() protected
Parse a header value into preference => value mapping
-
preferredType() public
Get the most preferred content type from a request.
Method Detail
acceptLanguage() ¶ public
acceptLanguage(Psr\Http\Message\RequestInterface $request, string $lang): bool
Check if the request accepts a given language code.
Language codes in the request will be normalized to lower case and have _
replaced
with -
.
Parameters
-
Psr\Http\Message\RequestInterface
$request The request to read headers from.
-
string
$lang The language code to check.
Returns
bool
acceptedLanguages() ¶ public
acceptedLanguages(Psr\Http\Message\RequestInterface $request): array<string>
Get the normalized list of accepted languages
Language codes in the request will be normalized to lower case and have
_
replaced with -
.
Parameters
-
Psr\Http\Message\RequestInterface
$request The request to read headers from.
Returns
array<string>
parseAccept() ¶ public
parseAccept(Psr\Http\Message\RequestInterface $request): array<string, array<string>>
Parse Accept* headers with qualifier options.
Only qualifiers will be extracted, any other accept extensions will be discarded as they are not frequently used.
Parameters
-
Psr\Http\Message\RequestInterface
$request The request to get an accept from.
Returns
array<string, array<string>>
parseAcceptLanguage() ¶ public
parseAcceptLanguage(Psr\Http\Message\RequestInterface $request): array<string, array<string>>
Parse the Accept-Language header
Only qualifiers will be extracted, other extensions will be ignored as they are not frequently used.
Parameters
-
Psr\Http\Message\RequestInterface
$request The request to get an accept from.
Returns
array<string, array<string>>
parseQualifiers() ¶ protected
parseQualifiers(string $header): array<string, array<string>>
Parse a header value into preference => value mapping
Parameters
-
string
$header The header value to parse
Returns
array<string, array<string>>
preferredType() ¶ public
preferredType(Psr\Http\Message\RequestInterface $request, array<string> $choices = []): string|null
Get the most preferred content type from a request.
Parse the Accept header preferences and return the most preferred type. If multiple types are tied in preference the first type of that preference value will be returned.
You can expect null when the request has no Accept header.
Parameters
-
Psr\Http\Message\RequestInterface
$request The request to use.
-
array<string>
$choices optional The supported content type choices.
Returns
string|null