1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Services; |
4
|
|
|
|
5
|
|
|
use Akibatech\Crud\Exceptions\FailedValidationException; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\Validator; |
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CrudValidator |
12
|
|
|
* |
13
|
|
|
* @package Akibatech\Crud\Services |
14
|
|
|
*/ |
15
|
|
|
class CrudValidator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CrudEntry |
19
|
|
|
*/ |
20
|
|
|
protected $entry; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Validator |
24
|
|
|
*/ |
25
|
|
|
protected $validator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $form_data; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* CrudValidator constructor. |
34
|
|
|
* |
35
|
|
|
* @param void |
36
|
|
|
*/ |
37
|
|
|
public function __construct(CrudEntry $entry) |
38
|
|
|
{ |
39
|
|
|
$this->setEntry($entry); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Defines the CrudEntry to validate. |
44
|
|
|
* |
45
|
|
|
* @param CrudEntry $entry |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function setEntry(CrudEntry $entry) |
49
|
|
|
{ |
50
|
|
|
$this->entry = $entry; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Gets the entry. |
57
|
|
|
* |
58
|
|
|
* @param void |
59
|
|
|
* @return CrudEntry |
60
|
|
|
*/ |
61
|
|
|
public function getEntry() |
62
|
|
|
{ |
63
|
|
|
return $this->entry; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $data |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
|
|
public function setFormData(array $data) |
71
|
|
|
{ |
72
|
|
|
$this->form_data = $data; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Request $request |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
|
|
public function validateRequest(Request $request) |
82
|
|
|
{ |
83
|
|
|
return $this->validate($request->only($this->getEntry()->getFields()->keys())); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Validate given array of data against fields rules. |
88
|
|
|
* |
89
|
|
|
* @param array $fields_data |
90
|
|
|
* @return self |
91
|
|
|
*/ |
92
|
|
|
public function validate(array $fields_data) |
93
|
|
|
{ |
94
|
|
|
$rules = $this->getEntry()->getFields()->validationRules(); |
95
|
|
|
$this->setFormData($fields_data); |
96
|
|
|
|
97
|
|
|
$this->validator = Validator::make($fields_data, $rules); |
98
|
|
|
$this->validator = $this->getEntry()->getFields()->contextualValidationRules($this->validator); |
|
|
|
|
99
|
|
|
$this->validator->fails(); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param void |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function fails() |
109
|
|
|
{ |
110
|
|
|
return count($this->validator->failed()) === 0 ? false : true; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param void |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function passes() |
118
|
|
|
{ |
119
|
|
|
return $this->validator->fails() === false; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param void |
124
|
|
|
* @return RedirectResponse |
125
|
|
|
*/ |
126
|
|
|
public function redirect() |
127
|
|
|
{ |
128
|
|
|
if ($this->fails()) |
129
|
|
|
{ |
130
|
|
|
return redirect()->back()->withInput()->withErrors($this->validator); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return redirect($this->entry->getManager()->getActionRoute('index')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param void |
138
|
|
|
* @return bool |
139
|
|
|
* @throws FailedValidationException |
140
|
|
|
*/ |
141
|
|
|
public function save() |
142
|
|
|
{ |
143
|
|
|
if ($this->fails()) |
144
|
|
|
{ |
145
|
|
|
throw new FailedValidationException("Entry has failed its validation and can't be saved."); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->entry->getFields()->hydrateFormData($this->form_data); |
149
|
|
|
|
150
|
|
|
return $this->entry->save(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param void |
155
|
|
|
* @return self |
156
|
|
|
*/ |
157
|
|
|
protected function resetValidator() |
158
|
|
|
{ |
159
|
|
|
$this->validator = null; |
160
|
|
|
$this->form_data = null; |
|
|
|
|
161
|
|
|
$this->getEntry()->resetValidator(); |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param void |
168
|
|
|
* @return Validator |
169
|
|
|
*/ |
170
|
|
|
public function getValidator() |
171
|
|
|
{ |
172
|
|
|
return $this->validator; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..