MessageBag.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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<string, array<string>>
  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<string, Arrayable|string|array<string>> $messages
  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<string>
  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<string, array<string>> $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<string>|array<string, array<string>>
  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<string, array<string>>
  189. */
  190. protected function getMessagesForWildcardKey($key, $format)
  191. {
  192. return (new Collection($this->messages))
  193. ->filter(fn ($messages, $messageKey) => Str::is($key, $messageKey))
  194. ->map(function ($messages, $messageKey) use ($format) {
  195. return $this->transform($messages, $this->checkFormat($format), $messageKey);
  196. })
  197. ->all();
  198. }
  199. /**
  200. * Get all of the messages for every key in the message bag.
  201. *
  202. * @param string|null $format
  203. * @return array<string>
  204. */
  205. public function all($format = null)
  206. {
  207. $format = $this->checkFormat($format);
  208. $all = [];
  209. foreach ($this->messages as $key => $messages) {
  210. $all = array_merge($all, $this->transform($messages, $format, $key));
  211. }
  212. return $all;
  213. }
  214. /**
  215. * Get all of the unique messages for every key in the message bag.
  216. *
  217. * @param string|null $format
  218. * @return array
  219. */
  220. public function unique($format = null)
  221. {
  222. return array_unique($this->all($format));
  223. }
  224. /**
  225. * Remove a message from the message bag.
  226. *
  227. * @param string $key
  228. * @return $this
  229. */
  230. public function forget($key)
  231. {
  232. unset($this->messages[$key]);
  233. return $this;
  234. }
  235. /**
  236. * Format an array of messages.
  237. *
  238. * @param array<string> $messages
  239. * @param string $format
  240. * @param string $messageKey
  241. * @return array<string>
  242. */
  243. protected function transform($messages, $format, $messageKey)
  244. {
  245. if ($format == ':message') {
  246. return (array) $messages;
  247. }
  248. return (new Collection((array) $messages))
  249. ->map(function ($message) use ($format, $messageKey) {
  250. // We will simply spin through the given messages and transform each one
  251. // replacing the :message place holder with the real message allowing
  252. // the messages to be easily formatted to each developer's desires.
  253. return str_replace([':message', ':key'], [$message, $messageKey], $format);
  254. })->all();
  255. }
  256. /**
  257. * Get the appropriate format based on the given format.
  258. *
  259. * @param string $format
  260. * @return string
  261. */
  262. protected function checkFormat($format)
  263. {
  264. return $format ?: $this->format;
  265. }
  266. /**
  267. * Get the raw messages in the message bag.
  268. *
  269. * @return array<string, array<string>>
  270. */
  271. public function messages()
  272. {
  273. return $this->messages;
  274. }
  275. /**
  276. * Get the raw messages in the message bag.
  277. *
  278. * @return array<string, array<string>>
  279. */
  280. public function getMessages()
  281. {
  282. return $this->messages();
  283. }
  284. /**
  285. * Get the messages for the instance.
  286. *
  287. * @return \Illuminate\Support\MessageBag
  288. */
  289. public function getMessageBag()
  290. {
  291. return $this;
  292. }
  293. /**
  294. * Get the default message format.
  295. *
  296. * @return string
  297. */
  298. public function getFormat()
  299. {
  300. return $this->format;
  301. }
  302. /**
  303. * Set the default message format.
  304. *
  305. * @param string $format
  306. * @return \Illuminate\Support\MessageBag
  307. */
  308. public function setFormat($format = ':message')
  309. {
  310. $this->format = $format;
  311. return $this;
  312. }
  313. /**
  314. * Determine if the message bag has any messages.
  315. *
  316. * @return bool
  317. */
  318. public function isEmpty()
  319. {
  320. return ! $this->any();
  321. }
  322. /**
  323. * Determine if the message bag has any messages.
  324. *
  325. * @return bool
  326. */
  327. public function isNotEmpty()
  328. {
  329. return $this->any();
  330. }
  331. /**
  332. * Determine if the message bag has any messages.
  333. *
  334. * @return bool
  335. */
  336. public function any()
  337. {
  338. return $this->count() > 0;
  339. }
  340. /**
  341. * Get the number of messages in the message bag.
  342. *
  343. * @return int
  344. */
  345. public function count(): int
  346. {
  347. return count($this->messages, COUNT_RECURSIVE) - count($this->messages);
  348. }
  349. /**
  350. * Get the instance as an array.
  351. *
  352. * @return array
  353. */
  354. public function toArray()
  355. {
  356. return $this->getMessages();
  357. }
  358. /**
  359. * Convert the object into something JSON serializable.
  360. *
  361. * @return array
  362. */
  363. public function jsonSerialize(): array
  364. {
  365. return $this->toArray();
  366. }
  367. /**
  368. * Convert the object to its JSON representation.
  369. *
  370. * @param int $options
  371. * @return string
  372. */
  373. public function toJson($options = 0)
  374. {
  375. return json_encode($this->jsonSerialize(), $options);
  376. }
  377. /**
  378. * Convert the object to pretty print formatted JSON.
  379. *
  380. * @params int $options
  381. *
  382. * @return string
  383. */
  384. public function toPrettyJson(int $options = 0)
  385. {
  386. return $this->toJson(JSON_PRETTY_PRINT | $options);
  387. }
  388. /**
  389. * Convert the message bag to its string representation.
  390. *
  391. * @return string
  392. */
  393. public function __toString()
  394. {
  395. return $this->toJson();
  396. }
  397. }