ColorHexMapper
Maps an input value to a normalized hex color 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 surrounding whitespace, then mb_strtolower() lowercases the result (so #AA BBCC style input becomes #aabbcc).
Validation possibilities
| Parameter | Type | Default | Description |
|---|---|---|---|
| $mandatory | bool | false | Rejects null input. |
Hex color format validation is always applied, regardless of $mandatory.
Example: basic normalization
$result = Bind::values(' #aaBBcc ', null)->map(Mappers::colorHex())->toArray()->exec();
var_dump($result->get());
Output:
array(2) {
[0]=>
string(7) "#aabbcc"
[1]=>
NULL
}
Example: mandatory and invalid format
$result = Bind::attrs(['color1' => 'asdf', 'color2' => '#GG0000', 'color3' => null])
->props(['color1', 'color2', 'color3'], Mappers::colorHex(true))
->toArray()
->exec();
// result will be invalid with error messages provided for all color properties.
var_dump($result->isValid()); // false
var_dump($result->getErrorMap()->getChild('color1')->isEmpty()); // false due to invalid format
var_dump($result->getErrorMap()->getChild('color2')->isEmpty()); // false due to invalid format
var_dump($result->getErrorMap()->getChild('color3')->isEmpty()); // false because mandatory