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\Support;
4
 
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Contracts\Support\Jsonable;
7
use Illuminate\Contracts\Support\MessageBag as MessageBagContract;
8
use Illuminate\Contracts\Support\MessageProvider;
9
use JsonSerializable;
10
 
11
class MessageBag implements Jsonable, JsonSerializable, MessageBagContract, MessageProvider
12
{
13
    /**
14
     * All of the registered messages.
15
     *
16
     * @var array
17
     */
18
    protected $messages = [];
19
 
20
    /**
21
     * Default format for message output.
22
     *
23
     * @var string
24
     */
25
    protected $format = ':message';
26
 
27
    /**
28
     * Create a new message bag instance.
29
     *
30
     * @param  array  $messages
31
     * @return void
32
     */
33
    public function __construct(array $messages = [])
34
    {
35
        foreach ($messages as $key => $value) {
36
            $value = $value instanceof Arrayable ? $value->toArray() : (array) $value;
37
 
38
            $this->messages[$key] = array_unique($value);
39
        }
40
    }
41
 
42
    /**
43
     * Get the keys present in the message bag.
44
     *
45
     * @return array
46
     */
47
    public function keys()
48
    {
49
        return array_keys($this->messages);
50
    }
51
 
52
    /**
53
     * Add a message to the message bag.
54
     *
55
     * @param  string  $key
56
     * @param  string  $message
57
     * @return $this
58
     */
59
    public function add($key, $message)
60
    {
61
        if ($this->isUnique($key, $message)) {
62
            $this->messages[$key][] = $message;
63
        }
64
 
65
        return $this;
66
    }
67
 
68
    /**
69
     * Add a message to the message bag if the given conditional is "true".
70
     *
71
     * @param  bool  $boolean
72
     * @param  string  $key
73
     * @param  string  $message
74
     * @return $this
75
     */
76
    public function addIf($boolean, $key, $message)
77
    {
78
        return $boolean ? $this->add($key, $message) : $this;
79
    }
80
 
81
    /**
82
     * Determine if a key and message combination already exists.
83
     *
84
     * @param  string  $key
85
     * @param  string  $message
86
     * @return bool
87
     */
88
    protected function isUnique($key, $message)
89
    {
90
        $messages = (array) $this->messages;
91
 
92
        return ! isset($messages[$key]) || ! in_array($message, $messages[$key]);
93
    }
94
 
95
    /**
96
     * Merge a new array of messages into the message bag.
97
     *
98
     * @param  \Illuminate\Contracts\Support\MessageProvider|array  $messages
99
     * @return $this
100
     */
101
    public function merge($messages)
102
    {
103
        if ($messages instanceof MessageProvider) {
104
            $messages = $messages->getMessageBag()->getMessages();
105
        }
106
 
107
        $this->messages = array_merge_recursive($this->messages, $messages);
108
 
109
        return $this;
110
    }
111
 
112
    /**
113
     * Determine if messages exist for all of the given keys.
114
     *
115
     * @param  array|string|null  $key
116
     * @return bool
117
     */
118
    public function has($key)
119
    {
120
        if ($this->isEmpty()) {
121
            return false;
122
        }
123
 
124
        if (is_null($key)) {
125
            return $this->any();
126
        }
127
 
128
        $keys = is_array($key) ? $key : func_get_args();
129
 
130
        foreach ($keys as $key) {
131
            if ($this->first($key) === '') {
132
                return false;
133
            }
134
        }
135
 
136
        return true;
137
    }
138
 
139
    /**
140
     * Determine if messages exist for any of the given keys.
141
     *
142
     * @param  array|string|null  $keys
143
     * @return bool
144
     */
145
    public function hasAny($keys = [])
146
    {
147
        if ($this->isEmpty()) {
148
            return false;
149
        }
150
 
151
        $keys = is_array($keys) ? $keys : func_get_args();
152
 
153
        foreach ($keys as $key) {
154
            if ($this->has($key)) {
155
                return true;
156
            }
157
        }
158
 
159
        return false;
160
    }
161
 
162
    /**
163
     * Get the first message from the message bag for a given key.
164
     *
165
     * @param  string|null  $key
166
     * @param  string|null  $format
167
     * @return string
168
     */
169
    public function first($key = null, $format = null)
170
    {
171
        $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
172
 
173
        $firstMessage = Arr::first($messages, null, '');
174
 
175
        return is_array($firstMessage) ? Arr::first($firstMessage) : $firstMessage;
176
    }
177
 
178
    /**
179
     * Get all of the messages from the message bag for a given key.
180
     *
181
     * @param  string  $key
182
     * @param  string|null  $format
183
     * @return array
184
     */
185
    public function get($key, $format = null)
186
    {
187
        // If the message exists in the message bag, we will transform it and return
188
        // the message. Otherwise, we will check if the key is implicit & collect
189
        // all the messages that match the given key and output it as an array.
190
        if (array_key_exists($key, $this->messages)) {
191
            return $this->transform(
192
                $this->messages[$key], $this->checkFormat($format), $key
193
            );
194
        }
195
 
196
        if (str_contains($key, '*')) {
197
            return $this->getMessagesForWildcardKey($key, $format);
198
        }
199
 
200
        return [];
201
    }
202
 
203
    /**
204
     * Get the messages for a wildcard key.
205
     *
206
     * @param  string  $key
207
     * @param  string|null  $format
208
     * @return array
209
     */
210
    protected function getMessagesForWildcardKey($key, $format)
211
    {
212
        return collect($this->messages)
213
                ->filter(function ($messages, $messageKey) use ($key) {
214
                    return Str::is($key, $messageKey);
215
                })
216
                ->map(function ($messages, $messageKey) use ($format) {
217
                    return $this->transform(
218
                        $messages, $this->checkFormat($format), $messageKey
219
                    );
220
                })->all();
221
    }
222
 
223
    /**
224
     * Get all of the messages for every key in the message bag.
225
     *
226
     * @param  string|null  $format
227
     * @return array
228
     */
229
    public function all($format = null)
230
    {
231
        $format = $this->checkFormat($format);
232
 
233
        $all = [];
234
 
235
        foreach ($this->messages as $key => $messages) {
236
            $all = array_merge($all, $this->transform($messages, $format, $key));
237
        }
238
 
239
        return $all;
240
    }
241
 
242
    /**
243
     * Get all of the unique messages for every key in the message bag.
244
     *
245
     * @param  string|null  $format
246
     * @return array
247
     */
248
    public function unique($format = null)
249
    {
250
        return array_unique($this->all($format));
251
    }
252
 
253
    /**
254
     * Format an array of messages.
255
     *
256
     * @param  array  $messages
257
     * @param  string  $format
258
     * @param  string  $messageKey
259
     * @return array
260
     */
261
    protected function transform($messages, $format, $messageKey)
262
    {
263
        if ($format == ':message') {
264
            return (array) $messages;
265
        }
266
 
267
        return collect((array) $messages)
268
            ->map(function ($message) use ($format, $messageKey) {
269
                // We will simply spin through the given messages and transform each one
270
                // replacing the :message place holder with the real message allowing
271
                // the messages to be easily formatted to each developer's desires.
272
                return str_replace([':message', ':key'], [$message, $messageKey], $format);
273
            })->all();
274
    }
275
 
276
    /**
277
     * Get the appropriate format based on the given format.
278
     *
279
     * @param  string  $format
280
     * @return string
281
     */
282
    protected function checkFormat($format)
283
    {
284
        return $format ?: $this->format;
285
    }
286
 
287
    /**
288
     * Get the raw messages in the message bag.
289
     *
290
     * @return array
291
     */
292
    public function messages()
293
    {
294
        return $this->messages;
295
    }
296
 
297
    /**
298
     * Get the raw messages in the message bag.
299
     *
300
     * @return array
301
     */
302
    public function getMessages()
303
    {
304
        return $this->messages();
305
    }
306
 
307
    /**
308
     * Get the messages for the instance.
309
     *
310
     * @return \Illuminate\Support\MessageBag
311
     */
312
    public function getMessageBag()
313
    {
314
        return $this;
315
    }
316
 
317
    /**
318
     * Get the default message format.
319
     *
320
     * @return string
321
     */
322
    public function getFormat()
323
    {
324
        return $this->format;
325
    }
326
 
327
    /**
328
     * Set the default message format.
329
     *
330
     * @param  string  $format
331
     * @return \Illuminate\Support\MessageBag
332
     */
333
    public function setFormat($format = ':message')
334
    {
335
        $this->format = $format;
336
 
337
        return $this;
338
    }
339
 
340
    /**
341
     * Determine if the message bag has any messages.
342
     *
343
     * @return bool
344
     */
345
    public function isEmpty()
346
    {
347
        return ! $this->any();
348
    }
349
 
350
    /**
351
     * Determine if the message bag has any messages.
352
     *
353
     * @return bool
354
     */
355
    public function isNotEmpty()
356
    {
357
        return $this->any();
358
    }
359
 
360
    /**
361
     * Determine if the message bag has any messages.
362
     *
363
     * @return bool
364
     */
365
    public function any()
366
    {
367
        return $this->count() > 0;
368
    }
369
 
370
    /**
371
     * Get the number of messages in the message bag.
372
     *
373
     * @return int
374
     */
375
    public function count(): int
376
    {
377
        return count($this->messages, COUNT_RECURSIVE) - count($this->messages);
378
    }
379
 
380
    /**
381
     * Get the instance as an array.
382
     *
383
     * @return array
384
     */
385
    public function toArray()
386
    {
387
        return $this->getMessages();
388
    }
389
 
390
    /**
391
     * Convert the object into something JSON serializable.
392
     *
393
     * @return array
394
     */
395
    public function jsonSerialize(): array
396
    {
397
        return $this->toArray();
398
    }
399
 
400
    /**
401
     * Convert the object to its JSON representation.
402
     *
403
     * @param  int  $options
404
     * @return string
405
     */
406
    public function toJson($options = 0)
407
    {
408
        return json_encode($this->jsonSerialize(), $options);
409
    }
410
 
411
    /**
412
     * Convert the message bag to its string representation.
413
     *
414
     * @return string
415
     */
416
    public function __toString()
417
    {
418
        return $this->toJson();
419
    }
420
}