Fr3nch13/CakePHP Utilities

ContentTypeNegotiation
in package

Negotiates the prefered content type from what the application provides and what the request has in its Accept header.

Table of Contents

Methods

acceptedLanguages()  : array<string|int, string>
Get the normalized list of accepted languages
acceptLanguage()  : bool
Check if the request accepts a given language code.
parseAccept()  : array<string, array<string|int, string>>
Parse Accept* headers with qualifier options.
parseAcceptLanguage()  : array<string, array<string|int, string>>
Parse the Accept-Language header
preferredType()  : string|null
Get the most preferred content type from a request.
parseQualifiers()  : array<string, array<string|int, string>>
Parse a header value into preference => value mapping

Methods

acceptedLanguages()

Get the normalized list of accepted languages

public acceptedLanguages(RequestInterface $request) : array<string|int, string>

Language codes in the request will be normalized to lower case and have _ replaced with -.

Parameters
$request : RequestInterface

The request to read headers from.

Return values
array<string|int, string>

A list of language codes that are accepted.

acceptLanguage()

Check if the request accepts a given language code.

public acceptLanguage(RequestInterface $request, string $lang) : bool

Language codes in the request will be normalized to lower case and have _ replaced with -.

Parameters
$request : RequestInterface

The request to read headers from.

$lang : string

The language code to check.

Return values
bool

Whether the request accepts $lang

parseAccept()

Parse Accept* headers with qualifier options.

public parseAccept(RequestInterface $request) : array<string, array<string|int, string>>

Only qualifiers will be extracted, any other accept extensions will be discarded as they are not frequently used.

Parameters
$request : RequestInterface

The request to get an accept from.

Return values
array<string, array<string|int, string>>

A mapping of preference values => content types

parseAcceptLanguage()

Parse the Accept-Language header

public parseAcceptLanguage(RequestInterface $request) : array<string, array<string|int, string>>

Only qualifiers will be extracted, other extensions will be ignored as they are not frequently used.

Parameters
$request : RequestInterface

The request to get an accept from.

Return values
array<string, array<string|int, string>>

A mapping of preference values => languages

preferredType()

Get the most preferred content type from a request.

public preferredType(RequestInterface $request[, array<string|int, string> $choices = [] ]) : string|null

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
$request : RequestInterface

The request to use.

$choices : array<string|int, string> = []

The supported content type choices.

Return values
string|null

The prefered type or null if there is no match with choices or if the request had no Accept header.

parseQualifiers()

Parse a header value into preference => value mapping

protected parseQualifiers(string $header) : array<string, array<string|int, string>>
Parameters
$header : string

The header value to parse

Return values
array<string, array<string|int, string>>

        
On this page

Search results