filterLanguages.php
Table of Contents
Functions
- filterLanguages() : array<string, string|null>|null
- Filter an array or object of translations according to the given or available languages.
Functions
filterLanguages()
Filter an array or object of translations according to the given or available languages.
filterLanguages(mixed $fields[, array<string|int, string>|null $languages = null ][, callable|null $sanitize = null ]) : array<string, string|null>|null
This helper transforms an input array/object from the client to prepare a multilingual (i18n) property. It keeps only string or null values, allows optional transformation or sanitization via a callback.
๐ Only the languages actually received come back. A translation map is edited like every other
field of a partial body : what the caller did not mention is left alone. Filling the absent languages
in โ with a null, as this helper used to โ turned every partial edit into a full replacement : a body
carrying { "fr": "Bonjour" } rewrote the English to null, and the caller read a 200 with nothing
to warn them. The rule is now the one that governs the rest of a partial write : absent means
untouched, and it is what lets a language be added to a project without every edit wiping it.
๐ An empty string is normalised to null. A label that exists but says nothing states no more
than a missing label, and keeping both shapes means every reader must test for both. The
normalisation runs after the sanitize callback, so a value that sanitizing empties follows the same
rule. Clearing one language is therefore { "fr": null } or { "fr": "" }, indifferently.
โ ๏ธ An input carrying no usable language at all โ an empty map, or only unknown languages โ still
returns null, which a payload layer reads as an explicit null : ยซ clear the whole property ยป. It is
unchanged behaviour, and the reason a caller meaning ยซ touch nothing ยป must omit the property rather
than send an empty map.
Note: this helper is permissive on input shape โ invalid inputs (string, scalar, etc.) silently return null rather than throwing. Callers that need to reject invalid shapes (e.g. to return a 422) must validate the raw input upstream before calling this helper.
Parameters
- $fields : mixed
-
Input translations (array<string,string|null> or object). Any other shape (string, scalar, โฆ) is treated as invalid and ignored โ the function returns null. Type validation must be done upstream by callers.
- $languages : array<string|int, string>|null = null
-
Optional array of allowed languages. A language outside the list is dropped ;
nullapplies no filtering and keeps every language received. - $sanitize : callable|null = null
-
Optional callback to transform or sanitize each value. Signature:
fn(string|null $value, string $lang): string|null
Tags
Return values
array<string, string|null>|null —The received translations, filtered and normalised, or null when the input holds no usable language.