diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Bouncer.php (renamed from src/Validator.php) | 90 | ||||
| -rw-r--r-- | src/Validators.php | 68 | ||||
| -rw-r--r-- | src/Validators/EmailValidator.php | 37 | ||||
| -rw-r--r-- | src/Validators/LenValidator.php | 46 | ||||
| -rw-r--r-- | src/Validators/RequiredValidator.php | 37 | ||||
| -rw-r--r-- | src/Validators/Validator.php | 9 |
6 files changed, 189 insertions, 98 deletions
diff --git a/src/Validator.php b/src/Bouncer.php index 72ae8ee..9c16b52 100644 --- a/src/Validator.php +++ b/src/Bouncer.php @@ -2,10 +2,15 @@ declare(strict_types=1); -namespace Askonomm\Validator; +namespace Askonomm\Bouncer; + +use Askonomm\Bouncer\Validators\Validator; +use Askonomm\Bouncer\Validators\LenValidator; +use Askonomm\Bouncer\Validators\EmailValidator; +use Askonomm\Bouncer\Validators\RequiredValidator; /** - * The Validator takes in an array of fields, an array of + * The Bouncer takes in an array of fields, an array of * rules and optionally an array of validators. If no validators * are provided, default validators will be used instead, which are: * @@ -14,7 +19,7 @@ namespace Askonomm\Validator; * - `Validators::required()` * * The key of each item in the `$fields` array must correspond to the - * the key of each item in the `$rules` array, so that Validator + * the key of each item in the `$rules` array, so that Bouncer * would know how to connect the two to each other. * * The `$rules` must have a value that is a string where the rules @@ -28,15 +33,15 @@ namespace Askonomm\Validator; * that rule as `len:8`, which would indicate using a `len` validator and passing * a modifier with the value `8` to it. * - * Example usage of Validator: + * Example usage of Bouncer: * * ```php * $fields = ['email' => 'asko@bien.ee']; * $rules = ['email' => 'required|email']; - * $validator = new Validator($fields, $rules); + * $bouncer = new Bouncer($fields, $rules); * - * if ($validator->fails()) { - * return $validator->errors(); + * if ($bouncer->fails()) { + * return $bouncer->errors(); * } * ``` * @@ -53,19 +58,19 @@ namespace Askonomm\Validator; * // validate your $value here and return true if * // the validation succeeded, or false if there was * // an error, in which case the rule's error will be - * // added to the array of errors used by Validator. + * // added to the array of errors used by Bouncer. * } * ]; * - * // Add validator to Validator - * $validator = new Validator($fields, $rules, [ + * // Add validator to Bouncer + * $bouncer = new Bouncer($fields, $rules, [ * 'rule-name' => $validator * ]); * ``` * * If you want to also use the default validators, and add yours as an extra, * simply join the array of your validators with the array that you get from - * `$validator->defaultValidators()`, for example: + * `$bouncer->defaultValidators()`, for example: * * ```php * $validators = [ @@ -73,38 +78,64 @@ namespace Askonomm\Validator; * 'rule-name' => $validator, * ]); * - * $validator = new Validator($fields, $rules, $validators); + * $bouncer = new Bouncer($fields, $rules, $validators); + * ``` + * + * Additionally, you can register your own validators via the + * `$bouncer->registerValidator` function like this: + * + * ```php + * $bouncer->registerValidator('rule-name', $validator]); * ``` + * * @author Asko Nomm <asko@bien.ee> */ -class Validator +class Bouncer { private array $errors = []; + private array $validators = []; public function __construct( private array $fields, private array $rules, - array $validators = [], ) { - if (empty($validators)) { - $this->validators = $this->defaultValidators(); - } - + $this->registerDefaultValidators(); $this->validate(); } /** - * Returns the default, built-in validators. + * Registers the default, built-in validators. * * @return array */ - public function defaultValidators(): array + public function registerDefaultValidators(): void { - return [ - 'len' => Validators::len(), - 'email' => Validators::email(), - 'required' => Validators::required(), - ]; + $this->registerValidator('len', (new LenValidator)); + $this->registerValidator('email', (new EmailValidator)); + $this->registerValidator('required', (new RequiredValidator)); + } + + /** + * Registers a validator to a `$ruleName`. + * + * @param string $ruleName + * @param Validator $validator + * @return void + */ + public function registerValidator(string $ruleName, Validator $validator): void + { + $this->validators[$ruleName] = $validator; + } + + /** + * Removes a validator assigned to the `$ruleName`. + * + * @param string $ruleName + * @return void + */ + public function removeValidator(string $ruleName): void + { + unset($this->validators[$ruleName]); } /** @@ -123,13 +154,12 @@ class Validator if (str_contains($item, ':')) { [$name, $modifier] = explode(':', $item); - if (!$this->rules[$name]['validates']($value, $modifier)) { - $this->errors[] = $this->rules[$name]['error']($field, $modifier); + if (!$this->validators[$name]->validate($value, $modifier)) { + $this->errors[] = $this->validators[$name]->composeError($field, $modifier); } } else { - - if (!$this->rules[$item]['validates']($value)) { - $this->errors[] = $this->rules[$item]['error']($field); + if (!$this->validators[$item]->validate($value)) { + $this->errors[] = $this->validators[$item]->composeError($field); } } } diff --git a/src/Validators.php b/src/Validators.php deleted file mode 100644 index 0040657..0000000 --- a/src/Validators.php +++ /dev/null @@ -1,68 +0,0 @@ -<?php - -namespace Askonomm\Validator; - -/** - * Undocumented class - */ -class Validators -{ - /** - * Undocumented function - * - * @return array - */ - public static function len(): array - { - return [ - 'error' => function (string $field, mixed $modifier = 0): string { - return "{$field} is shorter than the required ${modifier} characters."; - }, - 'validates' => function (string $value, mixed $modifier = 0): bool { - if (!isset($value) || strlen($value) < (int) $modifier) { - return false; - } - - return true; - } - ]; - } - - /** - * Undocumented function - * - * @return array - */ - public static function email(): array - { - return [ - 'error' => function (string $field): string { - return "${field} is not a valid e-mail address."; - }, - 'validates' => function (string $value): bool { - if (!isset($value) || !filter_var($value, FILTER_VALIDATE_EMAIL)) { - return false; - } - - return true; - } - ]; - } - - /** - * Undocumented function - * - * @return array - */ - public static function required(): array - { - return [ - 'error' => function (string $field): string { - return "${field} is required."; - }, - 'validates' => function (string $value): bool { - return isset($value) && $value !== ''; - } - ]; - } -} 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 @@ +<?php + +namespace Askonomm\Bouncer\Validators; + +/** + * Implements an e-mail validator that has a job + * of validating e-mail addresses. + * + * @author Asko Nomm <asko@bien.ee> + */ +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 @@ +<?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."; + } +} 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 @@ +<?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 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 @@ +<?php + +namespace Askonomm\Bouncer\Validators; + +interface Validator +{ + public static function validate(string $value, mixed $modifier = null): bool; + public static function composeError(string $field, mixed $modifier = null): string; +} |
