|
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\VeteranStatus; |
|
11
|
|
|
use App\Models\Lookup\PreferredLanguage; |
|
12
|
|
|
use App\Services\Validation\Rules\ContainsObjectWithAttributeRule; |
|
13
|
|
|
use App\Services\Validation\JobApplicationAnswerValidator; |
|
14
|
|
|
|
|
15
|
|
|
class ApplicationValidator { |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
protected $citizenship_ids; |
|
18
|
|
|
protected $veteran_status_ids; |
|
19
|
|
|
protected $preferred_language_ids; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() { |
|
|
|
|
|
|
22
|
|
|
$this->citizenship_ids = CitizenshipDeclaration::all()->pluck('id')->toArray(); |
|
23
|
|
|
$this->veteran_status_ids = VeteranStatus::all()->pluck('id')->toArray(); |
|
24
|
|
|
$this->preferred_language_ids = PreferredLanguage::all()->pluck('id')->toArray(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function validate(JobApplication $application) { |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$rules = [ |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
//Validate basic data is filled in |
|
34
|
|
|
Validator::make($application->getAttributes(), [ |
|
|
|
|
|
|
35
|
|
|
'job_poster_id' => 'required', |
|
36
|
|
|
'application_status_id' => 'required', |
|
37
|
|
|
'citizenship_declaration_id' => ['required', Rule::in($this->citizenship_ids)], |
|
38
|
|
|
'veteran_status_id' => ['required', Rule::in($this->veteran_status_ids)], |
|
39
|
|
|
'preferred_language_id' => ['required', Rule::in($this->preferred_language_ids)], |
|
40
|
|
|
'applicant_id' => 'required', |
|
41
|
|
|
'submission_signature' => 'required|string|max:191', |
|
42
|
|
|
'submission_date' => 'required|string|max:191', |
|
43
|
|
|
])->validate(); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
//TODO |
|
46
|
|
|
//Validate that all questions have been answered |
|
47
|
|
|
|
|
48
|
|
|
//TODO |
|
49
|
|
|
//Validate that essential skill declarations have been supplied |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function arrayMapKeys($fn, $array) { |
|
|
|
|
|
|
53
|
|
|
$newArray = []; |
|
54
|
|
|
foreach($array as $key => $value) { |
|
|
|
|
|
|
55
|
|
|
$newArray[$fn($key)] = $value; |
|
56
|
|
|
} |
|
57
|
|
|
return $newArray; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function basicsValidator(JobApplication $application) { |
|
|
|
|
|
|
61
|
|
|
// Validate the fields common to every application |
|
62
|
|
|
$rules = [ |
|
63
|
|
|
'citizenship_declaration_id' => ['required', Rule::in($this->citizenship_ids)], |
|
64
|
|
|
'veteran_status_id' => ['required', Rule::in($this->veteran_status_ids)], |
|
65
|
|
|
'preferred_language_id' => ['required', Rule::in($this->preferred_language_ids)], |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
//Load application answers so they are included in application->toArray() |
|
69
|
|
|
$application->load('job_application_answers'); |
|
70
|
|
|
|
|
71
|
|
|
// Validate that each question has been answered |
|
72
|
|
|
$jobPosterQuestionRules = []; |
|
73
|
|
|
foreach($application->job_poster->job_poster_questions as $question) { |
|
|
|
|
|
|
74
|
|
|
$jobPosterQuestionRules[] = new ContainsObjectWithAttributeRule('job_poster_question_id', $question->id); |
|
75
|
|
|
} |
|
76
|
|
|
$rules['job_application_answers'] = $jobPosterQuestionRules; |
|
77
|
|
|
$answerValidator = new JobApplicationAnswerValidator($application); |
|
78
|
|
|
|
|
79
|
|
|
//Validate that each answer is complete |
|
80
|
|
|
foreach($application->job_application_answers as $key=>$answer) { |
|
|
|
|
|
|
81
|
|
|
$attribute = implode('.', ['job_application_answers', $key]); |
|
82
|
|
|
$newRules = $this->arrayMapKeys(function($key) use ($attribute) { |
|
|
|
|
|
|
83
|
|
|
return implode('.', [$attribute, $key]); |
|
84
|
|
|
}, |
|
|
|
|
|
|
85
|
|
|
$answerValidator->rules()); |
|
|
|
|
|
|
86
|
|
|
debugbar()->debug($newRules); |
|
87
|
|
|
$rules = array_merge($rules, $newRules); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$validator = Validator::make($application->toArray(), $rules); |
|
91
|
|
|
debugbar()->debug($application->toArray()); |
|
92
|
|
|
debugbar()->debug($validator->getRules()); |
|
93
|
|
|
return $validator; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function basicsComplete(JobApplication $application) { |
|
|
|
|
|
|
97
|
|
|
$validator = $this->basicsValidator($application); |
|
98
|
|
|
debugbar()->debug($validator->messages()); |
|
99
|
|
|
return $validator->passes(); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|