TimeMapper
Maps an input value to an n2n\util\calendar\Time instance (or null).
Accepts the following input types:
Time— used directly.string— parsed vianew Time($str). An unparseable string produces anInvalidvalidation error.null— left asnull, subject to validation.
Validation possibilities
| Parameter | Type | Default | Description |
|---|---|---|---|
| $mandatory | bool | false | Rejects null input. |
| $min | Time? | null | Rejects times earlier than this (inclusive). |
| $max | Time? | null | Rejects times later than this (inclusive). |
Example: parsing a time string
$result = Bind::values('11:59', null)->map(Mappers::time())->toArray()->exec();
var_dump($result->get());
Output:
array(2) {
[0]=>
object(n2n\util\calendar\Time)#1 (0) {
}
[1]=>
NULL
}
Example: mandatory and range validation
$result = Bind::attrs([
't1' => null,
't2' => new Time('06:00:00'),
't3' => new Time('23:00:00'),
])
->props(['t1', 't2', 't3'], Mappers::time(true, new Time('08:00:00'), new Time('22:45:00')))
->toArray()
->exec();
// result will be invalid with error messages provided for all time properties.
var_dump($result->isValid()); // false
var_dump($result->getErrorMap()->getChild('t1')->isEmpty()); // false because mandatory
var_dump($result->getErrorMap()->getChild('t2')->isEmpty()); // false due to min
var_dump($result->getErrorMap()->getChild('t3')->isEmpty()); // false due to max