Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Http;
4
 
5
use Illuminate\Auth\Access\AuthorizationException;
6
use Illuminate\Auth\Access\Response;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
9
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
10
use Illuminate\Contracts\Validation\Validator;
11
use Illuminate\Http\Request;
12
use Illuminate\Routing\Redirector;
13
use Illuminate\Validation\ValidatesWhenResolvedTrait;
14
use Illuminate\Validation\ValidationException;
15
 
16
class FormRequest extends Request implements ValidatesWhenResolved
17
{
18
    use ValidatesWhenResolvedTrait;
19
 
20
    /**
21
     * The container instance.
22
     *
23
     * @var \Illuminate\Contracts\Container\Container
24
     */
25
    protected $container;
26
 
27
    /**
28
     * The redirector instance.
29
     *
30
     * @var \Illuminate\Routing\Redirector
31
     */
32
    protected $redirector;
33
 
34
    /**
35
     * The URI to redirect to if validation fails.
36
     *
37
     * @var string
38
     */
39
    protected $redirect;
40
 
41
    /**
42
     * The route to redirect to if validation fails.
43
     *
44
     * @var string
45
     */
46
    protected $redirectRoute;
47
 
48
    /**
49
     * The controller action to redirect to if validation fails.
50
     *
51
     * @var string
52
     */
53
    protected $redirectAction;
54
 
55
    /**
56
     * The key to be used for the view error bag.
57
     *
58
     * @var string
59
     */
60
    protected $errorBag = 'default';
61
 
62
    /**
63
     * Indicates whether validation should stop after the first rule failure.
64
     *
65
     * @var bool
66
     */
67
    protected $stopOnFirstFailure = false;
68
 
69
    /**
70
     * The validator instance.
71
     *
72
     * @var \Illuminate\Contracts\Validation\Validator
73
     */
74
    protected $validator;
75
 
76
    /**
77
     * Get the validator instance for the request.
78
     *
79
     * @return \Illuminate\Contracts\Validation\Validator
80
     */
81
    protected function getValidatorInstance()
82
    {
83
        if ($this->validator) {
84
            return $this->validator;
85
        }
86
 
87
        $factory = $this->container->make(ValidationFactory::class);
88
 
89
        if (method_exists($this, 'validator')) {
90
            $validator = $this->container->call([$this, 'validator'], compact('factory'));
91
        } else {
92
            $validator = $this->createDefaultValidator($factory);
93
        }
94
 
95
        if (method_exists($this, 'withValidator')) {
96
            $this->withValidator($validator);
97
        }
98
 
99
        $this->setValidator($validator);
100
 
101
        return $this->validator;
102
    }
103
 
104
    /**
105
     * Create the default validator instance.
106
     *
107
     * @param  \Illuminate\Contracts\Validation\Factory  $factory
108
     * @return \Illuminate\Contracts\Validation\Validator
109
     */
110
    protected function createDefaultValidator(ValidationFactory $factory)
111
    {
1663 lars 112
        $rules = method_exists($this, 'rules') ? $this->container->call([$this, 'rules']) : [];
148 lars 113
 
114
        $validator = $factory->make(
115
            $this->validationData(), $rules,
116
            $this->messages(), $this->attributes()
117
        )->stopOnFirstFailure($this->stopOnFirstFailure);
118
 
119
        if ($this->isPrecognitive()) {
120
            $validator->setRules(
121
                $this->filterPrecognitiveRules($validator->getRulesWithoutPlaceholders())
122
            );
123
        }
124
 
125
        return $validator;
126
    }
127
 
128
    /**
129
     * Get data to be validated from the request.
130
     *
131
     * @return array
132
     */
133
    public function validationData()
134
    {
135
        return $this->all();
136
    }
137
 
138
    /**
139
     * Handle a failed validation attempt.
140
     *
141
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
142
     * @return void
143
     *
144
     * @throws \Illuminate\Validation\ValidationException
145
     */
146
    protected function failedValidation(Validator $validator)
147
    {
148
        throw (new ValidationException($validator))
149
                    ->errorBag($this->errorBag)
150
                    ->redirectTo($this->getRedirectUrl());
151
    }
152
 
153
    /**
154
     * Get the URL to redirect to on a validation error.
155
     *
156
     * @return string
157
     */
158
    protected function getRedirectUrl()
159
    {
160
        $url = $this->redirector->getUrlGenerator();
161
 
162
        if ($this->redirect) {
163
            return $url->to($this->redirect);
164
        } elseif ($this->redirectRoute) {
165
            return $url->route($this->redirectRoute);
166
        } elseif ($this->redirectAction) {
167
            return $url->action($this->redirectAction);
168
        }
169
 
170
        return $url->previous();
171
    }
172
 
173
    /**
174
     * Determine if the request passes the authorization check.
175
     *
176
     * @return bool
177
     *
178
     * @throws \Illuminate\Auth\Access\AuthorizationException
179
     */
180
    protected function passesAuthorization()
181
    {
182
        if (method_exists($this, 'authorize')) {
183
            $result = $this->container->call([$this, 'authorize']);
184
 
185
            return $result instanceof Response ? $result->authorize() : $result;
186
        }
187
 
188
        return true;
189
    }
190
 
191
    /**
192
     * Handle a failed authorization attempt.
193
     *
194
     * @return void
195
     *
196
     * @throws \Illuminate\Auth\Access\AuthorizationException
197
     */
198
    protected function failedAuthorization()
199
    {
200
        throw new AuthorizationException;
201
    }
202
 
203
    /**
204
     * Get a validated input container for the validated input.
205
     *
206
     * @param  array|null  $keys
207
     * @return \Illuminate\Support\ValidatedInput|array
208
     */
209
    public function safe(array $keys = null)
210
    {
211
        return is_array($keys)
212
                    ? $this->validator->safe()->only($keys)
213
                    : $this->validator->safe();
214
    }
215
 
216
    /**
217
     * Get the validated data from the request.
218
     *
150 lars 219
     * @param  array|int|string|null  $key
148 lars 220
     * @param  mixed  $default
221
     * @return mixed
222
     */
223
    public function validated($key = null, $default = null)
224
    {
225
        return data_get($this->validator->validated(), $key, $default);
226
    }
227
 
228
    /**
229
     * Get custom messages for validator errors.
230
     *
231
     * @return array
232
     */
233
    public function messages()
234
    {
235
        return [];
236
    }
237
 
238
    /**
239
     * Get custom attributes for validator errors.
240
     *
241
     * @return array
242
     */
243
    public function attributes()
244
    {
245
        return [];
246
    }
247
 
248
    /**
249
     * Set the Validator instance.
250
     *
251
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
252
     * @return $this
253
     */
254
    public function setValidator(Validator $validator)
255
    {
256
        $this->validator = $validator;
257
 
258
        return $this;
259
    }
260
 
261
    /**
262
     * Set the Redirector instance.
263
     *
264
     * @param  \Illuminate\Routing\Redirector  $redirector
265
     * @return $this
266
     */
267
    public function setRedirector(Redirector $redirector)
268
    {
269
        $this->redirector = $redirector;
270
 
271
        return $this;
272
    }
273
 
274
    /**
275
     * Set the container implementation.
276
     *
277
     * @param  \Illuminate\Contracts\Container\Container  $container
278
     * @return $this
279
     */
280
    public function setContainer(Container $container)
281
    {
282
        $this->container = $container;
283
 
284
        return $this;
285
    }
286
}