Skip to content

Commit

Permalink
php 8.1 workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko committed Mar 15, 2024
1 parent 11a2e04 commit 76d4f34
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Checker/DatetimeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#[Singleton]
final class DatetimeChecker extends AbstractChecker
{
//COOKIE formal
private const LONGEST_STR_FORMAT_LEN = 32;
//PHP_MAX_INT 64 strlen
private const LONGEST_INT_FORMAT_LEN = 19;

public const MESSAGES = [
'future' => '[[Should be a date in the future.]]',
'past' => '[[Should be a date in the past.]]',
Expand Down Expand Up @@ -129,6 +134,17 @@ private function date(mixed $value): ?\DateTimeInterface
$value = '0';
}

// in php below 8.2 a huge brute numeric values triggers exception
if (\is_string($value)) {
$maxLen = self::LONGEST_STR_FORMAT_LEN;
if (is_numeric($value)) {
$maxLen = self::LONGEST_INT_FORMAT_LEN;
}
if (\strlen($value) > $maxLen) {
return null;
}
}

return new \DateTimeImmutable(\is_numeric($value) ? "@$value" : \trim($value));
} catch (\Throwable) {
//here's the fail;
Expand Down
51 changes: 51 additions & 0 deletions tests/src/Unit/Checkers/DatetimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,57 @@ public function validProvider(): iterable
yield [false, []];
yield [false, new \stdClass()];

yield 'ATOM format' => [
true,
'2005-08-15T15:52:01+00:00',
];
yield 'W3C format' => [
true,
'2005-08-15T15:52:01+00:00',
];
yield 'COOKIE format' => [
true,
'Monday, 15-Aug-2005 15:52:01 UTC',
];
yield 'RFC822 format' => [
true,
'Mon, 15 Aug 05 15:52:01 +0000',
];
yield 'RFC1036 format' => [
true,
'Mon, 15 Aug 05 15:52:01 +0000',
];
yield 'RFC850 format' => [
true,
'Monday, 15-Aug-05 15:52:01 UTC',
];
yield 'RFC1123 format' => [
true,
'Mon, 15 Aug 2005 15:52:01 +0000',
];
yield 'RFC7231 format' => [
true,
'Sat, 30 Apr 2016 17:52:13 GMT',
];
yield 'RFC2822 format' => [
true,
'Mon, 15 Aug 2005 15:52:01 +0000',
];
yield 'RFC3339_EXTENDED format' => [
true,
'2005-08-15T15:52:01.000+00:00',
];
yield 'RSS format' => [
true,
'Mon, 15 Aug 2005 15:52:01 +0000',
];
if (PHP_VERSION_ID >= 80200) {
yield 'ISO8601_EXPANDED format' => [
true,
'2005-08-15T15:52:01+0000',
];
}

yield 'invalid datetime string' => [
false,
'you shall not pass',
Expand Down

0 comments on commit 76d4f34

Please sign in to comment.