PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Front_End/vendor/laravel/framework/src/Illuminate/Support/MessageBag.php

https://gitlab.com/Sigpot/AirSpot
PHP | 359 lines | 152 code | 46 blank | 161 comment | 7 complexity | 7c3084943de361e862aa38c695428f66 MD5 | raw file
  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] = (array) $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 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. * Merge a new array of messages into the bag.
  60. *
  61. * @param \Illuminate\Contracts\Support\MessageProvider|array $messages
  62. * @return $this
  63. */
  64. public function merge($messages)
  65. {
  66. if ($messages instanceof MessageProvider) {
  67. $messages = $messages->getMessageBag()->getMessages();
  68. }
  69. $this->messages = array_merge_recursive($this->messages, $messages);
  70. return $this;
  71. }
  72. /**
  73. * Determine if a key and message combination already exists.
  74. *
  75. * @param string $key
  76. * @param string $message
  77. * @return bool
  78. */
  79. protected function isUnique($key, $message)
  80. {
  81. $messages = (array) $this->messages;
  82. return ! isset($messages[$key]) || ! in_array($message, $messages[$key]);
  83. }
  84. /**
  85. * Determine if messages exist for all of the given keys.
  86. *
  87. * @param array|string $key
  88. * @return bool
  89. */
  90. public function has($key = null)
  91. {
  92. if (is_null($key)) {
  93. return $this->any();
  94. }
  95. $keys = is_array($key) ? $key : func_get_args();
  96. foreach ($keys as $key) {
  97. if ($this->first($key) === '') {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. /**
  104. * Determine if messages exist for any of the given keys.
  105. *
  106. * @param array $keys
  107. * @return bool
  108. */
  109. public function hasAny($keys = [])
  110. {
  111. foreach ($keys as $key) {
  112. if ($this->has($key)) {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. /**
  119. * Get the first message from the bag for a given key.
  120. *
  121. * @param string $key
  122. * @param string $format
  123. * @return string
  124. */
  125. public function first($key = null, $format = null)
  126. {
  127. $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
  128. return count($messages) > 0 ? $messages[0] : '';
  129. }
  130. /**
  131. * Get all of the messages from the bag for a given key.
  132. *
  133. * @param string $key
  134. * @param string $format
  135. * @return array
  136. */
  137. public function get($key, $format = null)
  138. {
  139. // If the message exists in the container, we will transform it and return
  140. // the message. Otherwise, we'll return an empty array since the entire
  141. // methods is to return back an array of messages in the first place.
  142. if (array_key_exists($key, $this->messages)) {
  143. return $this->transform($this->messages[$key], $this->checkFormat($format), $key);
  144. }
  145. return [];
  146. }
  147. /**
  148. * Get all of the messages for every key in the bag.
  149. *
  150. * @param string $format
  151. * @return array
  152. */
  153. public function all($format = null)
  154. {
  155. $format = $this->checkFormat($format);
  156. $all = [];
  157. foreach ($this->messages as $key => $messages) {
  158. $all = array_merge($all, $this->transform($messages, $format, $key));
  159. }
  160. return $all;
  161. }
  162. /**
  163. * Get all of the unique messages for every key in the bag.
  164. *
  165. * @param string $format
  166. * @return array
  167. */
  168. public function unique($format = null)
  169. {
  170. return array_unique($this->all($format));
  171. }
  172. /**
  173. * Format an array of messages.
  174. *
  175. * @param array $messages
  176. * @param string $format
  177. * @param string $messageKey
  178. * @return array
  179. */
  180. protected function transform($messages, $format, $messageKey)
  181. {
  182. $messages = (array) $messages;
  183. // We will simply spin through the given messages and transform each one
  184. // replacing the :message place holder with the real message allowing
  185. // the messages to be easily formatted to each developer's desires.
  186. $replace = [':message', ':key'];
  187. foreach ($messages as &$message) {
  188. $message = str_replace($replace, [$message, $messageKey], $format);
  189. }
  190. return $messages;
  191. }
  192. /**
  193. * Get the appropriate format based on the given format.
  194. *
  195. * @param string $format
  196. * @return string
  197. */
  198. protected function checkFormat($format)
  199. {
  200. return $format ?: $this->format;
  201. }
  202. /**
  203. * Get the raw messages in the container.
  204. *
  205. * @return array
  206. */
  207. public function messages()
  208. {
  209. return $this->messages;
  210. }
  211. /**
  212. * Get the raw messages in the container.
  213. *
  214. * @return array
  215. */
  216. public function getMessages()
  217. {
  218. return $this->messages();
  219. }
  220. /**
  221. * Get the messages for the instance.
  222. *
  223. * @return \Illuminate\Support\MessageBag
  224. */
  225. public function getMessageBag()
  226. {
  227. return $this;
  228. }
  229. /**
  230. * Get the default message format.
  231. *
  232. * @return string
  233. */
  234. public function getFormat()
  235. {
  236. return $this->format;
  237. }
  238. /**
  239. * Set the default message format.
  240. *
  241. * @param string $format
  242. * @return \Illuminate\Support\MessageBag
  243. */
  244. public function setFormat($format = ':message')
  245. {
  246. $this->format = $format;
  247. return $this;
  248. }
  249. /**
  250. * Determine if the message bag has any messages.
  251. *
  252. * @return bool
  253. */
  254. public function isEmpty()
  255. {
  256. return ! $this->any();
  257. }
  258. /**
  259. * Determine if the message bag has any messages.
  260. *
  261. * @return bool
  262. */
  263. public function any()
  264. {
  265. return $this->count() > 0;
  266. }
  267. /**
  268. * Get the number of messages in the container.
  269. *
  270. * @return int
  271. */
  272. public function count()
  273. {
  274. return count($this->messages, COUNT_RECURSIVE) - count($this->messages);
  275. }
  276. /**
  277. * Get the instance as an array.
  278. *
  279. * @return array
  280. */
  281. public function toArray()
  282. {
  283. return $this->getMessages();
  284. }
  285. /**
  286. * Convert the object into something JSON serializable.
  287. *
  288. * @return array
  289. */
  290. public function jsonSerialize()
  291. {
  292. return $this->toArray();
  293. }
  294. /**
  295. * Convert the object to its JSON representation.
  296. *
  297. * @param int $options
  298. * @return string
  299. */
  300. public function toJson($options = 0)
  301. {
  302. return json_encode($this->jsonSerialize(), $options);
  303. }
  304. /**
  305. * Convert the message bag to its string representation.
  306. *
  307. * @return string
  308. */
  309. public function __toString()
  310. {
  311. return $this->toJson();
  312. }
  313. }