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..69b3ad010 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 $requireDateInFuture = false; + + public function __construct(?bool $requireDateInFuture = null) { + if (!is_null($requireDateInFuture)) { + $this->requireDateInFuture = $requireDateInFuture; + } } /** @@ -37,7 +45,13 @@ public function isValid($value): bool } try { - new \DateTime($value); + $date = new \DateTime($value); + $now = new \DateTime(); + + if ($this->requireDateInFuture === true && $date < $now) { + $this->message = 'Date must be in the future'; + 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..7b239aecd 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(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: false); + + $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), -3))); + $this->assertEquals(true, $dateValidator->isValid(DateTime::addSeconds(new \DateTime(), 5))); + } }