MessageBag.php 10 KB

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