Test Setup Failed
Push — master ( 2fc4ab...ad3b88 )
by Mohamed
15:12 queued 08:10
created

ValidatorExtended::validateNic()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 6
nop 4
dl 0
loc 17
rs 9.8666
1
<?php
2
3
namespace App\Providers;
4
5
namespace App\Providers;
6
7
use App\Models\Academic_period;
8
use App\Models\Area_administrative;
9
use App\Models\Security_user;
10
use App\Models\Identity_type;
11
use Illuminate\Support\Carbon;
12
use Illuminate\Support\Facades\Date;
13
use Illuminate\Support\Facades\Validator;
14
use Illuminate\Validation\Validator as IlluminateValidator;
15
use Illuminate\Support\Facades\Log;
16
use App\Models\Education_grade;
17
use App\Models\Institution_class;
18
use App\Models\Institution_class_grade;
19
use App\Models\Institution_class_student;
20
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)
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

65
    protected function validateAdmissionAge($attribute, $value, $parameters, /** @scrutinizer ignore-unused */ $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        $institutionClass = Institution_class::find($parameters[0]);
68
        $institutionGrade = Institution_class_grade::where('institution_class_id', '=', $institutionClass->id)->first();
69
        if (!empty($institutionClass)) {
70
            $gradeEntity = Education_grade::where('id', '=', $institutionGrade->education_grade_id)->first();
0 ignored issues
show
Bug introduced by
The property education_grade_id does not seem to exist on App\Models\Institution_class_grade. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
71
            $academicPeriod = Academic_period::find($institutionClass->academic_period_id);
0 ignored issues
show
Bug introduced by
The property academic_period_id does not seem to exist on App\Models\Institution_class. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
72
            if (empty($value)) {
73
                return false;
74
            } elseif ($gradeEntity !== null) {
75
                $admissionAge = (($gradeEntity->admission_age) * 12) - 1;
0 ignored issues
show
Bug introduced by
The property admission_age does not seem to exist on App\Models\Education_grade. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
76
                $to = $academicPeriod->start_date;
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on App\Models\Academic_period. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
77
                $diff_in_months = $to->diffInMonths($value);
78
                $ageOfStudent = $diff_in_months;
79
                $enrolmentMaximumAge = $admissionAge + 120;
80
                return ($ageOfStudent <= $enrolmentMaximumAge) && ($ageOfStudent >= $admissionAge);
81
            } else {
82
                return false;
83
            }
84
        } else {
85
            $this->_custom_messages['admission_age'] = 'given' . $attribute . 'Not found';
86
            $this->_set_custom_stuff();
87
            return false;
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();
0 ignored issues
show
Bug introduced by
The property education_grade_id does not seem to exist on App\Models\Institution_class_grade. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
119
        if (in_array($institutionGrade->code, $bmiGrades)) {
0 ignored issues
show
Bug introduced by
The property code does not seem to exist on App\Models\Institution_class_grade. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on App\Models\Education_grade. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $perameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

138
    protected function validateBirthPlace($attribute, $value, /** @scrutinizer ignore-unused */ $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

138
    protected function validateBirthPlace(/** @scrutinizer ignore-unused */ $attribute, $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

160
    protected function validateIsStudentInClass(/** @scrutinizer ignore-unused */ $attribute, $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

160
    protected function validateIsStudentInClass($attribute, $value, $perameters, /** @scrutinizer ignore-unused */ $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

175
    protected function validateNic($attribute, $value, $perameters, /** @scrutinizer ignore-unused */ $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $perameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

175
    protected function validateNic($attribute, $value, /** @scrutinizer ignore-unused */ $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
    {
177
        switch($data['identity_type']){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

195
    protected function validateUserUnique(/** @scrutinizer ignore-unused */ $attribute, $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $perameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

195
    protected function validateUserUnique($attribute, $value, /** @scrutinizer ignore-unused */ $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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') {
0 ignored issues
show
Bug introduced by
The property national_code does not seem to exist on App\Models\Identity_type. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $perameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

211
    protected function validateIsBc($attribute, $value, /** @scrutinizer ignore-unused */ $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

211
    protected function validateIsBc(/** @scrutinizer ignore-unused */ $attribute, $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

211
    protected function validateIsBc($attribute, /** @scrutinizer ignore-unused */ $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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') {
0 ignored issues
show
Bug introduced by
The property national_code does not seem to exist on App\Models\Identity_type. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

227
    protected function checkUnique($value, /** @scrutinizer ignore-unused */ $data, $identityType)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property openemis_no does not seem to exist on App\Models\Security_user. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The property national_code does not seem to exist on App\Models\Identity_type. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $perameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

254
    protected function ValidateIdentity($attribute, $value, /** @scrutinizer ignore-unused */ $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
255
256
    {
257
       // dd($value);
258
       
259
        foreach ($validator->getData() as $data) {
260
           
261
            
262
            if(($data['identity_type'] != null)&&($value != null))
263
            {
264
                //dd(true);
265
               
266
               if(!strcmp($data['identity_type'],"BC"))
267
               {
268
                    $out = 1;
269
               }
270
               else 
271
               {
272
                   $out = 2;
273
               }
274
               
275
               if(($out == 1)&&(preg_match('/^[0-9]{4}+$/',$value)))
276
                {
277
                    //dd(true);
278
                    return true;
279
    
280
                }
281
                
282
                elseif (($out == 2) &&(preg_match('/^[0-9]{9}[VX]{1}+$/',$value)))
283
                 {
284
                    return true;
285
                }
286
                elseif (($out == 2) && (preg_match('/^[0-9]{12}+$/',$value)))
287
                {
288
                    return true;
289
                }
290
                else {
291
                   //dd(false);
292
                        $this ->_custom_messages['unmatch'] = $attribute." format does not match with Identity type";
293
                        $this->_set_custom_stuff();
294
                        return false;
295
                       
296
                }
297
            }
298
            else if(($data['identity_type'] != null)&&($value == null)){
299
                return true;
300
            }
301
            else {
302
                if(preg_match('/^[0-9]{4}+$/',$value))
303
                {
304
                    //dd(true);
305
                    return true;
306
    
307
                }
308
                
309
                elseif (preg_match('/^[0-9]{9}[VX]{1}+$/',$value))
310
                 {
311
                    return true;
312
                }
313
                elseif (preg_match('/^[0-9]{12}+$/',$value))
314
                {
315
                    return true;
316
                }
317
                else {
318
                    $this ->_custom_messages['idnum'] = $attribute." format is invalild";
319
                    $this->_set_custom_stuff();
320
                    return false;
321
                }
322
            }
323
        }
324
325
        
326
    }
327
}
328