You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageBag.php 9.2KB

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