N2nLocaleMapper
Maps an input value to an n2n\l10n\N2nLocale object (or null).
Accepts the following input types:
string— a locale id (e.g.'de','de_CH').- Any
Stringableobject. null— left asnull, subject to validation.
The value is parsed with N2nLocale::build(). Unlike most mappers, an unparseable value (a string shorter than two characters) throws a n2n\bind\err\BindMismatchException rather than producing a validation error.
Validation possibilities
| Parameter | Type | Default | Description |
|---|---|---|---|
| $mandatory | bool | false | Rejects null input. |
| $allowedValues | ?N2nLocale[] | null | Restricts the value to this list of N2nLocale instances (enum validation). |
Example: basic conversion
$result = Bind::values('de_CH', null)->map(Mappers::n2nLocale())->toArray()->exec();
var_dump($result->get());
Output:
array(2) {
[0]=>
object(n2n\l10n\N2nLocale)#1 (1) {
["id":"n2n\l10n\N2nLocale":private]=>
string(5) "de_CH"
}
[1]=>
NULL
}
Example: mandatory and disallowed value
$allowed = [new \n2n\l10n\N2nLocale('de_CH'), new \n2n\l10n\N2nLocale('mn')];
$result = Bind::attrs(['n2nLocale1' => 'fr_FR', 'n2nLocale2' => null])
->props(['n2nLocale1', 'n2nLocale2'], Mappers::n2nLocale(true, $allowed))
->toArray()
->exec();
// result will be invalid with error messages provided for all n2nLocale properties.
var_dump($result->isValid()); // false
var_dump($result->getErrorMap()->getChild('n2nLocale1')->isEmpty()); // false due to disallowed value
var_dump($result->getErrorMap()->getChild('n2nLocale2')->isEmpty()); // false because mandatory