| Total Complexity | 43 |
| Total Lines | 218 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Validate 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 Validate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Validate |
||
| 29 | { |
||
| 30 | /** @var array $_currentItem The immediately posted item */ |
||
| 31 | private $_currentItem = null; |
||
| 32 | |||
| 33 | /** @var array $_postData Stores the Posted Data */ |
||
| 34 | private $_postData = array(); |
||
| 35 | |||
| 36 | /** @var object $_val The validator object */ |
||
| 37 | private $_val = array(); |
||
| 38 | |||
| 39 | /** @var array $_error Holds the current forms errors */ |
||
| 40 | private $_error = array(); |
||
| 41 | |||
| 42 | /** @var string $_method The Method POST, GET and PUT for form */ |
||
| 43 | private $_method; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * __construct - Instantiates the validator class. |
||
| 47 | */ |
||
| 48 | public function __construct($validationFields = null) |
||
| 49 | { |
||
| 50 | if (is_null($validationFields)) { |
||
| 51 | $this->_val = $validationFields = new Val(); |
||
| 52 | } |
||
| 53 | $this->_val = $validationFields; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function getMethod() |
||
| 57 | { |
||
| 58 | return $this->_method; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function setMethod($method) |
||
| 62 | { |
||
| 63 | $this->_method = $method; |
||
| 64 | |||
| 65 | return $this; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function post($field) |
||
| 69 | { |
||
| 70 | if ($this->_method == 'POST') { |
||
| 71 | $this->_postData[$field] = $_POST[$field]; |
||
| 72 | $this->_currentItem = $field; |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($this->_method == 'GET') { |
||
| 76 | $this->_postData[$field] = $_GET[$field]; |
||
| 77 | $this->_currentItem = $field; |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($this->_method == 'COOKIE') { |
||
| 81 | $this->_postData[$field] = $_COOKIE[$field]; |
||
| 82 | $this->_currentItem = $field; |
||
| 83 | } |
||
| 84 | |||
| 85 | return $this; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function any($field) |
||
| 89 | { |
||
| 90 | $req_method = strtolower($_SERVER['REQUEST_METHOD']); |
||
| 91 | |||
| 92 | if ($req_method == true) { |
||
|
|
|||
| 93 | $this->_postData[$field] = $req_method[$field]; |
||
| 94 | $this->_currentItem = $field; |
||
| 95 | } |
||
| 96 | |||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getPostDataForHash($fieldName = null, HashInterface $hashObject = null) |
||
| 101 | { |
||
| 102 | $it = new IteratorCollection($this->_postData); |
||
| 103 | if (isset($fieldName) && is_object($hashObject)) { |
||
| 104 | if (isset($this->_postData[$fieldName])) { |
||
| 105 | $this->_postData[$this->_currentItem]; |
||
| 106 | |||
| 107 | $securityHash = $hashObject::hash_password($this->_postData[$fieldName]); |
||
| 108 | $it->set($fieldName, $securityHash); |
||
| 109 | } |
||
| 110 | |||
| 111 | return $it->toArray(); |
||
| 112 | } |
||
| 113 | |||
| 114 | return $this->_postData; |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getPostData($fieldName = false) |
||
| 118 | { |
||
| 119 | if ($fieldName == false) { |
||
| 120 | return $this->_postData; |
||
| 121 | } |
||
| 122 | if ($this->submit() == true) { |
||
| 123 | if ($fieldName && isset($this->_postData[$fieldName])) { |
||
| 124 | return $this->_postData[$fieldName]; |
||
| 125 | } |
||
| 126 | |||
| 127 | return false; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $typeOfValidator |maxlength|minlength|digit|isValidLenght |
||
| 133 | * @param int $arg |
||
| 134 | * |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function val(string $typeOfValidator, int $length) |
||
| 138 | { |
||
| 139 | $error = ''; |
||
| 140 | |||
| 141 | if (!empty($length)) { |
||
| 142 | $error = $this->_val->{$typeOfValidator}($this->_postData[$this->_currentItem], $length); |
||
| 143 | } |
||
| 144 | if ($error) { |
||
| 145 | $this->_error[$this->_currentItem] = $error; |
||
| 146 | } |
||
| 147 | |||
| 148 | return $this; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function text() |
||
| 152 | { |
||
| 153 | if (!is_string($this->_postData[$this->_currentItem])) { |
||
| 154 | throw new \InvalidArgumentException('the value (' . $this->_postData[$this->_currentItem] . ') entered must have a Text'); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $this; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function email() |
||
| 161 | { |
||
| 162 | $email = filter_var($this->_postData[$this->_currentItem], FILTER_SANITIZE_EMAIL); |
||
| 163 | if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
||
| 164 | throw new \InvalidArgumentException('the value (' . $this->_postData[$this->_currentItem] . ') entered must have a Email'); |
||
| 165 | } |
||
| 166 | |||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function numeric() |
||
| 171 | { |
||
| 172 | if (!is_numeric($this->_postData[$this->_currentItem])) { |
||
| 173 | throw new \InvalidArgumentException('the value (' . $this->_postData[$this->_currentItem] . ') entered must have a Numeric'); |
||
| 174 | } |
||
| 175 | |||
| 176 | return $this; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function int() |
||
| 180 | { |
||
| 181 | if (!intval($this->_postData[$this->_currentItem])) { |
||
| 182 | throw new \InvalidArgumentException('the value (' . $this->_postData[$this->_currentItem] . ') entered must have a Int'); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $this; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function long() |
||
| 189 | { |
||
| 190 | if (!floatval($this->_postData[$this->_currentItem])) { |
||
| 191 | throw new \InvalidArgumentException('the value (' . $this->_postData[$this->_currentItem] . ') entered must have a Long'); |
||
| 192 | } |
||
| 193 | |||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | |||
| 197 | public function domain() |
||
| 198 | { |
||
| 199 | if (!filter_var($this->_postData[$this->_currentItem], FILTER_VALIDATE_DOMAIN)) { |
||
| 200 | throw new \InvalidArgumentException('the value (' . $this->_postData[$this->_currentItem] . ') entered must have a Domain'); |
||
| 201 | } |
||
| 202 | |||
| 203 | return $this; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function url() |
||
| 207 | { |
||
| 208 | if (!filter_var($this->_postData[$this->_currentItem], FILTER_VALIDATE_URL)) { |
||
| 209 | throw new \InvalidArgumentException('the value (' . $this->_postData[$this->_currentItem] . ') entered must have a Url'); |
||
| 210 | } |
||
| 211 | |||
| 212 | return $this; |
||
| 213 | } |
||
| 214 | |||
| 215 | public function ip() |
||
| 216 | { |
||
| 217 | if (!filter_var($this->_postData[$this->_currentItem], FILTER_VALIDATE_IP)) { |
||
| 218 | throw new \InvalidArgumentException('the value (' . $this->_postData[$this->_currentItem] . ') entered must have a Ip'); |
||
| 219 | } |
||
| 220 | |||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function date() |
||
| 231 | } |
||
| 232 | |||
| 233 | public function submit() |
||
| 234 | { |
||
| 235 | if (empty($this->_error)) { |
||
| 236 | return true; |
||
| 237 | } |
||
| 238 | |||
| 239 | $str = ''; |
||
| 240 | foreach ($this->_error as $key => $value) { |
||
| 241 | $str .= $key . ' => ' . $value . "\n" . '<br>'; |
||
| 242 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 |