EmailMapper
Maps an input value to a normalized email string (or null).
Accepts the following input types:
string.- Any
Stringableobject. null— left asnull, subject to validation.
Non-null values are normalized before validation: StringUtils::clean() trims, collapses whitespace and strips non-printable characters, then mb_strtolower() lowercases the result.
Validation possibilities
| Parameter | Type | Default | Description |
|---|---|---|---|
| $mandatory | bool | false | Rejects null input. |
Email format validation is always applied, regardless of $mandatory.
Example: basic normalization
$result = Bind::values(' Test@Testerich.ch ', null)->map(Mappers::email())->toArray()->exec();
var_dump($result->get());
Output:
array(2) {
[0]=>
string(17) "test@testerich.ch"
[1]=>
NULL
}
Example: mandatory and invalid format
$result = Bind::attrs(['email1' => 'asdf@', 'email2' => 'no-at-sign', 'email3' => null])
->props(['email1', 'email2', 'email3'], Mappers::email(true))
->toArray()
->exec();
// result will be invalid with error messages provided for all email properties.
var_dump($result->isValid()); // false
var_dump($result->getErrorMap()->getChild('email1')->isEmpty()); // false due to invalid format
var_dump($result->getErrorMap()->getChild('email2')->isEmpty()); // false due to invalid format
var_dump($result->getErrorMap()->getChild('email3')->isEmpty()); // false because mandatory