| Total Complexity | 68 |
| Total Lines | 300 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ValidatorExtended 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 ValidatorExtended, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ValidatorExtended extends IlluminateValidator |
||
| 22 | { |
||
| 23 | |||
| 24 | private $_custom_messages = array( |
||
| 25 | "admission_age" => "The age limit not match with admission age for this class", |
||
| 26 | "birth_place" => 'The Birth place combination in not valid, refer the Birth Registrar office only belongs to Divisional Secretariat', |
||
| 27 | 'user_unique' => 'The Birth place combination in not valid, refer the Birth Registrar office only belongs to Divisional Secretariat', |
||
| 28 | "is_bc" => "The Birth Certificate number is not valid", |
||
| 29 | "nic" => "NIC number is Not valid", |
||
| 30 | "is_student_in_class" => "The Student ID is not belong to this class", |
||
| 31 | "bmi" => "The record must have BMI information", |
||
| 32 | "unmatch" => "Identity number format should match with Identity type", |
||
| 33 | "idnum" => "Identity number format is invalid" |
||
| 34 | ); |
||
| 35 | |||
| 36 | public function __construct( |
||
| 37 | $translator, |
||
| 38 | $data, |
||
| 39 | $rules, |
||
| 40 | $messages = array(), |
||
| 41 | $customAttributes = array() |
||
| 42 | ) { |
||
| 43 | parent::__construct( |
||
| 44 | $translator, |
||
| 45 | $data, |
||
| 46 | $rules, |
||
| 47 | $messages, |
||
| 48 | $customAttributes |
||
| 49 | ); |
||
| 50 | $this->_set_custom_stuff(); |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function _set_custom_stuff() |
||
| 54 | { |
||
| 55 | //setup our custom error messages |
||
| 56 | $this->setCustomMessages($this->_custom_messages); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * this will validate admission age limit of the student |
||
| 61 | * |
||
| 62 | * admission age validation |
||
| 63 | */ |
||
| 64 | |||
| 65 | protected function validateAdmissionAge($attribute, $value, $parameters, $validator) |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | protected function validateHW($attribute, $value) |
||
| 92 | { |
||
| 93 | |||
| 94 | if (is_numeric($value)) { |
||
| 95 | if ($value < 10) { |
||
| 96 | $this->_custom_messages['bmi'] = $attribute . ' is must greater than 10'; |
||
| 97 | $this->_set_custom_stuff(); |
||
| 98 | return false; |
||
| 99 | } elseif ($value > 250) { |
||
| 100 | $this->_custom_messages['bmi'] = $attribute . ' is must smaller than 250'; |
||
| 101 | $this->_set_custom_stuff(); |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | $this->_custom_messages['bmi'] = $attribute . ' is must a valid numeric'; |
||
| 106 | $this->_set_custom_stuff(); |
||
| 107 | return false; |
||
| 108 | } |
||
| 109 | return true; |
||
| 110 | } |
||
| 111 | |||
| 112 | protected function validateBmi($attribute, $value, $parameters) |
||
| 113 | { |
||
| 114 | $bmiGrades = ['G1', 'G4', 'G7', 'G10']; |
||
| 115 | $institutionGrade = Institution_class_grade::where('institution_class_id', '=', $parameters[0]) |
||
| 116 | ->join('education_grades', 'institution_class_grades.education_grade_id', 'education_grades.id') |
||
| 117 | ->first(); |
||
| 118 | $educationGrade = Education_grade::where('id', '=', $institutionGrade->education_grade_id)->first(); |
||
| 119 | if (in_array($institutionGrade->code, $bmiGrades)) { |
||
| 120 | if (!empty($value)) { |
||
| 121 | if (($attribute == 'bmi_height') || ('bmi_weight')) { |
||
| 122 | return $this->validateHW($attribute, $value); |
||
| 123 | } |
||
| 124 | } else { |
||
| 125 | $this->_custom_messages['bmi'] = $attribute . ' is required for ' . $educationGrade->name; |
||
| 126 | $this->_set_custom_stuff(); |
||
| 127 | return false; |
||
| 128 | } |
||
| 129 | } elseif (!empty($value)) { |
||
| 130 | if (($attribute == 'bmi_height') || ('bmi_weight')) { |
||
| 131 | return $this->validateHW($attribute, $value); |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | return true; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | protected function validateBirthPlace($attribute, $value, $perameters, $validator) |
||
| 139 | { |
||
| 140 | foreach ($validator->getData() as $data) { |
||
| 141 | if ($data['identity_type'] == 'BC' && key_exists('birth_divisional_secretariat', $data)) { |
||
| 142 | $BirthDivision = Area_administrative::where('name', '=', '%' . $data['birth_divisional_secretariat'] . '%')->where('area_administrative_level_id', '=', 5); // |
||
| 143 | if ($BirthDivision->count() > 0) { |
||
| 144 | $BirthArea = Area_administrative::where('name', '=', '%' . $value . '%') //$data['birth_registrar_office_as_in_birth_certificate'] |
||
| 145 | ->where('parent_id', '=', $BirthDivision->first()->id)->count(); |
||
| 146 | return $BirthArea > 0; |
||
| 147 | } elseif (key_exists('birth_divisional_secretariat', $data) && (!key_exists('birth_registrar_office_as_in_birth_certificate', $data))) { |
||
| 148 | $this->_custom_messages['birth_place'] = 'birth_registrar_office_as_in_birth_certificate required with BC'; |
||
| 149 | $this->_set_custom_stuff(); |
||
| 150 | return false; |
||
| 151 | } else { |
||
| 152 | return true; |
||
| 153 | } |
||
| 154 | } else { |
||
| 155 | return true; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | protected function validateIsStudentInClass($attribute, $value, $perameters, $validator) |
||
| 161 | { |
||
| 162 | $student = Security_user::where('openemis_no', '=', $value); |
||
| 163 | if ($student->count() > 0) { |
||
| 164 | $student = $student->first()->toArray(); |
||
| 165 | $check = Institution_class_student::where('student_id', '=', $student['id'])->where('institution_class_id', '=', $perameters[0])->count(); |
||
| 166 | if ($check == 1) { |
||
| 167 | return true; |
||
| 168 | } else { |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | } else { |
||
| 172 | return false; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | protected function validateNic($attribute, $value, $perameters, $validator) |
||
| 176 | { |
||
| 177 | switch($data['identity_type']){ |
||
| 178 | case 'BC': |
||
| 179 | //inclde the bc validation |
||
| 180 | break; |
||
| 181 | case 'NIC': |
||
| 182 | //inclde the NIC validation |
||
| 183 | break; |
||
| 184 | } |
||
| 185 | $valid = preg_match('/^([0-9]{9}[VX]|[0-9]{12})$/i', $value); |
||
| 186 | if (!$valid) { |
||
| 187 | $this->_custom_messages['nic'] = $attribute . ' is not valid, Please check the NIC number'; |
||
| 188 | $this->_set_custom_stuff(); |
||
| 189 | return false; |
||
| 190 | } else { |
||
| 191 | return true; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | protected function validateUserUnique($attribute, $value, $perameters, $validator) |
||
| 196 | { |
||
| 197 | foreach ($validator->getData() as $data) { |
||
| 198 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||
| 199 | if ($identityType !== null && ($value !== null)) { |
||
| 200 | if ($identityType->national_code === 'BC') { |
||
| 201 | return $this->checkUnique($value, $data, $identityType); |
||
| 202 | } elseif ($identityType->national_code === 'NIC') { |
||
| 203 | return $this->checkUnique($value, $data, $identityType); |
||
| 204 | } |
||
| 205 | } elseif (($value == null) || $value == "") { |
||
| 206 | return true; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | protected function validateIsBc($attribute, $value, $perameters, $validator) |
||
| 212 | { |
||
| 213 | foreach ($validator->getData() as $data) { |
||
| 214 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||
| 215 | if (($identityType !== null) && ($identityType !== "")) { |
||
| 216 | if (($identityType->national_code) === 'BC') { |
||
| 217 | return (strlen((string) $data['identity_number']) < 7); |
||
| 218 | } else { |
||
| 219 | return true; |
||
| 220 | } |
||
| 221 | } else { |
||
| 222 | return true; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | protected function checkUnique($value, $data, $identityType) |
||
| 228 | { |
||
| 229 | $isUnique = Security_user::where('identity_number', '=', $value)->where('identity_type_id', '=', $identityType->id); |
||
| 230 | if ($isUnique->count() > 0) { |
||
| 231 | $this->_custom_messages['user_unique'] = 'The identity number already in use. User ID is : ' . $isUnique->first()->openemis_no; |
||
| 232 | $this->_set_custom_stuff(); |
||
| 233 | return false; |
||
| 234 | } else { |
||
| 235 | return true; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | protected function IsBc($data, $value) |
||
| 240 | { |
||
| 241 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||
| 242 | if ($identityType !== null) { |
||
| 243 | if (($identityType->national_code) === 'BC' && strlen((string) $value) < 8) { |
||
| 244 | return false; |
||
| 245 | } else { |
||
| 246 | return true; |
||
| 247 | } |
||
| 248 | } else { |
||
| 249 | return true; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | |||
| 254 | protected function ValidateIdentity($attribute, $value, $perameters, $validator) |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | |||
| 326 | } |
||
| 327 | } |
||
| 328 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.