Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve date validation #33

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ The field under this rule must be one of `'on'`, `'yes'`, `'1'`, `'true'` (the s

The field under this rule must be a date after the given minimum.

The parameter should be any valid string that can be parsed by `strtotime`. For example:
The parameter should be any valid string that can be parsed by `strtotime` including unix timestamps. For example:

* after:next week
* after:2016-12-31
* after:2016
* after:2016-12-31 09:56:02
* after:1583946133

</details>

Expand Down
20 changes: 12 additions & 8 deletions src/Rules/Behaviours/CanValidateDates.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@

trait CanValidateDates
{
protected function assertDate(string $date): void
protected function assertDate(int|string $date): void
{
if (!$this->isValidDate($date)) {
if ($this->getTimeStamp($date) === null) {
throw ParameterException::invalidDate($date);
}
}

protected function isValidDate(string $date): bool
protected function getTimeStamp(int|string $date): ?int
{
return (strtotime($date) !== false);
}
if (is_int($date)) {
return $date;
}

protected function getTimeStamp($date): int
{
return strtotime($date);
if (is_string($date) && is_numeric($date)) {
return (int)$date;
}

$timestamp = strtotime($date);
return $timestamp !== false ? $timestamp : null;
}
}
6 changes: 4 additions & 2 deletions tests/Rules/AfterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static function getInvalidDates(): array
[$now->format("Y m d")],
[$now->format("Y m d h:i:s")],
["tommorow"], //typo
["lasst year"] //typo
["lasst year"], //typo
["0123"] //invalid unix
];
}

Expand All @@ -72,7 +73,8 @@ public static function getValidDates(): array
[$now->format("Y-m-d h:i:s")],
["now"],
["tomorrow"],
["2 years ago"]
["2 years ago"],
["1741716133"]
];
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Rules/BeforeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public static function getValidDates(): array
[$now->format("Y-m-d h:i:s")],
["now"],
["tomorrow"],
["2 years ago"]
["2 years ago"],
["1741716133"]
];
}

Expand All @@ -64,7 +65,8 @@ public static function getInvalidDates(): array
[$now->format("Y m d")],
[$now->format("Y m d h:i:s")],
["tommorow"], //typo
["lasst year"] //typo
["lasst year"], //typo
["0123"] //invalid unix
];
}

Expand Down
Loading