MessageBag.php 9.5 KB

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