|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace App\Services\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
|
6
|
|
|
use Illuminate\Validation\Validator as BaseValidator; |
|
7
|
|
|
use Illuminate\Validation\Rule; |
|
8
|
|
|
use App\Models\Lookup\SkillLevel; |
|
9
|
|
|
use App\Models\Skill; |
|
10
|
|
|
use App\Models\Applicant; |
|
11
|
|
|
use App\Services\Validation\Rules\UniqueApplicantSkillRule; |
|
12
|
|
|
use App\Services\Validation\Rules\ApplicantHasRelationRule; |
|
13
|
|
|
|
|
14
|
|
|
class BulkSkillDeclarationValidator { |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
protected $applicant; |
|
17
|
|
|
protected $data; |
|
18
|
|
|
protected $rules = []; |
|
19
|
|
|
protected $messages = []; |
|
20
|
|
|
protected $customAttributes = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
|
|
|
|
|
23
|
|
|
* Create a new validator instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @param App\Models\Applicant $user |
|
|
|
|
|
|
26
|
|
|
* @return void |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Applicant $applicant, $data) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->applicant = $applicant; |
|
31
|
|
|
$this->data = $data; |
|
32
|
|
|
$this->initializeRules($data, $applicant); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function initializeRules($data, $applicant) { |
|
|
|
|
|
|
36
|
|
|
$rules = []; |
|
37
|
|
|
$customAttributes = []; |
|
38
|
|
|
|
|
39
|
|
|
$ages = ['new', 'old']; |
|
40
|
|
|
$types = ['soft', 'hard']; |
|
41
|
|
|
|
|
42
|
|
|
$skill_level_ids = SkillLevel::all()->pluck('id'); |
|
43
|
|
|
$skill_ids = Skill::all()->pluck('id'); |
|
44
|
|
|
$uniqueSkillRule = new UniqueApplicantSkillRule($applicant); |
|
45
|
|
|
$applicantHasSkillRule = new ApplicantHasRelationRule($applicant, 'skill_declarations'); |
|
46
|
|
|
|
|
47
|
|
|
if (isset($data['skill_declarations'])) { |
|
48
|
|
|
$skill_data = $data['skill_declarations']; |
|
49
|
|
|
foreach($ages as $age) { |
|
|
|
|
|
|
50
|
|
|
if (isset($skill_data[$age])) { |
|
51
|
|
|
$age_data = $skill_data[$age]; |
|
52
|
|
|
|
|
53
|
|
|
foreach($types as $type) { |
|
|
|
|
|
|
54
|
|
|
if (isset($age_data[$type])) { |
|
55
|
|
|
$type_data = $age_data[$type]; |
|
56
|
|
|
|
|
57
|
|
|
foreach($type_data as $id=>$declaration) { |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
//add description validation rule |
|
60
|
|
|
$description_field = implode('.', ['skill_declarations', $age, $type,$id,'description']); |
|
61
|
|
|
$description_rule = 'required|string'; |
|
62
|
|
|
$rules[$description_field] = $description_rule; |
|
63
|
|
|
$customAttributes[$description_field] = 'description'; |
|
64
|
|
|
|
|
65
|
|
|
//add skill_level validation rule |
|
66
|
|
|
$skill_level_field = implode('.', ['skill_declarations', $age, $type,$id,'skill_level_id']); |
|
67
|
|
|
$skill_level_rule = [ |
|
68
|
|
|
'required', |
|
69
|
|
|
Rule::in($skill_level_ids->toArray()), |
|
70
|
|
|
]; |
|
71
|
|
|
$rules[$skill_level_field] = $skill_level_rule; |
|
72
|
|
|
$customAttributes[$skill_level_field] = 'skill level'; |
|
73
|
|
|
|
|
74
|
|
|
//If age is new, add skill_id validation rule |
|
75
|
|
|
if ($age == 'new') { |
|
76
|
|
|
$skill_id_field = implode('.', ['skill_declarations', $age, $type,$id,'skill_id']); |
|
77
|
|
|
$skill_id_rule = [ |
|
78
|
|
|
'required', |
|
79
|
|
|
Rule::in($skill_ids->toArray()), |
|
80
|
|
|
$uniqueSkillRule, |
|
81
|
|
|
]; |
|
82
|
|
|
$rules[$skill_id_field] = $skill_id_rule; |
|
83
|
|
|
$customAttributes[$skill_id_field] = 'skill id'; |
|
84
|
|
|
} |
|
85
|
|
|
//If age is old, add skill_declaration_id validation rule |
|
86
|
|
|
else if ($age == 'old') { |
|
|
|
|
|
|
87
|
|
|
$skill_declaration_field = implode('.', ['skill_declarations', $age, $type,$id,'skill_declaration_id']); |
|
88
|
|
|
$skill_declaration_rule = [ |
|
89
|
|
|
'required', |
|
90
|
|
|
$applicantHasSkillRule, |
|
91
|
|
|
]; |
|
92
|
|
|
$rules[$skill_declaration_field] = $skill_declaration_rule; |
|
93
|
|
|
$customAttributes[$skill_declaration_field] = 'skill declaration id'; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->rules = $rules; |
|
103
|
|
|
$this->customAttributes = $customAttributes; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getRules() { |
|
|
|
|
|
|
107
|
|
|
return $this->rules; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function validate($data) { |
|
|
|
|
|
|
111
|
|
|
Validator::make($data, $this->getRules(), $this->messages, $this->customAttributes)->validate(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|