Total Complexity | 53 |
Total Lines | 236 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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 | "identity" => "Identity number format should match with Identity type", |
||
33 | ); |
||
34 | |||
35 | public function __construct( |
||
36 | $translator, |
||
37 | $data, |
||
38 | $rules, |
||
39 | $messages = array(), |
||
40 | $customAttributes = array() |
||
41 | ) { |
||
42 | parent::__construct( |
||
43 | $translator, |
||
44 | $data, |
||
45 | $rules, |
||
46 | $messages, |
||
47 | $customAttributes |
||
48 | ); |
||
49 | $this->_set_custom_stuff(); |
||
50 | } |
||
51 | |||
52 | protected function _set_custom_stuff() |
||
53 | { |
||
54 | //setup our custom error messages |
||
55 | $this->setCustomMessages($this->_custom_messages); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * this will validate admission age limit of the student |
||
60 | * |
||
61 | * admission age validation |
||
62 | */ |
||
63 | |||
64 | protected function validateAdmissionAge($attribute, $value, $parameters, $validator) |
||
87 | } |
||
88 | } |
||
89 | |||
90 | protected function validateHW($attribute, $value) |
||
91 | { |
||
92 | |||
93 | if (is_numeric($value)) { |
||
94 | if ($value < 10) { |
||
95 | $this->_custom_messages['bmi'] = $attribute . ' is must greater than 10'; |
||
96 | $this->_set_custom_stuff(); |
||
97 | return false; |
||
98 | } elseif ($value > 250) { |
||
99 | $this->_custom_messages['bmi'] = $attribute . ' is must smaller than 250'; |
||
100 | $this->_set_custom_stuff(); |
||
101 | return false; |
||
102 | } |
||
103 | } else { |
||
104 | $this->_custom_messages['bmi'] = $attribute . ' is must a valid numeric'; |
||
105 | $this->_set_custom_stuff(); |
||
106 | return false; |
||
107 | } |
||
108 | return true; |
||
109 | } |
||
110 | |||
111 | protected function validateBmi($attribute, $value, $parameters) |
||
112 | { |
||
113 | $bmiGrades = ['G1', 'G4', 'G7', 'G10']; |
||
114 | $institutionGrade = Institution_class_grade::where('institution_class_id', '=', $parameters[0]) |
||
115 | ->join('education_grades', 'institution_class_grades.education_grade_id', 'education_grades.id') |
||
116 | ->first(); |
||
117 | $educationGrade = Education_grade::where('id', '=', $institutionGrade->education_grade_id)->first(); |
||
118 | if (in_array($institutionGrade->code, $bmiGrades)) { |
||
119 | if (!empty($value)) { |
||
120 | if (($attribute == 'bmi_height') || ('bmi_weight')) { |
||
121 | return $this->validateHW($attribute, $value); |
||
122 | } |
||
123 | } else { |
||
124 | $this->_custom_messages['bmi'] = $attribute . ' is required for ' . $educationGrade->name; |
||
125 | $this->_set_custom_stuff(); |
||
126 | return false; |
||
127 | } |
||
128 | } elseif (!empty($value)) { |
||
129 | if (($attribute == 'bmi_height') || ('bmi_weight')) { |
||
130 | return $this->validateHW($attribute, $value); |
||
131 | } |
||
132 | } else { |
||
133 | return true; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | protected function validateBirthPlace($attribute, $value, $perameters, $validator) |
||
138 | { |
||
139 | foreach ($validator->getData() as $data) { |
||
140 | if ($data['identity_type'] == 'BC' && key_exists('birth_divisional_secretariat', $data)) { |
||
141 | $BirthDivision = Area_administrative::where('name', '=', '%' . $data['birth_divisional_secretariat'] . '%')->where('area_administrative_level_id', '=', 5); // |
||
142 | if ($BirthDivision->count() > 0) { |
||
143 | $BirthArea = Area_administrative::where('name', '=', '%' . $value . '%') //$data['birth_registrar_office_as_in_birth_certificate'] |
||
144 | ->where('parent_id', '=', $BirthDivision->first()->id)->count(); |
||
145 | return $BirthArea > 0; |
||
146 | } elseif (key_exists('birth_divisional_secretariat', $data) && (!key_exists('birth_registrar_office_as_in_birth_certificate', $data))) { |
||
147 | $this->_custom_messages['birth_place'] = 'birth_registrar_office_as_in_birth_certificate required with BC'; |
||
148 | $this->_set_custom_stuff(); |
||
149 | return false; |
||
150 | } else { |
||
151 | return true; |
||
152 | } |
||
153 | } else { |
||
154 | return true; |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | protected function validateIsStudentInClass($attribute, $value, $perameters, $validator) |
||
160 | { |
||
161 | $student = Security_user::where('openemis_no', '=', $value); |
||
162 | if ($student->count() > 0) { |
||
163 | $student = $student->first()->toArray(); |
||
164 | $check = Institution_class_student::where('student_id', '=', $student['id'])->where('institution_class_id', '=', $perameters[0])->count(); |
||
165 | if ($check == 1) { |
||
166 | return true; |
||
167 | } else { |
||
168 | return false; |
||
169 | } |
||
170 | } else { |
||
171 | return false; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | |||
176 | |||
177 | protected function validateIdentity($attribute, $value, $perameters, $validator) |
||
178 | { |
||
179 | $valid = true; |
||
180 | foreach ($validator->getData() as $data) { |
||
181 | switch($data[$perameters[0]]){ |
||
182 | case 'BC': |
||
183 | $valid = preg_match('/^([0-9]{10,12})$/i', $value); |
||
184 | break; |
||
185 | case 'NIC': |
||
186 | $valid = preg_match('/^([0-9]{9}[VX]|[0-9]{12})$/i', $value); |
||
187 | break; |
||
188 | default: |
||
189 | $valid = true; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | if (!$valid) { |
||
194 | $this->_custom_messages['nic'] = $attribute . ' is not valid, Please check the NIC number'; |
||
195 | $this->_set_custom_stuff(); |
||
196 | return false; |
||
197 | } else { |
||
198 | return true; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | protected function validateUserUnique($attribute, $value, $perameters, $validator) |
||
203 | { |
||
204 | foreach ($validator->getData() as $data) { |
||
205 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||
206 | if ($identityType !== null && ($value !== null)) { |
||
207 | if ($identityType->national_code === 'BC') { |
||
208 | return $this->checkUnique($value, $data, $identityType); |
||
209 | } elseif ($identityType->national_code === 'NIC') { |
||
210 | return $this->checkUnique($value, $data, $identityType); |
||
211 | } |
||
212 | } elseif (($value == null) || $value == "") { |
||
213 | return true; |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | protected function validateIsBc($attribute, $value, $perameters, $validator) |
||
219 | { |
||
220 | foreach ($validator->getData() as $data) { |
||
221 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||
222 | if (($identityType !== null) && ($identityType !== "")) { |
||
223 | if (($identityType->national_code) === 'BC') { |
||
224 | return (strlen((string) $data['identity_number']) < 7); |
||
225 | } else { |
||
226 | return true; |
||
227 | } |
||
228 | } else { |
||
229 | return true; |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | protected function checkUnique($value, $data, $identityType) |
||
243 | } |
||
244 | } |
||
245 | |||
246 | protected function IsBc($data, $value) |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.