summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsko Nomm <asko@bien.ee>2022-03-06 22:48:02 +0100
committerAsko Nomm <asko@bien.ee>2022-03-06 22:48:02 +0100
commitf313168d4b53b32b1ee3ce3e2e9ed749848f6985 (patch)
treedc55fed561e6d09ae8a9cd41e4f313d969abd36f
parent49a2e23470c291004f2fb203401408225fb61831 (diff)
(MINOR) Implement DateFormatValidator
-rw-r--r--README.md12
-rw-r--r--src/Hird.php2
-rw-r--r--src/Validators/DateFormatValidator.php45
-rw-r--r--tests/HirdTest.php19
4 files changed, 78 insertions, 0 deletions
diff --git a/README.md b/README.md
index 0be14fa..d60b44b 100644
--- a/README.md
+++ b/README.md
@@ -88,6 +88,18 @@ $rules = ['password' => 'required'];
$hird = new Hird($fields, $rules);
```
+### 'date-format`
+
+The `date-format` validator validates the string format of a date, and is registered as the `date-format` rule. It will pass validation if the value is set and the value is in the format specified by the rule.
+
+```php
+use Askonomm\Hird\Hird;
+
+$fields = ['date' => '2020-09-17'];
+$rules = ['date' => 'date-format:Y-m-d'];
+$hird = new Hird($fields, $rules);
+```
+
## Creating validators
You can also create your own validators, or replace existing ones if you're not happy with them.
diff --git a/src/Hird.php b/src/Hird.php
index 5ff3b31..933afe0 100644
--- a/src/Hird.php
+++ b/src/Hird.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Askonomm\Hird;
+use Askonomm\Hird\Validators\DateFormatValidator;
use Askonomm\Hird\Validators\Validator;
use Askonomm\Hird\Validators\LenValidator;
use Askonomm\Hird\Validators\EmailValidator;
@@ -64,6 +65,7 @@ class Hird
$this->registerValidator('len', (new LenValidator));
$this->registerValidator('email', (new EmailValidator));
$this->registerValidator('required', (new RequiredValidator));
+ $this->registerValidator('date-format', (new DateFormatValidator));
}
/**
diff --git a/src/Validators/DateFormatValidator.php b/src/Validators/DateFormatValidator.php
new file mode 100644
index 0000000..fedbb7d
--- /dev/null
+++ b/src/Validators/DateFormatValidator.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace Askonomm\Hird\Validators;
+
+/**
+ * Implements a DateFormat validator that has a job
+ * of validating that a given date is in the correct
+ * format.
+ *
+ * @author Asko Nomm <asko@bien.ee>
+ */
+class DateFormatValidator implements Validator
+{
+ /**
+ * Returns a boolean `true` when given `$value` is a valid e-mail
+ * address. Returns `false` otherwise.
+ *
+ * @param string $field
+ * @param mixed $value
+ * @param mixed $modifier
+ * @return boolean
+ */
+ public static function validate(string $field, mixed $value, mixed $modifier = null): bool
+ {
+ if ($value) {
+ $datetime = \DateTime::createFromFormat($modifier, $value);
+
+ return $datetime !== false && !array_sum($datetime::getLastErrors());
+ }
+
+ return true;
+ }
+
+ /**
+ * Composes the error message in case the validation fails.
+ *
+ * @param string $field
+ * @param mixed $modifier
+ * @return string
+ */
+ public static function composeError(string $field, mixed $modifier = null): string
+ {
+ return "${field} does not match the required date format ${modifier}.";
+ }
+}
diff --git a/tests/HirdTest.php b/tests/HirdTest.php
index ef217a1..9b00506 100644
--- a/tests/HirdTest.php
+++ b/tests/HirdTest.php
@@ -69,3 +69,22 @@ test('Validate an incorrect required string', function () {
'null-value is required.',
]);
});
+
+test('Validate a correct date format', function() {
+ $fields = ['date' => '2020-09-17 15:00:12'];
+ $rules = ['date' => 'date-format:Y-m-d H:i:s'];
+ $hird = new Hird($fields, $rules);
+
+ expect($hird->fails())->tobeFalse();
+});
+
+test('Validate an incorrect correct date format', function() {
+ $fields = ['date' => '2020-09-17 15:00'];
+ $rules = ['date' => 'date-format:Y-m-d H:i:s'];
+ $hird = new Hird($fields, $rules);
+ $hird->fails();
+
+ expect($hird->errors())->toBe([
+ 'date does not match the required date format Y-m-d H:i:s.',
+ ]);
+});