DateMapper
Maps an input value to an n2n\util\calendar\Date instance (or null).
Accepts the following input types:
Date— used directly.string— parsed vianew Date($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 | Date? | null | Rejects dates earlier than this (inclusive). |
| $max | Date? | null | Rejects dates later than this (inclusive). |
Example: parsing a date string
$result = Bind::values('2023-10-01', null)->map(Mappers::date())->toArray()->exec();
var_dump($result->get());
Output:
array(2) {
[0]=>
object(n2n\util\calendar\Date)#1 (1) {
["date"]=>
string(10) "2023-10-01"
}
[1]=>
NULL
}
Example: mandatory and range validation
$result = Bind::attrs([
'd1' => null,
'd2' => new Date('2023-01-01'),
'd3' => new Date('2025-01-01'),
])
->props(['d1', 'd2', 'd3'], Mappers::date(true, new Date('2023-06-01'), new Date('2024-06-01')))
->toArray()
->exec();
// result will be invalid with error messages provided for all date properties.
var_dump($result->isValid()); // false
var_dump($result->getErrorMap()->getChild('d1')->isEmpty()); // false because mandatory
var_dump($result->getErrorMap()->getChild('d2')->isEmpty()); // false due to min
var_dump($result->getErrorMap()->getChild('d3')->isEmpty()); // false due to max