MessageBag.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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|null $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. * Determine if messages don't exist for all of the given keys.
  139. *
  140. * @param array|string|null $key
  141. * @return bool
  142. */
  143. public function missing($key)
  144. {
  145. $keys = is_array($key) ? $key : func_get_args();
  146. return ! $this->hasAny($keys);
  147. }
  148. /**
  149. * Get the first message from the message bag for a given key.
  150. *
  151. * @param string|null $key
  152. * @param string|null $format
  153. * @return string
  154. */
  155. public function first($key = null, $format = null)
  156. {
  157. $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
  158. $firstMessage = Arr::first($messages, null, '');
  159. return is_array($firstMessage) ? Arr::first($firstMessage) : $firstMessage;
  160. }
  161. /**
  162. * Get all of the messages from the message bag for a given key.
  163. *
  164. * @param string $key
  165. * @param string|null $format
  166. * @return array
  167. */
  168. public function get($key, $format = null)
  169. {
  170. // If the message exists in the message bag, we will transform it and return
  171. // the message. Otherwise, we will check if the key is implicit & collect
  172. // all the messages that match the given key and output it as an array.
  173. if (array_key_exists($key, $this->messages)) {
  174. return $this->transform(
  175. $this->messages[$key], $this->checkFormat($format), $key
  176. );
  177. }
  178. if (str_contains($key, '*')) {
  179. return $this->getMessagesForWildcardKey($key, $format);
  180. }
  181. return [];
  182. }
  183. /**
  184. * Get the messages for a wildcard key.
  185. *
  186. * @param string $key
  187. * @param string|null $format
  188. * @return array
  189. */
  190. protected function getMessagesForWildcardKey($key, $format)
  191. {
  192. return collect($this->messages)
  193. ->filter(function ($messages, $messageKey) use ($key) {
  194. return Str::is($key, $messageKey);
  195. })
  196. ->map(function ($messages, $messageKey) use ($format) {
  197. return $this->transform(
  198. $messages, $this->checkFormat($format), $messageKey
  199. );
  200. })->all();
  201. }
  202. /**
  203. * Get all of the messages for every key in the message bag.
  204. *
  205. * @param string|null $format
  206. * @return array
  207. */
  208. public function all($format = null)
  209. {
  210. $format = $this->checkFormat($format);
  211. $all = [];
  212. foreach ($this->messages as $key => $messages) {
  213. $all = array_merge($all, $this->transform($messages, $format, $key));
  214. }
  215. return $all;
  216. }
  217. /**
  218. * Get all of the unique messages for every key in the message bag.
  219. *
  220. * @param string|null $format
  221. * @return array
  222. */
  223. public function unique($format = null)
  224. {
  225. return array_unique($this->all($format));
  226. }
  227. /**
  228. * Remove a message from the message bag.
  229. *
  230. * @param string $key
  231. * @return $this
  232. */
  233. public function forget($key)
  234. {
  235. unset($this->messages[$key]);
  236. return $this;
  237. }
  238. /**
  239. * Format an array of messages.
  240. *
  241. * @param array $messages
  242. * @param string $format
  243. * @param string $messageKey
  244. * @return array
  245. */
  246. protected function transform($messages, $format, $messageKey)
  247. {
  248. if ($format == ':message') {
  249. return (array) $messages;
  250. }
  251. return collect((array) $messages)
  252. ->map(function ($message) use ($format, $messageKey) {
  253. // We will simply spin through the given messages and transform each one
  254. // replacing the :message place holder with the real message allowing
  255. // the messages to be easily formatted to each developer's desires.
  256. return str_replace([':message', ':key'], [$message, $messageKey], $format);
  257. })->all();
  258. }
  259. /**
  260. * Get the appropriate format based on the given format.
  261. *
  262. * @param string $format
  263. * @return string
  264. */
  265. protected function checkFormat($format)
  266. {
  267. return $format ?: $this->format;
  268. }
  269. /**
  270. * Get the raw messages in the message bag.
  271. *
  272. * @return array
  273. */
  274. public function messages()
  275. {
  276. return $this->messages;
  277. }
  278. /**
  279. * Get the raw messages in the message bag.
  280. *
  281. * @return array
  282. */
  283. public function getMessages()
  284. {
  285. return $this->messages();
  286. }
  287. /**
  288. * Get the messages for the instance.
  289. *
  290. * @return \Illuminate\Support\MessageBag
  291. */
  292. public function getMessageBag()
  293. {
  294. return $this;
  295. }
  296. /**
  297. * Get the default message format.
  298. *
  299. * @return string
  300. */
  301. public function getFormat()
  302. {
  303. return $this->format;
  304. }
  305. /**
  306. * Set the default message format.
  307. *
  308. * @param string $format
  309. * @return \Illuminate\Support\MessageBag
  310. */
  311. public function setFormat($format = ':message')
  312. {
  313. $this->format = $format;
  314. return $this;
  315. }
  316. /**
  317. * Determine if the message bag has any messages.
  318. *
  319. * @return bool
  320. */
  321. public function isEmpty()
  322. {
  323. return ! $this->any();
  324. }
  325. /**
  326. * Determine if the message bag has any messages.
  327. *
  328. * @return bool
  329. */
  330. public function isNotEmpty()
  331. {
  332. return $this->any();
  333. }
  334. /**
  335. * Determine if the message bag has any messages.
  336. *
  337. * @return bool
  338. */
  339. public function any()
  340. {
  341. return $this->count() > 0;
  342. }
  343. /**
  344. * Get the number of messages in the message bag.
  345. *
  346. * @return int
  347. */
  348. public function count(): int
  349. {
  350. return count($this->messages, COUNT_RECURSIVE) - count($this->messages);
  351. }
  352. /**
  353. * Get the instance as an array.
  354. *
  355. * @return array
  356. */
  357. public function toArray()
  358. {
  359. return $this->getMessages();
  360. }
  361. /**
  362. * Convert the object into something JSON serializable.
  363. *
  364. * @return array
  365. */
  366. public function jsonSerialize(): array
  367. {
  368. return $this->toArray();
  369. }
  370. /**
  371. * Convert the object to its JSON representation.
  372. *
  373. * @param int $options
  374. * @return string
  375. */
  376. public function toJson($options = 0)
  377. {
  378. return json_encode($this->jsonSerialize(), $options);
  379. }
  380. /**
  381. * Convert the message bag to its string representation.
  382. *
  383. * @return string
  384. */
  385. public function __toString()
  386. {
  387. return $this->toJson();
  388. }
  389. }