diff --git a/src/Validators/DateTimeDiff.php b/src/Validators/DateTimeDiff.php index 74b52d1fc..c429e3f81 100644 --- a/src/Validators/DateTimeDiff.php +++ b/src/Validators/DateTimeDiff.php @@ -139,6 +139,10 @@ private function createDateTimeObject(mixed $input): DateTimeInterface|null } } + if (!$this->isDateTime($this->format, (string) $input)) { + return null; + } + $format = $this->getExceptionalFormats()[$this->format] ?? $this->format; $dateTime = DateTimeImmutable::createFromFormat($format, (string) $input); if ($dateTime === false) { diff --git a/tests/unit/Validators/DateTimeDiffTest.php b/tests/unit/Validators/DateTimeDiffTest.php index 5ee71bc29..43f214828 100644 --- a/tests/unit/Validators/DateTimeDiffTest.php +++ b/tests/unit/Validators/DateTimeDiffTest.php @@ -43,6 +43,11 @@ public static function providerForValidInput(): array 'years + DateTime' => [new DateTimeDiff('years', Stub::pass(1)), new DateTimeImmutable()], 'years + string' => [new DateTimeDiff('years', Stub::pass(1)), '2020-01-01'], 'years + string + format' => [new DateTimeDiff('years', Stub::pass(1), 'Y-m-d'), '2020-01-01'], + 'years + leap day + format' => [new DateTimeDiff('years', Stub::pass(1), 'Y-m-d'), '2024-02-29'], + 'years + string + exceptional format' => [ + new DateTimeDiff('years', Stub::pass(1), 'c'), + '2020-01-01T00:00:00+00:00', + ], 'years + string + format + now' => [ new DateTimeDiff('years', Stub::pass(1), 'Y-m-d', new DateTimeImmutable()), '2020-01-01', @@ -80,6 +85,14 @@ public static function providerForInvalidInput(): array new DateTimeDiff('years', Stub::fail(1), 'Y-m-d'), 'invalid date', ], + 'impossible date, with passing rule' => [ + new DateTimeDiff('years', Stub::pass(1), 'Y-m-d'), + '2023-02-30', + ], + 'impossible date in exceptional format, with passing rule' => [ + new DateTimeDiff('years', Stub::pass(1), 'c'), + '2023-02-30T00:00:00+00:00', + ], ] + array_map( static fn(array $args): array => [new DateTimeDiff('years', Stub::fail(1)), new DateTimeImmutable()], iterator_to_array(self::providerForNonScalarValues()),