summaryrefslogtreecommitdiff
path: root/src/Validators.php
diff options
context:
space:
mode:
authorAsko Nomm <asko@bien.ee>2022-02-22 18:32:14 +0100
committerAsko Nomm <asko@bien.ee>2022-02-22 18:32:14 +0100
commit4256c45b6b9c96400b8f372b289be1127495ac56 (patch)
treebe9b796b0fe293b7e63d79af9b616707fa9d0286 /src/Validators.php
parentfdb63236736bc69f07a4fd19e80ab03d44ecc945 (diff)
Swap rules and validators, makes more sense now
Diffstat (limited to 'src/Validators.php')
-rw-r--r--src/Validators.php68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/Validators.php b/src/Validators.php
new file mode 100644
index 0000000..0040657
--- /dev/null
+++ b/src/Validators.php
@@ -0,0 +1,68 @@
+<?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 !== '';
+ }
+ ];
+ }
+}