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