MessageBag.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. $this->messages[$key] = $value instanceof Arrayable
  33. ? $value->toArray() : (array) $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 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 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 (is_null($key)) {
  94. return $this->any();
  95. }
  96. $keys = is_array($key) ? $key : func_get_args();
  97. foreach ($keys as $key) {
  98. if ($this->first($key) === '') {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. /**
  105. * Determine if messages exist for any of the given keys.
  106. *
  107. * @param array|string $keys
  108. * @return bool
  109. */
  110. public function hasAny($keys = [])
  111. {
  112. $keys = is_array($keys) ? $keys : func_get_args();
  113. foreach ($keys as $key) {
  114. if ($this->has($key)) {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. /**
  121. * Get the first message from the bag for a given key.
  122. *
  123. * @param string $key
  124. * @param string $format
  125. * @return string
  126. */
  127. public function first($key = null, $format = null)
  128. {
  129. $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
  130. $firstMessage = Arr::first($messages, null, '');
  131. return is_array($firstMessage) ? Arr::first($firstMessage) : $firstMessage;
  132. }
  133. /**
  134. * Get all of the messages from the bag for a given key.
  135. *
  136. * @param string $key
  137. * @param string $format
  138. * @return array
  139. */
  140. public function get($key, $format = null)
  141. {
  142. // If the message exists in the container, we will transform it and return
  143. // the message. Otherwise, we'll check if the key is implicit & collect
  144. // all the messages that match a given key and output it as an array.
  145. if (array_key_exists($key, $this->messages)) {
  146. return $this->transform(
  147. $this->messages[$key], $this->checkFormat($format), $key
  148. );
  149. }
  150. if (Str::contains($key, '*')) {
  151. return $this->getMessagesForWildcardKey($key, $format);
  152. }
  153. return [];
  154. }
  155. /**
  156. * Get the messages for a wildcard key.
  157. *
  158. * @param string $key
  159. * @param string|null $format
  160. * @return array
  161. */
  162. protected function getMessagesForWildcardKey($key, $format)
  163. {
  164. return collect($this->messages)
  165. ->filter(function ($messages, $messageKey) use ($key) {
  166. return Str::is($key, $messageKey);
  167. })
  168. ->map(function ($messages, $messageKey) use ($format) {
  169. return $this->transform(
  170. $messages, $this->checkFormat($format), $messageKey
  171. );
  172. })->all();
  173. }
  174. /**
  175. * Get all of the messages for every key in the bag.
  176. *
  177. * @param string $format
  178. * @return array
  179. */
  180. public function all($format = null)
  181. {
  182. $format = $this->checkFormat($format);
  183. $all = [];
  184. foreach ($this->messages as $key => $messages) {
  185. $all = array_merge($all, $this->transform($messages, $format, $key));
  186. }
  187. return $all;
  188. }
  189. /**
  190. * Get all of the unique messages for every key in the bag.
  191. *
  192. * @param string $format
  193. * @return array
  194. */
  195. public function unique($format = null)
  196. {
  197. return array_unique($this->all($format));
  198. }
  199. /**
  200. * Format an array of messages.
  201. *
  202. * @param array $messages
  203. * @param string $format
  204. * @param string $messageKey
  205. * @return array
  206. */
  207. protected function transform($messages, $format, $messageKey)
  208. {
  209. return collect((array) $messages)
  210. ->map(function ($message) use ($format, $messageKey) {
  211. // We will simply spin through the given messages and transform each one
  212. // replacing the :message place holder with the real message allowing
  213. // the messages to be easily formatted to each developer's desires.
  214. return str_replace([':message', ':key'], [$message, $messageKey], $format);
  215. })->all();
  216. }
  217. /**
  218. * Get the appropriate format based on the given format.
  219. *
  220. * @param string $format
  221. * @return string
  222. */
  223. protected function checkFormat($format)
  224. {
  225. return $format ?: $this->format;
  226. }
  227. /**
  228. * Get the raw messages in the container.
  229. *
  230. * @return array
  231. */
  232. public function messages()
  233. {
  234. return $this->messages;
  235. }
  236. /**
  237. * Get the raw messages in the container.
  238. *
  239. * @return array
  240. */
  241. public function getMessages()
  242. {
  243. return $this->messages();
  244. }
  245. /**
  246. * Get the messages for the instance.
  247. *
  248. * @return \Illuminate\Support\MessageBag
  249. */
  250. public function getMessageBag()
  251. {
  252. return $this;
  253. }
  254. /**
  255. * Get the default message format.
  256. *
  257. * @return string
  258. */
  259. public function getFormat()
  260. {
  261. return $this->format;
  262. }
  263. /**
  264. * Set the default message format.
  265. *
  266. * @param string $format
  267. * @return \Illuminate\Support\MessageBag
  268. */
  269. public function setFormat($format = ':message')
  270. {
  271. $this->format = $format;
  272. return $this;
  273. }
  274. /**
  275. * Determine if the message bag has any messages.
  276. *
  277. * @return bool
  278. */
  279. public function isEmpty()
  280. {
  281. return ! $this->any();
  282. }
  283. /**
  284. * Determine if the message bag has any messages.
  285. *
  286. * @return bool
  287. */
  288. public function isNotEmpty()
  289. {
  290. return $this->any();
  291. }
  292. /**
  293. * Determine if the message bag has any messages.
  294. *
  295. * @return bool
  296. */
  297. public function any()
  298. {
  299. return $this->count() > 0;
  300. }
  301. /**
  302. * Get the number of messages in the container.
  303. *
  304. * @return int
  305. */
  306. public function count()
  307. {
  308. return count($this->messages, COUNT_RECURSIVE) - count($this->messages);
  309. }
  310. /**
  311. * Get the instance as an array.
  312. *
  313. * @return array
  314. */
  315. public function toArray()
  316. {
  317. return $this->getMessages();
  318. }
  319. /**
  320. * Convert the object into something JSON serializable.
  321. *
  322. * @return array
  323. */
  324. public function jsonSerialize()
  325. {
  326. return $this->toArray();
  327. }
  328. /**
  329. * Convert the object to its JSON representation.
  330. *
  331. * @param int $options
  332. * @return string
  333. */
  334. public function toJson($options = 0)
  335. {
  336. return json_encode($this->jsonSerialize(), $options);
  337. }
  338. /**
  339. * Convert the message bag to its string representation.
  340. *
  341. * @return string
  342. */
  343. public function __toString()
  344. {
  345. return $this->toJson();
  346. }
  347. }