Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Validators/DateTimeDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Validators/DateTimeDiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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()),
Expand Down
Loading