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