Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Validation;
4
 
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Facades\Validator as ValidatorFacade;
8
 
9
class ValidationException extends Exception
10
{
11
    /**
12
     * The validator instance.
13
     *
14
     * @var \Illuminate\Contracts\Validation\Validator
15
     */
16
    public $validator;
17
 
18
    /**
19
     * The recommended response to send to the client.
20
     *
21
     * @var \Symfony\Component\HttpFoundation\Response|null
22
     */
23
    public $response;
24
 
25
    /**
26
     * The status code to use for the response.
27
     *
28
     * @var int
29
     */
30
    public $status = 422;
31
 
32
    /**
33
     * The name of the error bag.
34
     *
35
     * @var string
36
     */
37
    public $errorBag;
38
 
39
    /**
40
     * The path the client should be redirected to.
41
     *
42
     * @var string
43
     */
44
    public $redirectTo;
45
 
46
    /**
47
     * Create a new exception instance.
48
     *
49
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
50
     * @param  \Symfony\Component\HttpFoundation\Response|null  $response
51
     * @param  string  $errorBag
52
     * @return void
53
     */
54
    public function __construct($validator, $response = null, $errorBag = 'default')
55
    {
56
        parent::__construct(static::summarize($validator));
57
 
58
        $this->response = $response;
59
        $this->errorBag = $errorBag;
60
        $this->validator = $validator;
61
    }
62
 
63
    /**
64
     * Create a new validation exception from a plain array of messages.
65
     *
66
     * @param  array  $messages
67
     * @return static
68
     */
69
    public static function withMessages(array $messages)
70
    {
71
        return new static(tap(ValidatorFacade::make([], []), function ($validator) use ($messages) {
72
            foreach ($messages as $key => $value) {
73
                foreach (Arr::wrap($value) as $message) {
74
                    $validator->errors()->add($key, $message);
75
                }
76
            }
77
        }));
78
    }
79
 
80
    /**
81
     * Create an error message summary from the validation errors.
82
     *
83
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
84
     * @return string
85
     */
86
    protected static function summarize($validator)
87
    {
88
        $messages = $validator->errors()->all();
89
 
90
        if (! count($messages) || ! is_string($messages[0])) {
91
            return $validator->getTranslator()->get('The given data was invalid.');
92
        }
93
 
94
        $message = array_shift($messages);
95
 
96
        if ($count = count($messages)) {
97
            $pluralized = $count === 1 ? 'error' : 'errors';
98
 
99
            $message .= ' '.$validator->getTranslator()->get("(and :count more $pluralized)", compact('count'));
100
        }
101
 
102
        return $message;
103
    }
104
 
105
    /**
106
     * Get all of the validation error messages.
107
     *
108
     * @return array
109
     */
110
    public function errors()
111
    {
112
        return $this->validator->errors()->messages();
113
    }
114
 
115
    /**
116
     * Set the HTTP status code to be used for the response.
117
     *
118
     * @param  int  $status
119
     * @return $this
120
     */
121
    public function status($status)
122
    {
123
        $this->status = $status;
124
 
125
        return $this;
126
    }
127
 
128
    /**
129
     * Set the error bag on the exception.
130
     *
131
     * @param  string  $errorBag
132
     * @return $this
133
     */
134
    public function errorBag($errorBag)
135
    {
136
        $this->errorBag = $errorBag;
137
 
138
        return $this;
139
    }
140
 
141
    /**
142
     * Set the URL to redirect to on a validation error.
143
     *
144
     * @param  string  $url
145
     * @return $this
146
     */
147
    public function redirectTo($url)
148
    {
149
        $this->redirectTo = $url;
150
 
151
        return $this;
152
    }
153
 
154
    /**
155
     * Get the underlying response instance.
156
     *
157
     * @return \Symfony\Component\HttpFoundation\Response|null
158
     */
159
    public function getResponse()
160
    {
161
        return $this->response;
162
    }
163
}