| Total Complexity | 42 |
| Total Lines | 226 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Validator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Validator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Validator |
||
| 30 | { |
||
| 31 | |||
| 32 | use General; |
||
| 33 | use Type; |
||
| 34 | use File; |
||
| 35 | use Lists; |
||
| 36 | use Length; |
||
| 37 | use Resource; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Rules |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $rules = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Validation Errors |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $errors = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Request data |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $data = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Custom validations |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private $customValidations = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Add a rules for given field |
||
| 65 | * @param string $field |
||
| 66 | * @param array $rules |
||
| 67 | */ |
||
| 68 | public function addRule(string $field, array $rules) |
||
| 69 | { |
||
| 70 | if (!empty($field)) { |
||
| 71 | foreach ($rules as $rule) { |
||
| 72 | if (!isset($this->rules[$field])) { |
||
| 73 | $this->rules[$field] = []; |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->rules[$field][array_keys($rule)[0]] = array_values($rule)[0]; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Adds rules for multiple fields |
||
| 83 | * @param array $rules |
||
| 84 | */ |
||
| 85 | public function addRules(array $rules) |
||
| 86 | { |
||
| 87 | foreach ($rules as $field => $params) { |
||
| 88 | $this->addRule($field, $params); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Updates the single rule in rules list for given field |
||
| 94 | * @param string $field |
||
| 95 | * @param array $rule |
||
| 96 | */ |
||
| 97 | public function updateRule(string $field, array $rule) |
||
| 98 | { |
||
| 99 | if (!empty($field)) { |
||
| 100 | if (isset($this->rules[$field][array_keys($rule)[0]])) { |
||
| 101 | $this->rules[$field][array_keys($rule)[0]] = array_values($rule)[0]; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Deletes the the rule in rules list for given field |
||
| 108 | * @param string $field |
||
| 109 | * @param string|null $rule |
||
| 110 | */ |
||
| 111 | public function deleteRule(string $field, string $rule = null) |
||
| 112 | { |
||
| 113 | if (!empty($field)) { |
||
| 114 | if (isset($this->rules[$field])) { |
||
| 115 | if (!empty($rule) && isset($this->rules[$field][$rule])) { |
||
| 116 | unset($this->rules[$field][$rule]); |
||
| 117 | } else { |
||
| 118 | if (empty($rule)) { |
||
| 119 | unset($this->rules[$field]); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Flush ruels |
||
| 128 | */ |
||
| 129 | public function flushRules() |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Validates the data against the rules |
||
| 137 | * @param array $data |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function isValid(array $data): bool |
||
| 141 | { |
||
| 142 | $this->data = $data; |
||
| 143 | |||
| 144 | if (count($this->rules)) { |
||
| 145 | |||
| 146 | foreach ($this->rules as $field => $rule) { |
||
| 147 | if (!array_key_exists($field, $data)) { |
||
| 148 | $data[$field] = ''; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | foreach ($data as $field => $value) { |
||
| 153 | |||
| 154 | if (isset($this->rules[$field])) { |
||
| 155 | foreach ($this->rules[$field] as $method => $param) { |
||
| 156 | |||
| 157 | if (is_callable([$this, $method])) { |
||
| 158 | $this->$method($field, $value, $param); |
||
| 159 | } elseif (isset($this->customValidations[$method])) { |
||
| 160 | $data = [ |
||
| 161 | 'rule' => $method, |
||
| 162 | 'field' => $field, |
||
| 163 | 'value' => $value, |
||
| 164 | 'param' => $param ?? null |
||
| 165 | ]; |
||
| 166 | |||
| 167 | $this->callCustomFunction($this->customValidations[$method], $data); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | return !count($this->errors); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Adds custom validation |
||
| 179 | * @param string $rule |
||
| 180 | * @param Closure $function |
||
| 181 | */ |
||
| 182 | public function addValidation(string $rule, Closure $function) |
||
| 183 | { |
||
| 184 | if (!empty($rule) && is_callable($function)) { |
||
| 185 | $this->customValidations[$rule] = $function; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Gets validation errors |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getErrors(): array |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Adds validation Error |
||
| 223 | * @param string $field |
||
| 224 | * @param string $rule |
||
| 225 | * @param null|mixed $param |
||
| 226 | */ |
||
| 227 | protected function addError(string $field, string $rule, $param = null) |
||
| 228 | { |
||
| 229 | if (!isset($this->errors[$field])) { |
||
| 230 | $this->errors[$field] = []; |
||
| 231 | } |
||
| 232 | |||
| 233 | $this->errors[$field][$rule] = $param; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Flush errors |
||
| 238 | */ |
||
| 239 | public function flushErrors() |
||
| 240 | { |
||
| 241 | $this->errors = []; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Calls custom function defined by developer |
||
| 246 | * @param Closure $function |
||
| 247 | * @param array $data |
||
| 248 | */ |
||
| 249 | protected function callCustomFunction(Closure $function, array $data) |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |