Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Validation;
4
 
5
use Illuminate\Contracts\Validation\DataAwareRule;
6
use Illuminate\Contracts\Validation\ImplicitRule;
7
use Illuminate\Contracts\Validation\InvokableRule;
8
use Illuminate\Contracts\Validation\Rule;
9
use Illuminate\Contracts\Validation\ValidatorAwareRule;
10
use Illuminate\Translation\PotentiallyTranslatedString;
11
 
12
class InvokableValidationRule implements Rule, ValidatorAwareRule
13
{
14
    /**
15
     * The invokable that validates the attribute.
16
     *
17
     * @var \Illuminate\Contracts\Validation\InvokableRule
18
     */
19
    protected $invokable;
20
 
21
    /**
22
     * Indicates if the validation invokable failed.
23
     *
24
     * @var bool
25
     */
26
    protected $failed = false;
27
 
28
    /**
29
     * The validation error messages.
30
     *
31
     * @var array
32
     */
33
    protected $messages = [];
34
 
35
    /**
36
     * The current validator.
37
     *
38
     * @var \Illuminate\Validation\Validator
39
     */
40
    protected $validator;
41
 
42
    /**
43
     * The data under validation.
44
     *
45
     * @var array
46
     */
47
    protected $data = [];
48
 
49
    /**
50
     * Create a new explicit Invokable validation rule.
51
     *
52
     * @param  \Illuminate\Contracts\Validation\InvokableRule  $invokable
53
     * @return void
54
     */
55
    protected function __construct(InvokableRule $invokable)
56
    {
57
        $this->invokable = $invokable;
58
    }
59
 
60
    /**
61
     * Create a new implicit or explicit Invokable validation rule.
62
     *
63
     * @param  \Illuminate\Contracts\Validation\InvokableRule  $invokable
64
     * @return \Illuminate\Contracts\Validation\Rule
65
     */
66
    public static function make($invokable)
67
    {
68
        if ($invokable->implicit ?? false) {
69
            return new class($invokable) extends InvokableValidationRule implements ImplicitRule
70
            {
71
                //
72
            };
73
        }
74
 
75
        return new InvokableValidationRule($invokable);
76
    }
77
 
78
    /**
79
     * Determine if the validation rule passes.
80
     *
81
     * @param  string  $attribute
82
     * @param  mixed  $value
83
     * @return bool
84
     */
85
    public function passes($attribute, $value)
86
    {
87
        $this->failed = false;
88
 
89
        if ($this->invokable instanceof DataAwareRule) {
90
            $this->invokable->setData($this->validator->getData());
91
        }
92
 
93
        if ($this->invokable instanceof ValidatorAwareRule) {
94
            $this->invokable->setValidator($this->validator);
95
        }
96
 
97
        $this->invokable->__invoke($attribute, $value, function ($attribute, $message = null) {
98
            $this->failed = true;
99
 
100
            return $this->pendingPotentiallyTranslatedString($attribute, $message);
101
        });
102
 
103
        return ! $this->failed;
104
    }
105
 
106
    /**
107
     * Get the underlying invokable rule.
108
     *
109
     * @return \Illuminate\Contracts\Validation\InvokableRule
110
     */
111
    public function invokable()
112
    {
113
        return $this->invokable;
114
    }
115
 
116
    /**
117
     * Get the validation error messages.
118
     *
119
     * @return array
120
     */
121
    public function message()
122
    {
123
        return $this->messages;
124
    }
125
 
126
    /**
127
     * Set the data under validation.
128
     *
129
     * @param  array  $data
130
     * @return $this
131
     */
132
    public function setData($data)
133
    {
134
        $this->data = $data;
135
 
136
        return $this;
137
    }
138
 
139
    /**
140
     * Set the current validator.
141
     *
142
     * @param  \Illuminate\Validation\Validator  $validator
143
     * @return $this
144
     */
145
    public function setValidator($validator)
146
    {
147
        $this->validator = $validator;
148
 
149
        return $this;
150
    }
151
 
152
    /**
153
     * Create a pending potentially translated string.
154
     *
155
     * @param  string  $attribute
156
     * @param  ?string  $message
157
     * @return \Illuminate\Translation\PotentiallyTranslatedString
158
     */
159
    protected function pendingPotentiallyTranslatedString($attribute, $message)
160
    {
161
        $destructor = $message === null
162
            ? fn ($message) => $this->messages[] = $message
163
            : fn ($message) => $this->messages[$attribute] = $message;
164
 
165
        return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString
166
        {
167
            /**
168
             * The callback to call when the object destructs.
169
             *
170
             * @var \Closure
171
             */
172
            protected $destructor;
173
 
174
            /**
175
             * Create a new pending potentially translated string.
176
             *
177
             * @param  string  $message
178
             * @param  \Illuminate\Contracts\Translation\Translator  $translator
179
             * @param  \Closure  $destructor
180
             */
181
            public function __construct($message, $translator, $destructor)
182
            {
183
                parent::__construct($message, $translator);
184
 
185
                $this->destructor = $destructor;
186
            }
187
 
188
            /**
189
             * Handle the object's destruction.
190
             *
191
             * @return void
192
             */
193
            public function __destruct()
194
            {
195
                ($this->destructor)($this->toString());
196
            }
197
        };
198
    }
199
}