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\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) {
991 lars 69
            return new class($invokable) extends InvokableValidationRule implements ImplicitRule {};
148 lars 70
        }
71
 
72
        return new InvokableValidationRule($invokable);
73
    }
74
 
75
    /**
76
     * Determine if the validation rule passes.
77
     *
78
     * @param  string  $attribute
79
     * @param  mixed  $value
80
     * @return bool
81
     */
82
    public function passes($attribute, $value)
83
    {
84
        $this->failed = false;
85
 
86
        if ($this->invokable instanceof DataAwareRule) {
87
            $this->invokable->setData($this->validator->getData());
88
        }
89
 
90
        if ($this->invokable instanceof ValidatorAwareRule) {
91
            $this->invokable->setValidator($this->validator);
92
        }
93
 
94
        $this->invokable->__invoke($attribute, $value, function ($attribute, $message = null) {
95
            $this->failed = true;
96
 
97
            return $this->pendingPotentiallyTranslatedString($attribute, $message);
98
        });
99
 
100
        return ! $this->failed;
101
    }
102
 
103
    /**
104
     * Get the underlying invokable rule.
105
     *
106
     * @return \Illuminate\Contracts\Validation\InvokableRule
107
     */
108
    public function invokable()
109
    {
110
        return $this->invokable;
111
    }
112
 
113
    /**
114
     * Get the validation error messages.
115
     *
116
     * @return array
117
     */
118
    public function message()
119
    {
120
        return $this->messages;
121
    }
122
 
123
    /**
124
     * Set the data under validation.
125
     *
126
     * @param  array  $data
127
     * @return $this
128
     */
129
    public function setData($data)
130
    {
131
        $this->data = $data;
132
 
133
        return $this;
134
    }
135
 
136
    /**
137
     * Set the current validator.
138
     *
139
     * @param  \Illuminate\Validation\Validator  $validator
140
     * @return $this
141
     */
142
    public function setValidator($validator)
143
    {
144
        $this->validator = $validator;
145
 
146
        return $this;
147
    }
148
 
149
    /**
150
     * Create a pending potentially translated string.
151
     *
152
     * @param  string  $attribute
150 lars 153
     * @param  string|null  $message
148 lars 154
     * @return \Illuminate\Translation\PotentiallyTranslatedString
155
     */
156
    protected function pendingPotentiallyTranslatedString($attribute, $message)
157
    {
158
        $destructor = $message === null
159
            ? fn ($message) => $this->messages[] = $message
160
            : fn ($message) => $this->messages[$attribute] = $message;
161
 
162
        return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString
163
        {
164
            /**
165
             * The callback to call when the object destructs.
166
             *
167
             * @var \Closure
168
             */
169
            protected $destructor;
170
 
171
            /**
172
             * Create a new pending potentially translated string.
173
             *
174
             * @param  string  $message
175
             * @param  \Illuminate\Contracts\Translation\Translator  $translator
176
             * @param  \Closure  $destructor
177
             */
178
            public function __construct($message, $translator, $destructor)
179
            {
180
                parent::__construct($message, $translator);
181
 
182
                $this->destructor = $destructor;
183
            }
184
 
185
            /**
186
             * Handle the object's destruction.
187
             *
188
             * @return void
189
             */
190
            public function __destruct()
191
            {
192
                ($this->destructor)($this->toString());
193
            }
194
        };
195
    }
196
}