From cba497b4078b01ab4a063d97bfa1feb09d7e5cde Mon Sep 17 00:00:00 2001 From: codisart Date: Sun, 19 Feb 2023 18:33:49 +0100 Subject: [PATCH] chore: Convert some parts of Date validator to php8 syntax and add some tests to check userland retrocompatibility. Signed-off-by: codisart --- src/Date.php | 26 +++++++++-------- test/DateTest.php | 17 +++++++++++ test/TestAsset/CustomDate.php | 53 +++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 test/TestAsset/CustomDate.php diff --git a/src/Date.php b/src/Date.php index a74c12216..b00ef676a 100644 --- a/src/Date.php +++ b/src/Date.php @@ -67,6 +67,7 @@ public function __construct($options = []) $options = iterator_to_array($options); } elseif (! is_array($options)) { $options = func_get_args(); + $temp = []; $temp['format'] = array_shift($options); $options = $temp; } @@ -152,22 +153,23 @@ protected function convertToDateTime($param, $addErrors = true) return DateTime::createFromImmutable($param); } - $type = gettype($param); - switch ($type) { - case 'string': - return $this->convertString($param, $addErrors); - case 'integer': - return $this->convertInteger($param); - case 'double': - return $this->convertDouble($param); - case 'array': - return $this->convertArray($param, $addErrors); - } + return match (gettype($param)) { + 'string' => $this->convertString($param, $addErrors), + 'integer' => $this->convertInteger($param), + 'double' => $this->convertDouble($param), + 'array' => $this->convertArray($param), + default => $this->addInvalidTypeError($addErrors), + }; + } + /** + * @return false + */ + private function addInvalidTypeError(bool $addErrors) + { if ($addErrors) { $this->error(self::INVALID); } - return false; } diff --git a/test/DateTest.php b/test/DateTest.php index 8a1f49840..ab58362d2 100644 --- a/test/DateTest.php +++ b/test/DateTest.php @@ -9,6 +9,7 @@ use Laminas\Validator\Date; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; +use LaminasTest\Validator\TestAsset\CustomDate; use PHPUnit\Framework\TestCase; use stdClass; @@ -195,4 +196,20 @@ public function testConstructorWithFormatParameter(): void self::assertSame($format, $validator->getFormat()); } + + public function testAddErrorsParam(): void + { + $customDateValidator = new Date(); + self::assertFalse($customDateValidator->isValid(new stdClass())); + self::assertArrayHasKey('dateInvalid', $customDateValidator->getMessages()); + } + + public function testExtensionDateValidator(): void + { + $customDateValidator = new CustomDate(); + self::assertTrue($customDateValidator->isValid(16757802.07)); + + self::assertFalse($customDateValidator->isValid(new stdClass())); + self::assertArrayNotHasKey('dateInvalid', $customDateValidator->getMessages()); + } } diff --git a/test/TestAsset/CustomDate.php b/test/TestAsset/CustomDate.php new file mode 100644 index 000000000..d04bfa8d7 --- /dev/null +++ b/test/TestAsset/CustomDate.php @@ -0,0 +1,53 @@ +