composer package updates
[openemr.git] / vendor / illuminate / support / MessageBag.php
blobda2c31e29dbe1bb80f01a1d12242e7774e9dd0b5
1 <?php
3 namespace Illuminate\Support;
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;
12 class MessageBag implements Arrayable, Countable, Jsonable, JsonSerializable, MessageBagContract, MessageProvider
14 /**
15 * All of the registered messages.
17 * @var array
19 protected $messages = [];
21 /**
22 * Default format for message output.
24 * @var string
26 protected $format = ':message';
28 /**
29 * Create a new message bag instance.
31 * @param array $messages
32 * @return void
34 public function __construct(array $messages = [])
36 foreach ($messages as $key => $value) {
37 $this->messages[$key] = $value instanceof Arrayable
38 ? $value->toArray() : (array) $value;
42 /**
43 * Get the keys present in the message bag.
45 * @return array
47 public function keys()
49 return array_keys($this->messages);
52 /**
53 * Add a message to the bag.
55 * @param string $key
56 * @param string $message
57 * @return $this
59 public function add($key, $message)
61 if ($this->isUnique($key, $message)) {
62 $this->messages[$key][] = $message;
65 return $this;
68 /**
69 * Determine if a key and message combination already exists.
71 * @param string $key
72 * @param string $message
73 * @return bool
75 protected function isUnique($key, $message)
77 $messages = (array) $this->messages;
79 return ! isset($messages[$key]) || ! in_array($message, $messages[$key]);
82 /**
83 * Merge a new array of messages into the bag.
85 * @param \Illuminate\Contracts\Support\MessageProvider|array $messages
86 * @return $this
88 public function merge($messages)
90 if ($messages instanceof MessageProvider) {
91 $messages = $messages->getMessageBag()->getMessages();
94 $this->messages = array_merge_recursive($this->messages, $messages);
96 return $this;
99 /**
100 * Determine if messages exist for all of the given keys.
102 * @param array|string $key
103 * @return bool
105 public function has($key)
107 if (is_null($key)) {
108 return $this->any();
111 $keys = is_array($key) ? $key : func_get_args();
113 foreach ($keys as $key) {
114 if ($this->first($key) === '') {
115 return false;
119 return true;
123 * Determine if messages exist for any of the given keys.
125 * @param array|string $keys
126 * @return bool
128 public function hasAny($keys = [])
130 $keys = is_array($keys) ? $keys : func_get_args();
132 foreach ($keys as $key) {
133 if ($this->has($key)) {
134 return true;
138 return false;
142 * Get the first message from the bag for a given key.
144 * @param string $key
145 * @param string $format
146 * @return string
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.
160 * @param string $key
161 * @param string $format
162 * @return array
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);
179 return [];
183 * Get the messages for a wildcard key.
185 * @param string $key
186 * @param string|null $format
187 * @return array
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
199 })->all();
203 * Get all of the messages for every key in the bag.
205 * @param string $format
206 * @return array
208 public function all($format = null)
210 $format = $this->checkFormat($format);
212 $all = [];
214 foreach ($this->messages as $key => $messages) {
215 $all = array_merge($all, $this->transform($messages, $format, $key));
218 return $all;
222 * Get all of the unique messages for every key in the bag.
224 * @param string $format
225 * @return array
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
238 * @return array
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);
248 })->all();
252 * Get the appropriate format based on the given format.
254 * @param string $format
255 * @return string
257 protected function checkFormat($format)
259 return $format ?: $this->format;
263 * Get the raw messages in the container.
265 * @return array
267 public function messages()
269 return $this->messages;
273 * Get the raw messages in the container.
275 * @return array
277 public function getMessages()
279 return $this->messages();
283 * Get the messages for the instance.
285 * @return \Illuminate\Support\MessageBag
287 public function getMessageBag()
289 return $this;
293 * Get the default message format.
295 * @return string
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;
312 return $this;
316 * Determine if the message bag has any messages.
318 * @return bool
320 public function isEmpty()
322 return ! $this->any();
326 * Determine if the message bag has any messages.
328 * @return bool
330 public function isNotEmpty()
332 return $this->any();
336 * Determine if the message bag has any messages.
338 * @return bool
340 public function any()
342 return $this->count() > 0;
346 * Get the number of messages in the container.
348 * @return int
350 public function count()
352 return count($this->messages, COUNT_RECURSIVE) - count($this->messages);
356 * Get the instance as an array.
358 * @return array
360 public function toArray()
362 return $this->getMessages();
366 * Convert the object into something JSON serializable.
368 * @return array
370 public function jsonSerialize()
372 return $this->toArray();
376 * Convert the object to its JSON representation.
378 * @param int $options
379 * @return string
381 public function toJson($options = 0)
383 return json_encode($this->jsonSerialize(), $options);
387 * Convert the message bag to its string representation.
389 * @return string
391 public function __toString()
393 return $this->toJson();