3 namespace Illuminate\Support
;
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
;
12 class MessageBag
implements Arrayable
, Countable
, Jsonable
, JsonSerializable
, MessageBagContract
, MessageProvider
15 * All of the registered messages.
19 protected $messages = [];
22 * Default format for message output.
26 protected $format = ':message';
29 * Create a new message bag instance.
31 * @param array $messages
34 public function __construct(array $messages = [])
36 foreach ($messages as $key => $value) {
37 $this->messages
[$key] = $value instanceof Arrayable
38 ?
$value->toArray() : (array) $value;
43 * Get the keys present in the message bag.
47 public function keys()
49 return array_keys($this->messages
);
53 * Add a message to the bag.
56 * @param string $message
59 public function add($key, $message)
61 if ($this->isUnique($key, $message)) {
62 $this->messages
[$key][] = $message;
69 * Determine if a key and message combination already exists.
72 * @param string $message
75 protected function isUnique($key, $message)
77 $messages = (array) $this->messages
;
79 return ! isset($messages[$key]) ||
! in_array($message, $messages[$key]);
83 * Merge a new array of messages into the bag.
85 * @param \Illuminate\Contracts\Support\MessageProvider|array $messages
88 public function merge($messages)
90 if ($messages instanceof MessageProvider
) {
91 $messages = $messages->getMessageBag()->getMessages();
94 $this->messages
= array_merge_recursive($this->messages
, $messages);
100 * Determine if messages exist for all of the given keys.
102 * @param array|string $key
105 public function has($key)
111 $keys = is_array($key) ?
$key : func_get_args();
113 foreach ($keys as $key) {
114 if ($this->first($key) === '') {
123 * Determine if messages exist for any of the given keys.
125 * @param array|string $keys
128 public function hasAny($keys = [])
130 $keys = is_array($keys) ?
$keys : func_get_args();
132 foreach ($keys as $key) {
133 if ($this->has($key)) {
142 * Get the first message from the bag for a given key.
145 * @param string $format
148 public function first($key = null, $format = null)
150 $messages = is_null($key) ?
$this->all($format) : $this->get($key, $format);
152 $firstMessage = Arr
::first($messages, null, '');
154 return is_array($firstMessage) ? Arr
::first($firstMessage) : $firstMessage;
158 * Get all of the messages from the bag for a given key.
161 * @param string $format
164 public function get($key, $format = null)
166 // If the message exists in the container, we will transform it and return
167 // the message. Otherwise, we'll check if the key is implicit & collect
168 // all the messages that match a given key and output it as an array.
169 if (array_key_exists($key, $this->messages
)) {
170 return $this->transform(
171 $this->messages
[$key], $this->checkFormat($format), $key
175 if (Str
::contains($key, '*')) {
176 return $this->getMessagesForWildcardKey($key, $format);
183 * Get the messages for a wildcard key.
186 * @param string|null $format
189 protected function getMessagesForWildcardKey($key, $format)
191 return collect($this->messages
)
192 ->filter(function ($messages, $messageKey) use ($key) {
193 return Str
::is($key, $messageKey);
195 ->map(function ($messages, $messageKey) use ($format) {
196 return $this->transform(
197 $messages, $this->checkFormat($format), $messageKey
203 * Get all of the messages for every key in the bag.
205 * @param string $format
208 public function all($format = null)
210 $format = $this->checkFormat($format);
214 foreach ($this->messages
as $key => $messages) {
215 $all = array_merge($all, $this->transform($messages, $format, $key));
222 * Get all of the unique messages for every key in the bag.
224 * @param string $format
227 public function unique($format = null)
229 return array_unique($this->all($format));
233 * Format an array of messages.
235 * @param array $messages
236 * @param string $format
237 * @param string $messageKey
240 protected function transform($messages, $format, $messageKey)
242 return collect((array) $messages)
243 ->map(function ($message) use ($format, $messageKey) {
244 // We will simply spin through the given messages and transform each one
245 // replacing the :message place holder with the real message allowing
246 // the messages to be easily formatted to each developer's desires.
247 return str_replace([':message', ':key'], [$message, $messageKey], $format);
252 * Get the appropriate format based on the given format.
254 * @param string $format
257 protected function checkFormat($format)
259 return $format ?
: $this->format
;
263 * Get the raw messages in the container.
267 public function messages()
269 return $this->messages
;
273 * Get the raw messages in the container.
277 public function getMessages()
279 return $this->messages();
283 * Get the messages for the instance.
285 * @return \Illuminate\Support\MessageBag
287 public function getMessageBag()
293 * Get the default message format.
297 public function getFormat()
299 return $this->format
;
303 * Set the default message format.
305 * @param string $format
306 * @return \Illuminate\Support\MessageBag
308 public function setFormat($format = ':message')
310 $this->format
= $format;
316 * Determine if the message bag has any messages.
320 public function isEmpty()
322 return ! $this->any();
326 * Determine if the message bag has any messages.
330 public function isNotEmpty()
336 * Determine if the message bag has any messages.
340 public function any()
342 return $this->count() > 0;
346 * Get the number of messages in the container.
350 public function count()
352 return count($this->messages
, COUNT_RECURSIVE
) - count($this->messages
);
356 * Get the instance as an array.
360 public function toArray()
362 return $this->getMessages();
366 * Convert the object into something JSON serializable.
370 public function jsonSerialize()
372 return $this->toArray();
376 * Convert the object to its JSON representation.
378 * @param int $options
381 public function toJson($options = 0)
383 return json_encode($this->jsonSerialize(), $options);
387 * Convert the message bag to its string representation.
391 public function __toString()
393 return $this->toJson();