Oihana PHP System

getParamI18n.php

Table of Contents

Functions

getParamI18n()  : array<string|int, mixed>|null
Retrieves an i18n parameter from the HTTP request, optionally filtered by allowed languages.

Functions

getParamI18n()

Retrieves an i18n parameter from the HTTP request, optionally filtered by allowed languages.

getParamI18n(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $default = [] ][, array<string|int, string>|null $languages = null ][, callable|null $sanitize = null ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : array<string|int, mixed>|null

This helper retrieves a parameter $name from the PSR-7 $request, supporting query string, body, or both (HttpParamStrategy). The parameter can be an array or object of translations (e.g., ['fr' => 'Bonjour', 'en' => 'Hello']). It then filters the values according to $languages and optionally applies a $sanitize callback on each value.

Nested keys in the request are supported via dot notation (e.g., 'user.profile.email').

If the parameter is not found:

  • Returns $default[$name] if present.
  • Returns null if no default is provided.
  • Throws NotFoundException if $throwable is true.
Parameters
$request : ServerRequestInterface|null

The PSR-7 server request instance.

$name : string

The parameter name or dot-notated path.

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

Optional default values as an associative array.

$languages : array<string|int, string>|null = null

Optional array of languages to filter the i18n definitions. If null, no filtering is applied.

$sanitize : callable|null = null

Optional callback to transform or sanitize each value. Signature: fn(string|null $value, string $lang): string|null

$strategy : string|null = HttpParamStrategy::BOTH

One of HttpParamStrategy::QUERY|BODY|BOTH. Default: BOTH.

$throwable : bool = false

Whether to throw a NotFoundException if parameter is missing. Default: false.

Tags
throws
NotFoundException

If $throwable is true and the parameter is not found.

example
$request = ...; // PSR-7 request with body/query

// Retrieve translations for 'description' filtered to 'fr' and 'en'
$translations = getParamI18n
(
     $request ,
     'description' ,
     ['description' => ['fr' => 'Default FR', 'en' => 'Default EN']],
     ['fr', 'en'],
     fn($v,$lang) => is_string($v) ? strtoupper($v) : $v
);
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array<string|int, mixed>|null

The i18n value if found, otherwise the default value or null.


        
On this page

Search results