diff options
| author | Asko Nomm <asko@bien.ee> | 2022-02-22 17:54:18 +0100 |
|---|---|---|
| committer | Asko Nomm <asko@bien.ee> | 2022-02-22 17:54:18 +0100 |
| commit | 2bb257006e8f2dd792c46c868b3bf80c5a25a64b (patch) | |
| tree | 094fcb8c4be50623fdafe33ced279f6706732e7a /src/ValidatorRules.php | |
Initial commit
Diffstat (limited to 'src/ValidatorRules.php')
| -rw-r--r-- | src/ValidatorRules.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/ValidatorRules.php b/src/ValidatorRules.php new file mode 100644 index 0000000..d3599e0 --- /dev/null +++ b/src/ValidatorRules.php @@ -0,0 +1,50 @@ +<?php + +namespace Askonomm\Validator; + +class ValidatorRules +{ + 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; + } + ]; + } + + 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; + } + ]; + } + + public static function required(): array + { + return [ + 'error' => function (string $field): string { + return "${field} is required."; + }, + 'validates' => function (string $value): bool { + return isset($value) && $value !== ''; + } + ]; + } +} |
