Total Complexity | 53 |
Total Lines | 235 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 | "is_student_in_class" => "The Student ID is not belong to this class", |
||
30 | "bmi" => "The record must have BMI information", |
||
31 | "identity" => "Identity number format should match with Identity type", |
||
32 | ); |
||
33 | |||
34 | public function __construct( |
||
35 | $translator, |
||
36 | $data, |
||
37 | $rules, |
||
38 | $messages = array(), |
||
39 | $customAttributes = array() |
||
40 | ) { |
||
41 | parent::__construct( |
||
42 | $translator, |
||
43 | $data, |
||
44 | $rules, |
||
45 | $messages, |
||
46 | $customAttributes |
||
47 | ); |
||
48 | $this->_set_custom_stuff(); |
||
49 | } |
||
50 | |||
51 | protected function _set_custom_stuff() |
||
52 | { |
||
53 | //setup our custom error messages |
||
54 | $this->setCustomMessages($this->_custom_messages); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * this will validate admission age limit of the student |
||
59 | * |
||
60 | * admission age validation |
||
61 | */ |
||
62 | |||
63 | protected function validateAdmissionAge($attribute, $value, $parameters, $validator) |
||
|
|||
64 | { |
||
65 | $institutionClass = Institution_class::find($parameters[0]); |
||
66 | $institutionGrade = Institution_class_grade::where('institution_class_id', '=', $institutionClass->id)->first(); |
||
67 | if (!empty($institutionClass)) { |
||
68 | $gradeEntity = Education_grade::where('id', '=', $institutionGrade->education_grade_id)->first(); |
||
69 | $academicPeriod = Academic_period::find($institutionClass->academic_period_id); |
||
70 | if (empty($value)) { |
||
71 | return false; |
||
72 | } elseif ($gradeEntity !== null) { |
||
73 | $admissionAge = (($gradeEntity->admission_age) * 12) - 1; |
||
74 | $to = $academicPeriod->start_date; |
||
75 | $diff_in_months = $to->diffInMonths($value); |
||
76 | $ageOfStudent = $diff_in_months; |
||
77 | $enrolmentMaximumAge = $admissionAge + 120; |
||
78 | return ($ageOfStudent <= $enrolmentMaximumAge) && ($ageOfStudent >= $admissionAge); |
||
79 | } else { |
||
80 | return false; |
||
81 | } |
||
82 | } else { |
||
83 | $this->_custom_messages['admission_age'] = 'given' . $attribute . 'Not found'; |
||
84 | $this->_set_custom_stuff(); |
||
85 | return false; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | protected function validateHW($attribute, $value) |
||
90 | { |
||
91 | |||
92 | if (is_numeric($value)) { |
||
93 | if ($value < 10) { |
||
94 | $this->_custom_messages['bmi'] = $attribute . ' is must greater than 10'; |
||
95 | $this->_set_custom_stuff(); |
||
96 | return false; |
||
97 | } elseif ($value > 250) { |
||
98 | $this->_custom_messages['bmi'] = $attribute . ' is must smaller than 250'; |
||
99 | $this->_set_custom_stuff(); |
||
100 | return false; |
||
101 | } |
||
102 | } else { |
||
103 | $this->_custom_messages['bmi'] = $attribute . ' is must a valid numeric'; |
||
104 | $this->_set_custom_stuff(); |
||
105 | return false; |
||
106 | } |
||
107 | return true; |
||
108 | } |
||
109 | |||
110 | protected function validateBmi($attribute, $value, $parameters) |
||
111 | { |
||
112 | $bmiGrades = ['G1', 'G4', 'G7', 'G10']; |
||
113 | $institutionGrade = Institution_class_grade::where('institution_class_id', '=', $parameters[0]) |
||
114 | ->join('education_grades', 'institution_class_grades.education_grade_id', 'education_grades.id') |
||
115 | ->first(); |
||
116 | $educationGrade = Education_grade::where('id', '=', $institutionGrade->education_grade_id)->first(); |
||
117 | if (in_array($institutionGrade->code, $bmiGrades)) { |
||
118 | if (!empty($value)) { |
||
119 | if (($attribute == 'bmi_height') || ('bmi_weight')) { |
||
120 | return $this->validateHW($attribute, $value); |
||
121 | } |
||
122 | } else { |
||
123 | $this->_custom_messages['bmi'] = $attribute . ' is required for ' . $educationGrade->name; |
||
124 | $this->_set_custom_stuff(); |
||
125 | return false; |
||
126 | } |
||
127 | } elseif (!empty($value)) { |
||
128 | if (($attribute == 'bmi_height') || ('bmi_weight')) { |
||
129 | return $this->validateHW($attribute, $value); |
||
130 | } |
||
131 | } else { |
||
132 | return true; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | protected function validateBirthPlace($attribute, $value, $perameters, $validator) |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | protected function validateIsStudentInClass($attribute, $value, $perameters, $validator) |
||
171 | } |
||
172 | } |
||
173 | |||
174 | |||
175 | |||
176 | protected function validateIdentity($attribute, $value, $perameters, $validator) |
||
177 | { |
||
178 | $valid = true; |
||
179 | foreach ($validator->getData() as $data) { |
||
180 | switch($data[$perameters[0]]){ |
||
181 | case 'BC': |
||
182 | $valid = preg_match('/^([0-9]{3,5})/i', $value); |
||
183 | break; |
||
184 | case 'NIC': |
||
185 | $valid = preg_match('/^([0-9]{9}[VX]|([0-9]{12}))/i', $value); |
||
186 | break; |
||
187 | default: |
||
188 | $valid = true; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | if ($valid == 0) { |
||
193 | $this->_custom_messages['identity'] = $attribute . ' is not valid Please check the NIC number'; |
||
194 | $this->_set_custom_stuff(); |
||
195 | return false; |
||
196 | } else { |
||
197 | return true; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | protected function validateUserUnique($attribute, $value, $perameters, $validator) |
||
202 | { |
||
203 | foreach ($validator->getData() as $data) { |
||
204 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||
205 | if ($identityType !== null && ($value !== null)) { |
||
206 | if ($identityType->national_code === 'BC') { |
||
207 | return $this->checkUnique($value, $data, $identityType); |
||
208 | } elseif ($identityType->national_code === 'NIC') { |
||
209 | return $this->checkUnique($value, $data, $identityType); |
||
210 | } |
||
211 | } elseif (($value == null) || $value == "") { |
||
212 | return true; |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | protected function validateIsBc($attribute, $value, $perameters, $validator) |
||
218 | { |
||
219 | foreach ($validator->getData() as $data) { |
||
220 | $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first(); |
||
221 | if (($identityType !== null) && ($identityType !== "")) { |
||
222 | if (($identityType->national_code) === 'BC') { |
||
223 | return (strlen((string) $data['identity_number']) < 7); |
||
224 | } else { |
||
225 | return true; |
||
226 | } |
||
227 | } else { |
||
228 | return true; |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
233 | protected function checkUnique($value, $data, $identityType) |
||
242 | } |
||
243 | } |
||
244 | |||
245 | protected function IsBc($data, $value) |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.