From 323b2813796b54817d6866aa1a28d9b996af168b Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Wed, 18 Oct 2023 11:51:24 +0530 Subject: [PATCH 1/4] adds past date validation --- composer.lock | 12 ++++++------ src/Database/Validator/Datetime.php | 18 ++++++++++++++++-- tests/Database/Validator/DateTimeTest.php | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/composer.lock b/composer.lock index d476dd777..8a37a9e84 100644 --- a/composer.lock +++ b/composer.lock @@ -839,16 +839,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.38", + "version": "1.10.39", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "5302bb402c57f00fb3c2c015bac86e0827e4b691" + "reference": "d9dedb0413f678b4d03cbc2279a48f91592c97c4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/5302bb402c57f00fb3c2c015bac86e0827e4b691", - "reference": "5302bb402c57f00fb3c2c015bac86e0827e4b691", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/d9dedb0413f678b4d03cbc2279a48f91592c97c4", + "reference": "d9dedb0413f678b4d03cbc2279a48f91592c97c4", "shasum": "" }, "require": { @@ -897,7 +897,7 @@ "type": "tidelift" } ], - "time": "2023-10-06T14:19:14+00:00" + "time": "2023-10-17T15:46:26+00:00" }, { "name": "phpunit/php-code-coverage", @@ -2608,5 +2608,5 @@ "php": ">=8.0" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.6.0" } diff --git a/src/Database/Validator/Datetime.php b/src/Database/Validator/Datetime.php index 702ce50b4..28e416bdb 100644 --- a/src/Database/Validator/Datetime.php +++ b/src/Database/Validator/Datetime.php @@ -11,8 +11,16 @@ class Datetime extends Validator */ protected string $message = 'Date is not valid'; - public function __construct() + /** + * @var bool + */ + protected bool $allowOldDates = true; + + public function __construct(?bool $allowOldDates = null) { + if (!is_null($allowOldDates)) { + $this->allowOldDates = $allowOldDates; + } } /** @@ -37,7 +45,13 @@ public function isValid($value): bool } try { - new \DateTime($value); + $date = new \DateTime($value); + $now = new \DateTime(); + + if ($this->allowOldDates === false && $date < $now) { + $this->message = 'Date is in the past'; + return false; + } } catch(\Exception $e) { $this->message = $e->getMessage(); return false; diff --git a/tests/Database/Validator/DateTimeTest.php b/tests/Database/Validator/DateTimeTest.php index c9b99bc1c..36c569d52 100644 --- a/tests/Database/Validator/DateTimeTest.php +++ b/tests/Database/Validator/DateTimeTest.php @@ -51,4 +51,18 @@ public function testCreateDatetime(): void */ $this->assertEquals(false, $dateValidator->isValid("2022-13-04 11:31:52.680")); } + + public function testPastDateValidation(): void + { + $dateValidator = new DatetimeValidator(allowOldDates: false); + + $this->assertEquals(false, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), -3))); + $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), 5))); + + + $dateValidator = new DatetimeValidator(allowOldDates: true); + + $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), -3))); + $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), 5))); + } } From 4425b7763b2f6017d230771a944caa8571bf710c Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Wed, 18 Oct 2023 12:04:37 +0530 Subject: [PATCH 2/4] review changes --- src/Database/Validator/Datetime.php | 12 ++++++------ tests/Database/Validator/DateTimeTest.php | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Database/Validator/Datetime.php b/src/Database/Validator/Datetime.php index 28e416bdb..8874c6ca0 100644 --- a/src/Database/Validator/Datetime.php +++ b/src/Database/Validator/Datetime.php @@ -14,12 +14,12 @@ class Datetime extends Validator /** * @var bool */ - protected bool $allowOldDates = true; + protected bool $requireDateInFuture = false; - public function __construct(?bool $allowOldDates = null) + public function __construct(?bool $requireDateInFuture = null) { - if (!is_null($allowOldDates)) { - $this->allowOldDates = $allowOldDates; + if (!is_null($requireDateInFuture)) { + $this->requireDateInFuture = $requireDateInFuture; } } @@ -48,8 +48,8 @@ public function isValid($value): bool $date = new \DateTime($value); $now = new \DateTime(); - if ($this->allowOldDates === false && $date < $now) { - $this->message = 'Date is in the past'; + if ($this->requireDateInFuture === false && $date < $now) { + $this->message = 'Date must be in the future'; return false; } } catch(\Exception $e) { diff --git a/tests/Database/Validator/DateTimeTest.php b/tests/Database/Validator/DateTimeTest.php index 36c569d52..b3919ae7b 100644 --- a/tests/Database/Validator/DateTimeTest.php +++ b/tests/Database/Validator/DateTimeTest.php @@ -54,13 +54,13 @@ public function testCreateDatetime(): void public function testPastDateValidation(): void { - $dateValidator = new DatetimeValidator(allowOldDates: false); + $dateValidator = new DatetimeValidator(requireDateInFuture: false); $this->assertEquals(false, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), -3))); $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), 5))); - $dateValidator = new DatetimeValidator(allowOldDates: true); + $dateValidator = new DatetimeValidator(requireDateInFuture: true); $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), -3))); $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), 5))); From 14bb474361d04266521d01445b555fcbed3f6419 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Wed, 18 Oct 2023 12:08:41 +0530 Subject: [PATCH 3/4] review changes --- src/Database/Validator/Datetime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Validator/Datetime.php b/src/Database/Validator/Datetime.php index 8874c6ca0..69b3ad010 100644 --- a/src/Database/Validator/Datetime.php +++ b/src/Database/Validator/Datetime.php @@ -48,7 +48,7 @@ public function isValid($value): bool $date = new \DateTime($value); $now = new \DateTime(); - if ($this->requireDateInFuture === false && $date < $now) { + if ($this->requireDateInFuture === true && $date < $now) { $this->message = 'Date must be in the future'; return false; } From 0801c6c8ed7bcf92b78b4db9b83efa405d5eb7e5 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Wed, 18 Oct 2023 12:14:49 +0530 Subject: [PATCH 4/4] review changes --- tests/Database/Validator/DateTimeTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Database/Validator/DateTimeTest.php b/tests/Database/Validator/DateTimeTest.php index b3919ae7b..7b239aecd 100644 --- a/tests/Database/Validator/DateTimeTest.php +++ b/tests/Database/Validator/DateTimeTest.php @@ -54,13 +54,13 @@ public function testCreateDatetime(): void public function testPastDateValidation(): void { - $dateValidator = new DatetimeValidator(requireDateInFuture: false); + $dateValidator = new DatetimeValidator(requireDateInFuture: true); $this->assertEquals(false, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), -3))); $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), 5))); - $dateValidator = new DatetimeValidator(requireDateInFuture: true); + $dateValidator = new DatetimeValidator(requireDateInFuture: false); $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), -3))); $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), 5)));