1
+ <?php
2
+
3
+ namespace App \Components \Validation ;
4
+
5
+ use Closure ;
6
+ use Illuminate \Support \Str ;
7
+ use Illuminate \Contracts \Container \Container ;
8
+ use Illuminate \Contracts \Translation \Translator ;
9
+ use Illuminate \Validation \PresenceVerifierInterface ;
10
+ use Illuminate \Contracts \Validation \Factory as FactoryContract ;
11
+
12
+ class Factory implements FactoryContract
13
+ {
14
+ /**
15
+ * The Translator implementation.
16
+ *
17
+ * @var \Illuminate\Contracts\Translation\Translator
18
+ */
19
+ protected $ translator ;
20
+
21
+ /**
22
+ * The Presence Verifier implementation.
23
+ *
24
+ * @var \Illuminate\Validation\PresenceVerifierInterface
25
+ */
26
+ protected $ verifier ;
27
+
28
+ /**
29
+ * The IoC container instance.
30
+ *
31
+ * @var \Illuminate\Contracts\Container\Container
32
+ */
33
+ protected $ container ;
34
+
35
+ /**
36
+ * All of the custom validator extensions.
37
+ *
38
+ * @var array
39
+ */
40
+ protected $ extensions = [];
41
+
42
+ /**
43
+ * All of the custom implicit validator extensions.
44
+ *
45
+ * @var array
46
+ */
47
+ protected $ implicitExtensions = [];
48
+
49
+ /**
50
+ * All of the custom dependent validator extensions.
51
+ *
52
+ * @var array
53
+ */
54
+ protected $ dependentExtensions = [];
55
+
56
+ /**
57
+ * All of the custom validator message replacers.
58
+ *
59
+ * @var array
60
+ */
61
+ protected $ replacers = [];
62
+
63
+ /**
64
+ * All of the fallback messages for custom rules.
65
+ *
66
+ * @var array
67
+ */
68
+ protected $ fallbackMessages = [];
69
+
70
+ /**
71
+ * The Validator resolver instance.
72
+ *
73
+ * @var Closure
74
+ */
75
+ protected $ resolver ;
76
+
77
+ /**
78
+ * Create a new Validator factory instance.
79
+ *
80
+ * @param \Illuminate\Contracts\Translation\Translator $translator
81
+ * @param \Illuminate\Contracts\Container\Container $container
82
+ * @return void
83
+ */
84
+ public function __construct (Translator $ translator , Container $ container = null )
85
+ {
86
+ $ this ->container = $ container ;
87
+ $ this ->translator = $ translator ;
88
+ }
89
+
90
+ /**
91
+ * Create a new Validator instance.
92
+ *
93
+ * @param array $data
94
+ * @param array $rules
95
+ * @param array $messages
96
+ * @param array $customAttributes
97
+ * @return \Illuminate\Validation\Validator
98
+ */
99
+ public function make (array $ data , array $ rules , array $ messages = [], array $ customAttributes = [])
100
+ {
101
+ // The presence verifier is responsible for checking the unique and exists data
102
+ // for the validator. It is behind an interface so that multiple versions of
103
+ // it may be written besides database. We'll inject it into the validator.
104
+ $ validator = $ this ->resolve (
105
+ $ data , $ rules , $ messages , $ customAttributes
106
+ );
107
+
108
+ if (! is_null ($ this ->verifier )) {
109
+ $ validator ->setPresenceVerifier ($ this ->verifier );
110
+ }
111
+
112
+ // Next we'll set the IoC container instance of the validator, which is used to
113
+ // resolve out class based validator extensions. If it is not set then these
114
+ // types of extensions will not be possible on these validation instances.
115
+ if (! is_null ($ this ->container )) {
116
+ $ validator ->setContainer ($ this ->container );
117
+ }
118
+
119
+ $ this ->addExtensions ($ validator );
120
+
121
+ return $ validator ;
122
+ }
123
+
124
+ /**
125
+ * Validate the given data against the provided rules.
126
+ *
127
+ * @param array $data
128
+ * @param array $rules
129
+ * @param array $messages
130
+ * @param array $customAttributes
131
+ * @return void
132
+ *
133
+ * @throws \Illuminate\Validation\ValidationException
134
+ */
135
+ public function validate (array $ data , array $ rules , array $ messages = [], array $ customAttributes = [])
136
+ {
137
+ $ this ->make ($ data , $ rules , $ messages , $ customAttributes )->validate ();
138
+ }
139
+
140
+ /**
141
+ * Resolve a new Validator instance.
142
+ *
143
+ * @param array $data
144
+ * @param array $rules
145
+ * @param array $messages
146
+ * @param array $customAttributes
147
+ * @return \Illuminate\Validation\Validator
148
+ */
149
+ protected function resolve (array $ data , array $ rules , array $ messages , array $ customAttributes )
150
+ {
151
+ if (is_null ($ this ->resolver )) {
152
+ return new Validator ($ this ->translator , $ data , $ rules , $ messages , $ customAttributes );
153
+ }
154
+
155
+ return call_user_func ($ this ->resolver , $ this ->translator , $ data , $ rules , $ messages , $ customAttributes );
156
+ }
157
+
158
+ /**
159
+ * Add the extensions to a validator instance.
160
+ *
161
+ * @param \App\Components\Validation\Validator $validator
162
+ * @return void
163
+ */
164
+ protected function addExtensions (Validator $ validator )
165
+ {
166
+ $ validator ->addExtensions ($ this ->extensions );
167
+
168
+ // Next, we will add the implicit extensions, which are similar to the required
169
+ // and accepted rule in that they are run even if the attributes is not in a
170
+ // array of data that is given to a validator instances via instantiation.
171
+ $ validator ->addImplicitExtensions ($ this ->implicitExtensions );
172
+
173
+ $ validator ->addDependentExtensions ($ this ->dependentExtensions );
174
+
175
+ $ validator ->addReplacers ($ this ->replacers );
176
+
177
+ $ validator ->setFallbackMessages ($ this ->fallbackMessages );
178
+ }
179
+
180
+ /**
181
+ * Register a custom validator extension.
182
+ *
183
+ * @param string $rule
184
+ * @param \Closure|string $extension
185
+ * @param string $message
186
+ * @return void
187
+ */
188
+ public function extend ($ rule , $ extension , $ message = null )
189
+ {
190
+ $ this ->extensions [$ rule ] = $ extension ;
191
+
192
+ if ($ message ) {
193
+ $ this ->fallbackMessages [Str::snake ($ rule )] = $ message ;
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Register a custom implicit validator extension.
199
+ *
200
+ * @param string $rule
201
+ * @param \Closure|string $extension
202
+ * @param string $message
203
+ * @return void
204
+ */
205
+ public function extendImplicit ($ rule , $ extension , $ message = null )
206
+ {
207
+ $ this ->implicitExtensions [$ rule ] = $ extension ;
208
+
209
+ if ($ message ) {
210
+ $ this ->fallbackMessages [Str::snake ($ rule )] = $ message ;
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Register a custom dependent validator extension.
216
+ *
217
+ * @param string $rule
218
+ * @param \Closure|string $extension
219
+ * @param string $message
220
+ * @return void
221
+ */
222
+ public function extendDependent ($ rule , $ extension , $ message = null )
223
+ {
224
+ $ this ->dependentExtensions [$ rule ] = $ extension ;
225
+
226
+ if ($ message ) {
227
+ $ this ->fallbackMessages [Str::snake ($ rule )] = $ message ;
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Register a custom validator message replacer.
233
+ *
234
+ * @param string $rule
235
+ * @param \Closure|string $replacer
236
+ * @return void
237
+ */
238
+ public function replacer ($ rule , $ replacer )
239
+ {
240
+ $ this ->replacers [$ rule ] = $ replacer ;
241
+ }
242
+
243
+ /**
244
+ * Set the Validator instance resolver.
245
+ *
246
+ * @param \Closure $resolver
247
+ * @return void
248
+ */
249
+ public function resolver (Closure $ resolver )
250
+ {
251
+ $ this ->resolver = $ resolver ;
252
+ }
253
+
254
+ /**
255
+ * Get the Translator implementation.
256
+ *
257
+ * @return \Illuminate\Contracts\Translation\Translator
258
+ */
259
+ public function getTranslator ()
260
+ {
261
+ return $ this ->translator ;
262
+ }
263
+
264
+ /**
265
+ * Get the Presence Verifier implementation.
266
+ *
267
+ * @return \Illuminate\Validation\PresenceVerifierInterface
268
+ */
269
+ public function getPresenceVerifier ()
270
+ {
271
+ return $ this ->verifier ;
272
+ }
273
+
274
+ /**
275
+ * Set the Presence Verifier implementation.
276
+ *
277
+ * @param \Illuminate\Validation\PresenceVerifierInterface $presenceVerifier
278
+ * @return void
279
+ */
280
+ public function setPresenceVerifier (PresenceVerifierInterface $ presenceVerifier )
281
+ {
282
+ $ this ->verifier = $ presenceVerifier ;
283
+ }
284
+ }
0 commit comments