messages.php 3.9 KB

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