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/EmailValidator.php | 37 +++++++++++++++++++++++++++++ src/Validators/LenValidator.php | 46 ++++++++++++++++++++++++++++++++++++ src/Validators/RequiredValidator.php | 37 +++++++++++++++++++++++++++++ src/Validators/Validator.php | 9 +++++++ 4 files changed, 129 insertions(+) create mode 100644 src/Validators/EmailValidator.php create mode 100644 src/Validators/LenValidator.php create mode 100644 src/Validators/RequiredValidator.php create mode 100644 src/Validators/Validator.php (limited to 'src/Validators') diff --git a/src/Validators/EmailValidator.php b/src/Validators/EmailValidator.php new file mode 100644 index 0000000..5d69b5b --- /dev/null +++ b/src/Validators/EmailValidator.php @@ -0,0 +1,37 @@ + + */ +class EmailValidator implements Validator +{ + /** + * Returns a boolean `true` when given `$value` is a valid e-mail + * address. Returns `false` otherwise. + * + * @param string $value + * @param mixed $modifier + * @return boolean + */ + public static function validate(string $value, mixed $modifier = null): bool + { + return filter_var($value, FILTER_VALIDATE_EMAIL); + } + + /** + * 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 not a valid e-mail address."; + } +} 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."; + } +} diff --git a/src/Validators/RequiredValidator.php b/src/Validators/RequiredValidator.php new file mode 100644 index 0000000..4cf6bfc --- /dev/null +++ b/src/Validators/RequiredValidator.php @@ -0,0 +1,37 @@ + + */ +class RequiredValidator implements Validator +{ + /** + * Returns a boolean `true` when given `$value` is present + * and not empty. Returns `false` otherwise. + * + * @param string $value + * @param mixed $modifier + * @return boolean + */ + public static function validate(string $value, mixed $modifier = null): bool + { + return isset($value) && $value !== ''; + } + + /** + * 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 required."; + } +} diff --git a/src/Validators/Validator.php b/src/Validators/Validator.php new file mode 100644 index 0000000..11f2b01 --- /dev/null +++ b/src/Validators/Validator.php @@ -0,0 +1,9 @@ +