diff options
| author | Asko Nomm <asko@bien.ee> | 2022-02-22 21:32:37 +0100 |
|---|---|---|
| committer | Asko Nomm <asko@bien.ee> | 2022-02-22 21:32:37 +0100 |
| commit | b0fd14cd71d60c1b9926aff10fe9e8eeebe1285c (patch) | |
| tree | ff5f1689ffa87a84d8c7487d8a64c885ce7b592e /src/Validators/LenValidator.php | |
| parent | 4256c45b6b9c96400b8f372b289be1127495ac56 (diff) | |
Lots of documentation and basic tests
Diffstat (limited to 'src/Validators/LenValidator.php')
| -rw-r--r-- | src/Validators/LenValidator.php | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/Validators/LenValidator.php b/src/Validators/LenValidator.php new file mode 100644 index 0000000..b4b5e2b --- /dev/null +++ b/src/Validators/LenValidator.php @@ -0,0 +1,46 @@ +<?php + +namespace Askonomm\Bouncer\Validators; + +/** + * Implements a length validator that has a job of validating + * that a given string is of correct length. + * + * @author Asko Nomm <asko@bien.ee> + */ +class LenValidator implements Validator +{ + /** + * Returns a boolean `true` when given `$value` is as long as + * required. Returns `false` otherwise. + * + * @param string $value + * @param mixed $modifier + * @return boolean + */ + public static function validate(string $value, mixed $modifier = null): bool + { + // If no modifier present then this validator will always validate. + if (!$modifier) { + return true; + } + + if (!isset($value) || strlen($value) < (int) $modifier) { + return false; + } + + 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} is shorter than the required ${modifier} characters."; + } +} |
