From 76d4f345d59dc155a423e0840f3e935424879b81 Mon Sep 17 00:00:00 2001 From: gam6itko Date: Fri, 15 Mar 2024 11:29:08 +0300 Subject: [PATCH] php 8.1 workaround --- src/Checker/DatetimeChecker.php | 16 ++++++++ tests/src/Unit/Checkers/DatetimeTest.php | 51 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/Checker/DatetimeChecker.php b/src/Checker/DatetimeChecker.php index d19cec8..d6b4516 100644 --- a/src/Checker/DatetimeChecker.php +++ b/src/Checker/DatetimeChecker.php @@ -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.]]', @@ -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; diff --git a/tests/src/Unit/Checkers/DatetimeTest.php b/tests/src/Unit/Checkers/DatetimeTest.php index dff19fe..a364e9c 100644 --- a/tests/src/Unit/Checkers/DatetimeTest.php +++ b/tests/src/Unit/Checkers/DatetimeTest.php @@ -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',