Debian Codegraph @

lib/3rdParty/composer/illuminate/support/MessageBag.php

raw symbols SCIP
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 use Stringable;
11
12 class MessageBag implements Jsonable, JsonSerializable, MessageBagContract, MessageProvider, Stringable
13 {
14 /**
15 * All of the registered messages.
16 *
17 * @var array<string, array<string>>
18 */
19 protected $messages = [];
20
21 /**
22 * Default format for message output.
23 *
24 * @var string
25 */
26 protected $format = ':message';
27
28 /**
29 * Create a new message bag instance.
30 *
31 * @param array<string, Arrayable|string|array<string>> $messages
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<string>
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<string, array<string>> $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 * Determine if messages don't exist for all of the given keys.
164 *
165 * @param array|string|null $key
166 * @return bool
167 */
168 public function missing($key)
169 {
170 $keys = is_array($key) ? $key : func_get_args();
171
172 return ! $this->hasAny($keys);
173 }
174
175 /**
176 * Get the first message from the message bag for a given key.
177 *
178 * @param string|null $key
179 * @param string|null $format
180 * @return string
181 */
182 public function first($key = null, $format = null)
183 {
184 $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
185
186 $firstMessage = Arr::first($messages, null, '');
187
188 return is_array($firstMessage) ? Arr::first($firstMessage) : $firstMessage;
189 }
190
191 /**
192 * Get all of the messages from the message bag for a given key.
193 *
194 * @param string $key
195 * @param string|null $format
196 * @return array<string>|array<string, array<string>>
197 */
198 public function get($key, $format = null)
199 {
200 // If the message exists in the message bag, we will transform it and return
201 // the message. Otherwise, we will check if the key is implicit & collect
202 // all the messages that match the given key and output it as an array.
203 if (array_key_exists($key, $this->messages)) {
204 return $this->transform(
205 $this->messages[$key], $this->checkFormat($format), $key
206 );
207 }
208
209 if (str_contains($key, '*')) {
210 return $this->getMessagesForWildcardKey($key, $format);
211 }
212
213 return [];
214 }
215
216 /**
217 * Get the messages for a wildcard key.
218 *
219 * @param string $key
220 * @param string|null $format
221 * @return array<string, array<string>>
222 */
223 protected function getMessagesForWildcardKey($key, $format)
224 {
225 return (new Collection($this->messages))
226 ->filter(fn ($messages, $messageKey) => Str::is($key, $messageKey))
227 ->map(function ($messages, $messageKey) use ($format) {
228 return $this->transform($messages, $this->checkFormat($format), $messageKey);
229 })
230 ->all();
231 }
232
233 /**
234 * Get all of the messages for every key in the message bag.
235 *
236 * @param string|null $format
237 * @return array<string>
238 */
239 public function all($format = null)
240 {
241 $format = $this->checkFormat($format);
242
243 $all = [];
244
245 foreach ($this->messages as $key => $messages) {
246 array_push($all, ...$this->transform($messages, $format, $key));
247 }
248
249 return $all;
250 }
251
252 /**
253 * Get all of the unique messages for every key in the message bag.
254 *
255 * @param string|null $format
256 * @return array
257 */
258 public function unique($format = null)
259 {
260 return array_unique($this->all($format));
261 }
262
263 /**
264 * Remove a message from the message bag.
265 *
266 * @param string $key
267 * @return $this
268 */
269 public function forget($key)
270 {
271 unset($this->messages[$key]);
272
273 return $this;
274 }
275
276 /**
277 * Format an array of messages.
278 *
279 * @param array<string> $messages
280 * @param string $format
281 * @param string $messageKey
282 * @return array<string>
283 */
284 protected function transform($messages, $format, $messageKey)
285 {
286 if ($format == ':message') {
287 return (array) $messages;
288 }
289
290 return (new Collection((array) $messages))
291 ->map(function ($message) use ($format, $messageKey) {
292 // We will simply spin through the given messages and transform each one
293 // replacing the :message place holder with the real message allowing
294 // the messages to be easily formatted to each developer's desires.
295 return str_replace([':message', ':key'], [$message, $messageKey], $format);
296 })->all();
297 }
298
299 /**
300 * Get the appropriate format based on the given format.
301 *
302 * @param string $format
303 * @return string
304 */
305 protected function checkFormat($format)
306 {
307 return $format ?: $this->format;
308 }
309
310 /**
311 * Get the raw messages in the message bag.
312 *
313 * @return array<string, array<string>>
314 */
315 public function messages()
316 {
317 return $this->messages;
318 }
319
320 /**
321 * Get the raw messages in the message bag.
322 *
323 * @return array<string, array<string>>
324 */
325 public function getMessages()
326 {
327 return $this->messages();
328 }
329
330 /**
331 * Get the messages for the instance.
332 *
333 * @return \Illuminate\Support\MessageBag
334 */
335 public function getMessageBag()
336 {
337 return $this;
338 }
339
340 /**
341 * Get the default message format.
342 *
343 * @return string
344 */
345 public function getFormat()
346 {
347 return $this->format;
348 }
349
350 /**
351 * Set the default message format.
352 *
353 * @param string $format
354 * @return \Illuminate\Support\MessageBag
355 */
356 public function setFormat($format = ':message')
357 {
358 $this->format = $format;
359
360 return $this;
361 }
362
363 /**
364 * Determine if the message bag has any messages.
365 *
366 * @return bool
367 */
368 public function isEmpty()
369 {
370 return ! $this->any();
371 }
372
373 /**
374 * Determine if the message bag has any messages.
375 *
376 * @return bool
377 */
378 public function isNotEmpty()
379 {
380 return $this->any();
381 }
382
383 /**
384 * Determine if the message bag has any messages.
385 *
386 * @return bool
387 */
388 public function any()
389 {
390 return $this->count() > 0;
391 }
392
393 /**
394 * Get the number of messages in the message bag.
395 *
396 * @return int
397 */
398 public function count(): int
399 {
400 return count($this->messages, COUNT_RECURSIVE) - count($this->messages);
401 }
402
403 /**
404 * Get the instance as an array.
405 *
406 * @return array
407 */
408 public function toArray()
409 {
410 return $this->getMessages();
411 }
412
413 /**
414 * Convert the object into something JSON serializable.
415 *
416 * @return array
417 */
418 public function jsonSerialize(): array
419 {
420 return $this->toArray();
421 }
422
423 /**
424 * Convert the object to its JSON representation.
425 *
426 * @param int $options
427 * @return string
428 */
429 public function toJson($options = 0)
430 {
431 return json_encode($this->jsonSerialize(), $options);
432 }
433
434 /**
435 * Convert the object to pretty print formatted JSON.
436 *
437 * @param int $options
438 * @return string
439 */
440 public function toPrettyJson(int $options = 0)
441 {
442 return $this->toJson(JSON_PRETTY_PRINT | $options);
443 }
444
445 /**
446 * Convert the message bag to its string representation.
447 *
448 * @return string
449 */
450 public function __toString()
451 {
452 return $this->toJson();
453 }
454 }