diff options
| -rw-r--r-- | README.md | 12 | ||||
| -rw-r--r-- | src/Hird.php | 2 | ||||
| -rw-r--r-- | src/Validators/DateFormatValidator.php | 45 | ||||
| -rw-r--r-- | tests/HirdTest.php | 19 |
4 files changed, 78 insertions, 0 deletions
@@ -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.', + ]); +}); |
