Skip to content

Commit

Permalink
Validators::isInRange() works with DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 26, 2017
1 parent f39a0c5 commit 266160a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Utils/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ public static function isInRange($value, $range)
$limit = isset($range[0]) ? $range[0] : $range[1];
if (is_string($limit)) {
$value = (string) $value;
} elseif ($limit instanceof \DateTimeInterface) {
if (!$value instanceof \DateTimeInterface) {
return FALSE;
}
} elseif (is_numeric($value)) {
$value *= 1;
} else {
Expand Down
5 changes: 5 additions & 0 deletions tests/Utils/Validators.isInRange().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ Assert::false(Validators::isInRange('a', [NULL, 9]));
Assert::true(Validators::isInRange('1', [NULL, 9]));
Assert::false(Validators::isInRange(10, ['a', NULL]));
Assert::true(Validators::isInRange(10, [NULL, 'a']));

Assert::false(Validators::isInRange(new DateTimeImmutable('2017-04-26'), [new DateTime('2017-04-20'), new DateTime('2017-04-23')]));
Assert::true(Validators::isInRange(new DateTimeImmutable('2017-04-26'), [new DateTime('2017-04-20'), new DateTime('2017-04-30')]));
Assert::false(Validators::isInRange(new DateTimeImmutable('2017-04-26'), [10, NULL]));
Assert::false(Validators::isInRange(new DateTimeImmutable('2017-04-26'), [NULL, 10]));

0 comments on commit 266160a

Please sign in to comment.