1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace App\Services\Validation; |
4
|
|
|
|
5
|
|
|
use App\Models\JobApplication; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use Illuminate\Validation\Rule; |
8
|
|
|
use Illuminate\Validation\Validator as BaseValidator; |
9
|
|
|
use App\Models\Lookup\CitizenshipDeclaration; |
10
|
|
|
use App\Models\Lookup\CriteriaType; |
11
|
|
|
use App\Models\Lookup\VeteranStatus; |
12
|
|
|
use App\Models\Lookup\PreferredLanguage; |
13
|
|
|
use App\Services\Validation\Rules\ContainsObjectWithAttributeRule; |
14
|
|
|
use App\Services\Validation\JobApplicationAnswerValidator; |
15
|
|
|
|
16
|
|
|
class ApplicationValidator { |
|
|
|
|
17
|
|
|
|
18
|
|
|
protected $citizenship_ids; |
19
|
|
|
protected $veteran_status_ids; |
20
|
|
|
protected $preferred_language_ids; |
21
|
|
|
|
22
|
|
|
public function __construct() { |
|
|
|
|
23
|
|
|
$this->citizenship_ids = CitizenshipDeclaration::all()->pluck('id')->toArray(); |
24
|
|
|
$this->veteran_status_ids = VeteranStatus::all()->pluck('id')->toArray(); |
25
|
|
|
$this->preferred_language_ids = PreferredLanguage::all()->pluck('id')->toArray(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function validate(JobApplication $application) { |
|
|
|
|
29
|
|
|
|
30
|
|
|
$backendRules = [ |
31
|
|
|
'job_poster_id' => 'required', |
32
|
|
|
'application_status_id' => 'required', |
33
|
|
|
'applicant_id' => 'required' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
$rules = array_merge( |
37
|
|
|
$backendRules, |
38
|
|
|
//$this->basicsValidator($application)->getRules(), |
39
|
|
|
$this->experienceValidator($application)->getRules(), |
40
|
|
|
//$this->essentialSkillsValidator($application)->getRules(), |
41
|
|
|
$this->affirmationValidator($application)->getRules() |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$data = $application->toArray(); |
45
|
|
|
|
46
|
|
|
// Combining and simplifiying error messages |
47
|
|
|
$rules = array_merge( |
48
|
|
|
$rules, |
49
|
|
|
['application_step_1' => 'required|boolean|accepted'], |
50
|
|
|
['application_step_3' => 'required|boolean|accepted'] |
51
|
|
|
); |
52
|
|
|
$data = array_merge( |
53
|
|
|
$data, |
54
|
|
|
['application_step_1' => $this->basicsComplete($application)], |
55
|
|
|
['application_step_3' => $this->essentialSkillsComplete($application)] |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
//Validate basic data is filled in |
59
|
|
|
Validator::make($data, $rules)->validate(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return a copy of $array, with function $fn applied to each key, but values left unchanged. |
64
|
|
|
* |
65
|
|
|
* @param function $fn Function applied to each key. |
|
|
|
|
66
|
|
|
* @param array $array Array to operate on. |
|
|
|
|
67
|
|
|
* @return array |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
protected function arrayMapKeys($fn, $array): array |
70
|
|
|
{ |
71
|
|
|
$newArray = []; |
72
|
|
|
foreach ($array as $key => $value) { |
73
|
|
|
$newArray[$fn($key)] = $value; |
74
|
|
|
} |
75
|
|
|
return $newArray; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function addNestedValidatorRules($nestedAttribute, $validatorRules, $rules = []) { |
|
|
|
|
79
|
|
|
// prepend the attribute name of each validator rule with the nested attribute name |
80
|
|
|
$newRules = $this->arrayMapKeys(function($key) use ($nestedAttribute) { |
|
|
|
|
81
|
|
|
return implode('.', [$nestedAttribute, $key]); |
82
|
|
|
}, |
|
|
|
|
83
|
|
|
$validatorRules); |
|
|
|
|
84
|
|
|
//Merge new rules with old rules |
85
|
|
|
$rules = array_merge($rules, $newRules); |
86
|
|
|
return $rules; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function basicsValidator(JobApplication $application) { |
|
|
|
|
90
|
|
|
// Validate the fields common to every application |
91
|
|
|
$rules = [ |
92
|
|
|
'citizenship_declaration_id' => ['required', Rule::in($this->citizenship_ids)], |
93
|
|
|
'veteran_status_id' => ['required', Rule::in($this->veteran_status_ids)], |
94
|
|
|
'preferred_language_id' => ['required', Rule::in($this->preferred_language_ids)], |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
//Load application answers so they are included in application->toArray() |
98
|
|
|
$application->load('job_application_answers'); |
99
|
|
|
|
100
|
|
|
// Validate that each question has been answered |
101
|
|
|
$jobPosterQuestionRules = []; |
102
|
|
|
foreach($application->job_poster->job_poster_questions as $question) { |
|
|
|
|
103
|
|
|
$jobPosterQuestionRules[] = new ContainsObjectWithAttributeRule('job_poster_question_id', $question->id); |
104
|
|
|
} |
105
|
|
|
$rules['job_application_answers'] = $jobPosterQuestionRules; |
106
|
|
|
$answerValidatorFactory = new JobApplicationAnswerValidator($application); |
107
|
|
|
|
108
|
|
|
//Validate that each answer is complete |
109
|
|
|
foreach($application->job_application_answers as $key=>$answer) { |
|
|
|
|
110
|
|
|
$attribute = implode('.', ['job_application_answers', $key]); |
111
|
|
|
$rules = $this->addNestedValidatorRules($attribute, $answerValidatorFactory->rules(), $rules); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$validator = Validator::make($application->toArray(), $rules); |
115
|
|
|
return $validator; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function basicsComplete(JobApplication $application) { |
|
|
|
|
119
|
|
|
$validator = $this->basicsValidator($application); |
120
|
|
|
return $validator->passes(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function experienceValidator(JobApplication $application) { |
|
|
|
|
124
|
|
|
$rules = ['experience_saved' => 'required|boolean|accepted']; |
125
|
|
|
return Validator::make($application->toArray(), $rules); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function experienceComplete(JobApplication $application) { |
|
|
|
|
129
|
|
|
return $this->experienceValidator($application)->passes(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function skillsValidator(JobApplication $application, $criteria_type) { |
|
|
|
|
133
|
|
|
$rules = []; |
134
|
|
|
|
135
|
|
|
$skillDeclarationRules = []; |
136
|
|
|
$criteriaTypeId = CriteriaType::where('name', $criteria_type)->firstOrFail()->id; |
137
|
|
|
foreach($application->job_poster->criteria->where('criteria_type_id', $criteriaTypeId) as $criteria) { |
|
|
|
|
138
|
|
|
//Validate that every essential skill has a corresponding declaration |
139
|
|
|
$skillDeclarationRules[] = new ContainsObjectWithAttributeRule('skill_id', $criteria->skill_id); |
140
|
|
|
} |
141
|
|
|
$rules['skill_declarations'] = $skillDeclarationRules; |
142
|
|
|
$application->applicant->load('skill_declarations'); |
143
|
|
|
|
144
|
|
|
//Validate that those declarations are complete |
145
|
|
|
$skilDeclarationValidatorFactory = new SkillDeclarationValidator($application->applicant); |
146
|
|
|
$relevantSkillIds = $application->job_poster->criteria->where('criteria_type_id', $criteriaTypeId)->pluck('skill_id'); |
147
|
|
|
foreach( $application->skill_declarations as $key=>$declaration) { |
|
|
|
|
148
|
|
|
if ($relevantSkillIds->contains($declaration->skill_id)) { |
149
|
|
|
$attribute = implode('.', ['skill_declarations', $key]); |
150
|
|
|
$skillDeclarationValidator = $skilDeclarationValidatorFactory->validator($declaration); |
151
|
|
|
$rules = $this->addNestedValidatorRules($attribute, $skillDeclarationValidator->getRules(), $rules); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$validator = Validator::make($application->toArray(), $rules); |
156
|
|
|
return $validator; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function essentialSkillsValidator(JobApplication $application) { |
|
|
|
|
160
|
|
|
return $this->skillsValidator($application, 'essential'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function essentialSkillsComplete(JobApplication $application) { |
|
|
|
|
164
|
|
|
return $this->essentialSkillsValidator($application)->passes(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function assetSkillsValidator(JobApplication $application) { |
|
|
|
|
168
|
|
|
return $this->skillsValidator($application, 'asset'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function assetSkillsComplete(JobApplication $application) { |
|
|
|
|
172
|
|
|
return $this->assetSkillsValidator($application)->passes(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function affirmationValidator(JobApplication $application) { |
|
|
|
|
176
|
|
|
$rules = [ |
177
|
|
|
'submission_signature' => [ |
178
|
|
|
'required', |
179
|
|
|
'string', |
180
|
|
|
'max:191', |
181
|
|
|
], |
182
|
|
|
'submission_date' => [ |
183
|
|
|
'required', |
184
|
|
|
'string', |
185
|
|
|
'max:191', |
186
|
|
|
] |
187
|
|
|
]; |
188
|
|
|
return Validator::make($application->toArray(), $rules); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function affirmationComplete(JobApplication $application) { |
|
|
|
|
192
|
|
|
return $this->affirmationValidator($application)->passes(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|