| Total Complexity | 44 |
| Total Lines | 210 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Validation 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 Validation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Validation |
||
| 6 | { |
||
| 7 | private $app; |
||
| 8 | |||
| 9 | private $input; |
||
| 10 | |||
| 11 | private $errors = []; |
||
| 12 | |||
| 13 | public function __construct(Application $app) |
||
| 16 | } |
||
| 17 | |||
| 18 | public function input($input) |
||
| 23 | } |
||
| 24 | |||
| 25 | public function require($input = null, $msg = null) |
||
| 26 | { |
||
| 27 | if (!$this->input) { |
||
| 28 | |||
| 29 | $this->input = $input; |
||
| 30 | } |
||
| 31 | |||
| 32 | $value = $this->value($this->input); |
||
| 33 | |||
| 34 | if (!$value) { |
||
| 35 | |||
| 36 | $msg = $msg ?: 'This input is required'; |
||
| 37 | |||
| 38 | $this->addError($this->input, $msg); |
||
| 39 | } |
||
| 40 | return $this; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function email($input = null, $msg = null) |
||
| 60 | } |
||
| 61 | |||
| 62 | public function number($input = null, $msg = null) |
||
| 63 | { |
||
| 64 | if (!$this->input) { |
||
| 65 | |||
| 66 | $this->input = $input; |
||
| 67 | } |
||
| 68 | |||
| 69 | $value = $this->value($this->input); |
||
| 70 | |||
| 71 | if ($value) { |
||
| 72 | |||
| 73 | if (!is_numeric($value)) { |
||
| 74 | |||
| 75 | $msg = $msg ?: 'the Input must be number'; |
||
| 76 | |||
| 77 | $this->addError($this->input, $msg); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | return $this; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function text($input = null, $msg = null) |
||
| 104 | } |
||
| 105 | |||
| 106 | public function minLen($length, $msg = null) |
||
| 121 | } |
||
| 122 | |||
| 123 | public function maxLen($length, $msg = null) |
||
| 124 | { |
||
| 125 | $value = $this->value($this->input); |
||
| 126 | |||
| 127 | if ($value) { |
||
| 128 | |||
| 129 | if (strlen($value) > $length) { |
||
| 130 | |||
| 131 | $msg = $msg ?: 'This must be ' . $length . ' or fewer'; |
||
| 132 | |||
| 133 | $this->addError($this->input, $msg); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function match($input, $msg = null) |
||
| 157 | } |
||
| 158 | |||
| 159 | public function unique(array $data, $msg = null) |
||
| 160 | { |
||
| 161 | $value = $this->value($this->input); |
||
| 162 | |||
| 163 | list($table, $column) = $data; |
||
| 164 | |||
| 165 | $result = $this->app->db->select($column)->from($table)->where($column . ' = ? ', $value)->fetch(); |
||
| 166 | |||
| 167 | if ($result) { |
||
| 168 | |||
| 169 | $msg = $msg ?: sprintf('%s is already exist', ucfirst($this->input)); |
||
| 170 | |||
| 171 | $this->addError($this->input, $msg); |
||
| 172 | } |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function message($msg = null) |
||
| 178 | { |
||
| 179 | $this->errors[] = $msg; |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function passes() |
||
| 185 | { |
||
| 186 | return empty($this->errors); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function fails() |
||
| 190 | { |
||
| 191 | return !empty($this->errors); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function getMsgs() |
||
| 195 | { |
||
| 196 | return $this->errors; |
||
| 197 | } |
||
| 198 | |||
| 199 | private function value($input) |
||
| 200 | { |
||
| 201 | return $this->app->request->post($input); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function addError($input, $msg) |
||
| 205 | { |
||
| 206 | if (!$this->checkError($input)) { |
||
| 207 | |||
| 208 | $this->errors[$input] = $msg; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | private function checkError($input) |
||
| 215 | } |
||
| 216 | } |