From b0fd14cd71d60c1b9926aff10fe9e8eeebe1285c Mon Sep 17 00:00:00 2001 From: Asko Nomm Date: Tue, 22 Feb 2022 21:32:37 +0100 Subject: Lots of documentation and basic tests --- src/Validators/LenValidator.php | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Validators/LenValidator.php (limited to 'src/Validators/LenValidator.php') 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 @@ + + */ +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."; + } +} -- cgit v1.2.3