Class ContentTypeNegotiation
Negotiates the prefered content type from what the application provides and what the request has in its Accept header.
Method Summary
- 
          acceptLanguage() publicCheck if the request accepts a given language code. 
- 
          acceptedLanguages() publicGet the normalized list of accepted languages 
- 
          parseAccept() publicParse Accept* headers with qualifier options. 
- 
          parseAcceptLanguage() publicParse the Accept-Language header 
- 
          parseQualifiers() protectedParse a header value into preference => value mapping 
- 
          preferredType() publicGet the most preferred content type from a request. 
Method Detail
acceptLanguage() ¶ public
acceptLanguage(Psr\Http\Message\RequestInterface $request, string $lang): boolCheck 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
boolWhether the request accepts $lang
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>A list of language codes that are accepted.
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>>A mapping of preference values => content types
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>>A mapping of preference values => languages
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, list<string> $choices = []): string|nullGet 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. 
- 
                list<string>$choices optional
- The supported content type choices. 
Returns
string|nullThe prefered type or null if there is no match with choices or if the request had no Accept header.
