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