messages.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php namespace Laravel;
  2. class Messages {
  3. /**
  4. * All of the registered messages.
  5. *
  6. * @var array
  7. */
  8. public $messages;
  9. /**
  10. * Create a new Messages instance.
  11. *
  12. * @param array $messages
  13. * @return void
  14. */
  15. public function __construct($messages = array())
  16. {
  17. $this->messages = (array) $messages;
  18. }
  19. /**
  20. * Add a message to the collector.
  21. *
  22. * <code>
  23. * // Add a message for the e-mail attribute
  24. * $messages->add('email', 'The e-mail address is invalid.');
  25. * </code>
  26. *
  27. * @param string $key
  28. * @param string $message
  29. * @return void
  30. */
  31. public function add($key, $message)
  32. {
  33. if ($this->unique($key, $message)) $this->messages[$key][] = $message;
  34. }
  35. /**
  36. * Determine if a key and message combination already exists.
  37. *
  38. * @param string $key
  39. * @param string $message
  40. * @return bool
  41. */
  42. protected function unique($key, $message)
  43. {
  44. return ! isset($this->messages[$key]) or ! in_array($message, $this->messages[$key]);
  45. }
  46. /**
  47. * Determine if messages exist for a given key.
  48. *
  49. * <code>
  50. * // Is there a message for the e-mail attribute
  51. * return $messages->has('email');
  52. *
  53. * // Is there a message for the any attribute
  54. * echo $messages->has();
  55. * </code>
  56. *
  57. * @param string $key
  58. * @return bool
  59. */
  60. public function has($key = null)
  61. {
  62. return $this->first($key) !== '';
  63. }
  64. /**
  65. * Get the first message from the container for a given key.
  66. *
  67. * <code>
  68. * // Echo the first message out of all messages.
  69. * echo $messages->first();
  70. *
  71. * // Echo the first message for the e-mail attribute
  72. * echo $messages->first('email');
  73. *
  74. * // Format the first message for the e-mail attribute
  75. * echo $messages->first('email', '<p>:message</p>');
  76. * </code>
  77. *
  78. * @param string $key
  79. * @param string $format
  80. * @return string
  81. */
  82. public function first($key = null, $format = ':message')
  83. {
  84. $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
  85. return (count($messages) > 0) ? $messages[0] : '';
  86. }
  87. /**
  88. * Get all of the messages from the container for a given key.
  89. *
  90. * <code>
  91. * // Echo all of the messages for the e-mail attribute
  92. * echo $messages->get('email');
  93. *
  94. * // Format all of the messages for the e-mail attribute
  95. * echo $messages->get('email', '<p>:message</p>');
  96. * </code>
  97. *
  98. * @param string $key
  99. * @param string $format
  100. * @return array
  101. */
  102. public function get($key, $format = ':message')
  103. {
  104. if (array_key_exists($key, $this->messages))
  105. {
  106. return $this->format($this->messages[$key], $format);
  107. }
  108. return array();
  109. }
  110. /**
  111. * Get all of the messages for every key in the container.
  112. *
  113. * <code>
  114. * // Get all of the messages in the collector
  115. * $all = $messages->all();
  116. *
  117. * // Format all of the messages in the collector
  118. * $all = $messages->all('<p>:message</p>');
  119. * </code>
  120. *
  121. * @param string $format
  122. * @return array
  123. */
  124. public function all($format = ':message')
  125. {
  126. $all = array();
  127. foreach ($this->messages as $messages)
  128. {
  129. $all = array_merge($all, $this->format($messages, $format));
  130. }
  131. return $all;
  132. }
  133. /**
  134. * Format an array of messages.
  135. *
  136. * @param array $messages
  137. * @param string $format
  138. * @return array
  139. */
  140. protected function format($messages, $format)
  141. {
  142. $messages = (array) $messages;
  143. foreach ($messages as $key => &$message)
  144. {
  145. $message = str_replace(':message', $message, $format);
  146. }
  147. return $messages;
  148. }
  149. }