Oihana PHP

PrepareLang uses trait:short

Prepares the `lang` parameter of a controller request.

This trait resolves the requested language from the ControllerParam::LANG query parameter against the supported $languages set provided by LanguagesTrait. A recognized language is lowercased and recorded in the parameter bag, while the special ControllerParam::ALL value resets it to null (no language filtering).

Only the query string is inspected (the request body is ignored).

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Constants

LANGUAGES  : string = 'languages'
Key used to initialize languages in an array or container.

Properties

$languages  : array<string|int, string>
Array of valid language codes supported by the controller.

Methods

initializeLanguages()  : static
Initialize the internal `$languages` property from an array or a PSR-11 container.
prepareLang()  : string|null
Prepares and returns the `lang` value.

Constants

LANGUAGES

Key used to initialize languages in an array or container.

public string LANGUAGES = 'languages'

Properties

$languages

Array of valid language codes supported by the controller.

public array<string|int, string> $languages = []

Methods

initializeLanguages()

Initialize the internal `$languages` property from an array or a PSR-11 container.

public initializeLanguages([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static

This method first checks the provided $init array for a key 'languages'. If not found, it optionally checks a PSR-11 container for fallback configuration.

Parameters
$init : array<string|int, mixed> = []

Optional array with key 'languages' containing supported language codes.

$container : ContainerInterface|null = null

Optional PSR-11 container for fallback configuration.

Tags
throws
ContainerExceptionInterface

If the container encounters an error while retrieving an entry.

NotFoundExceptionInterface

If no entry was found in the container for the given identifier.

Return values
static

Returns the current instance for method chaining.

prepareLang()

Prepares and returns the `lang` value.

protected prepareLang(ServerRequestInterface|null $request[, array<string|int, mixed> $args = [] ][, array<string|int, mixed>|null &$params = null ]) : string|null
Parameters
$request : ServerRequestInterface|null

The incoming PSR-7 server request, or null when no request context is available.

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

The route/controller arguments that may carry an initial lang value.

$params : array<string|int, mixed>|null = null

A reference to the parameter bag updated in place with the resolved language.

Return values
string|null

The resolved language code, or null when language filtering is disabled.

On this page

Search results