ValidatorExtended::IsBc()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 10
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
        "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)
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

63
    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...
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();
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...
69
            $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...
70
            if (empty($value)) {
71
                return false;
72
            } elseif ($gradeEntity !== null) {
73
                $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...
74
                $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...
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();
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...
117
        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...
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;
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...
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)
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

136
    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...
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

136
    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...
137
    {
138
        foreach ($validator->getData() as $data) {
139
            if ($data['identity_type'] == 'BC' && key_exists('birth_divisional_secretariat', $data)) {
140
                $BirthDivision = Area_administrative::where('name', '=',  '%' . $data['birth_divisional_secretariat'] . '%')->where('area_administrative_level_id', '=', 5); //
141
                if ($BirthDivision->count() > 0) {
142
                    $BirthArea = Area_administrative::where('name', '=', '%' . $value . '%') //$data['birth_registrar_office_as_in_birth_certificate']
143
                        ->where('parent_id', '=', $BirthDivision->first()->id)->count();
144
                    return $BirthArea  > 0;
145
                } elseif (key_exists('birth_divisional_secretariat', $data) && (!key_exists('birth_registrar_office_as_in_birth_certificate', $data))) {
146
                    $this->_custom_messages['birth_place'] = 'birth_registrar_office_as_in_birth_certificate required with BC';
147
                    $this->_set_custom_stuff();
148
                    return false;
149
                } else {
150
                    return true;
151
                }
152
            } else {
153
                return true;
154
            }
155
        }
156
    }
157
158
    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

158
    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

158
    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...
159
    {
160
        $student =  Security_user::where('openemis_no', '=', $value);
161
        if ($student->count() > 0) {
162
            $student = $student->first()->toArray();
163
            $check =  Institution_class_student::where('student_id', '=', $student['id'])->where('institution_class_id', '=', $perameters[0])->count();
164
            if ($check == 1) {
165
                return true;
166
            } else {
167
                return false;
168
            }
169
        } else {
170
            return false;
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)
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

201
    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...
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

201
    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...
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') {
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...
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)
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

217
    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 $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

217
    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...
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

217
    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...
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') {
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...
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)
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

233
    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...
234
    {
235
        $isUnique = Security_user::where('identity_number', '=', $value)->where('identity_type_id', '=', $identityType->id);
236
        if ($isUnique->count() > 0) {
237
            $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...
238
            $this->_set_custom_stuff();
239
            return false;
240
        } else {
241
            return true;
242
        }
243
    }
244
245
    protected function IsBc($data, $value)
246
    {
247
        $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first();
248
        if ($identityType !== null) {
249
            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...
250
                return false;
251
            } else {
252
                return true;
253
            }
254
        } else {
255
            return true;
256
        }
257
    }
258
}
259