|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PHPAether\Utils\Forms; |
| 6 | + |
| 7 | +class FormValidator |
| 8 | +{ |
| 9 | + |
| 10 | + protected array $field; |
| 11 | + protected array $validators = []; |
| 12 | + protected array $errors = []; |
| 13 | + |
| 14 | + public function __construct( |
| 15 | + protected array $form |
| 16 | + ) { |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + public function validate(?callable $validatorCallbackFn=null): array |
| 21 | + { |
| 22 | + |
| 23 | + if ($validatorCallbackFn !== null) { |
| 24 | + $validatorCallbackFn($this); |
| 25 | + } |
| 26 | + |
| 27 | + foreach ($this->validators as $field => $fieldValidators) { |
| 28 | + foreach ($fieldValidators as $validator) { |
| 29 | + [$validatorFn, $errorMsg, $alias] = $validator; |
| 30 | + |
| 31 | + $value = $this->form[$field]; |
| 32 | + if (! $validatorFn($value)) { |
| 33 | + $errorField = $alias ?? $field; |
| 34 | + $this->errors[$field] = is_string($errorMsg) ? $errorMsg : (is_callable($errorMsg) ? $errorMsg($value, $errorField) : "Invalid {$errorField}"); |
| 35 | + |
| 36 | + continue 2; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return $this->errors; |
| 42 | + } |
| 43 | + |
| 44 | + public function for(string $field, ?string $alias=null): self |
| 45 | + { |
| 46 | + if (! isset($this->form, $field)) { |
| 47 | + throw new \InvalidArgumentException("The field: $field provided is not in the form"); |
| 48 | + } |
| 49 | + |
| 50 | + $this->field = [$field, $alias]; |
| 51 | + return $this; |
| 52 | + } |
| 53 | + |
| 54 | + protected function validateFor( |
| 55 | + callable $validatorFn, |
| 56 | + string|callable|null $errorMsg=null |
| 57 | + ): self |
| 58 | + { |
| 59 | + if (! isset($this->field)) { |
| 60 | + throw new \FormValidatorException("No field is selected for validation. Use the `for` method to select a field"); |
| 61 | + } |
| 62 | + |
| 63 | + // Register the validator callback function |
| 64 | + [$field, $alias] = $this->field; |
| 65 | + $this->validators[$field] ??= []; |
| 66 | + $this->validators[$field][] = [$validatorFn, $errorMsg, $alias]; |
| 67 | + |
| 68 | + return $this; |
| 69 | + } |
| 70 | + |
| 71 | + public function email( |
| 72 | + string|callable|null $errorMsg=null |
| 73 | + ): self |
| 74 | + { |
| 75 | + return $this->validateFor( |
| 76 | + fn (string $value): bool => false, // validate for email here |
| 77 | + $errorMsg |
| 78 | + ); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + public function text( |
| 83 | + ?int $min=null, |
| 84 | + ?int $max=null, |
| 85 | + string|callable|null $errorMsg=null |
| 86 | + ): self |
| 87 | + { |
| 88 | + return $this->validateFor( |
| 89 | + fn (string $value): bool => true, // |
| 90 | + $errorMsg |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + public function tel( |
| 95 | + string|callable|null $errorMsg=null |
| 96 | + ): self |
| 97 | + { |
| 98 | + return $this->validateFor( |
| 99 | + fn (string $value): bool => ( |
| 100 | + is_numeric($value) |
| 101 | + ), |
| 102 | + $errorMsg |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + public function url( |
| 107 | + string|callable|null $errorMsg=null |
| 108 | + ): self |
| 109 | + { |
| 110 | + return $this->validateFor( |
| 111 | + fn (string $value): bool => false, |
| 112 | + $errorMsg |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + public function regex( |
| 117 | + string $pattern, |
| 118 | + string|callable|null $errorMsg=null |
| 119 | + ): self |
| 120 | + { |
| 121 | + return $this->validateFor( |
| 122 | + fn (string $value): bool => (bool) preg_match($pattern, $value), |
| 123 | + $errorMsg |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + public function password( |
| 128 | + ?int $min=8, |
| 129 | + ?int $max=15, |
| 130 | + ?int $options=null, |
| 131 | + string|callable|null $errorMsg=null |
| 132 | + ): self |
| 133 | + { |
| 134 | + return $this->validateFor( |
| 135 | + fn (string $value): bool => false, |
| 136 | + $errorMsg |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + public function matches( |
| 141 | + string $value, |
| 142 | + ?string $regex=null, |
| 143 | + string|callable|null $errorMsg=null |
| 144 | + ): self |
| 145 | + { |
| 146 | + return $this->validateFor( |
| 147 | + fn (string $value): bool => false, |
| 148 | + $errorMsg |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments