add isDateCorrect() assert
I think this assertion will be very useful
What's the real life use case for this? What is then supposed to be done with the value (which is now known to be a "correct date") and the knowledge about it being a "correct date"?
What's the real life use case for this? What is then supposed to be done with the value (which is now known to be a "correct date") and the knowledge about it being a "correct date"?
I use this check in api, at the very beginning, when receiving arguments. This allows you to immediately throw a validation error.
This allows you to immediately throw a validation error.
I'm not sure assertion should be used instead of validation, but that's another discussion.
And what exactly do you do that value (later in your code)?
And what exactly do you do that value (later in your code)?
I use it in sql queries. If the date is initially incorrect, all further actions do not make sense
You should really use DateTime instances and convert them to strings yourself, for example like in https://github.com/doctrine/dbal/blob/3622d6d5823ccaba5d768c311aed27383655041f/lib/Doctrine/DBAL/Types/DateType.php#L30-L44
So this is verifying if a specific value (2021-02-28) is on the format (Y-m-d).
I dont see any tests and Im hesitant about the use case. What you really should do is:
$date = \DateTimeImmutable::createFromFormat('Y-m-d', $rawDateInput);
if ($date === false) {
throw new ValidationError('Wrong date');
}
$mySercice->foo($date);
Ie, as soon as you can, use a DateTimeImmutable, because you know that is safe compared to $rawDateInput.