messages.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * @param string $key
  50. * @return bool
  51. */
  52. public function has($key)
  53. {
  54. return $this->first($key) !== '';
  55. }
  56. /**
  57. * Get the first message from the container for a given key.
  58. *
  59. * <code>
  60. * // Echo the first message out of all messages.
  61. * echo $messages->first();
  62. *
  63. * // Echo the first message for the e-mail attribute
  64. * echo $messages->first('email');
  65. *
  66. * // Format the first message for the e-mail attribute
  67. * echo $messages->first('email', '<p>:message</p>');
  68. * </code>
  69. *
  70. * @param string $key
  71. * @param string $format
  72. * @return string
  73. */
  74. public function first($key = null, $format = ':message')
  75. {
  76. $messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
  77. return (count($messages) > 0) ? $messages[0] : '';
  78. }
  79. /**
  80. * Get all of the messages from the container for a given key.
  81. *
  82. * <code>
  83. * // Echo all of the messages for the e-mail attribute
  84. * echo $messages->get('email');
  85. *
  86. * // Format all of the messages for the e-mail attribute
  87. * echo $messages->get('email', '<p>:message</p>');
  88. * </code>
  89. *
  90. * @param string $key
  91. * @param string $format
  92. * @return array
  93. */
  94. public function get($key, $format = ':message')
  95. {
  96. if (array_key_exists($key, $this->messages))
  97. {
  98. return $this->format($this->messages[$key], $format);
  99. }
  100. return array();
  101. }
  102. /**
  103. * Get all of the messages for every key in the container.
  104. *
  105. * <code>
  106. * // Get all of the messages in the collector
  107. * $all = $messages->all();
  108. *
  109. * // Format all of the messages in the collector
  110. * $all = $messages->all('<p>:message</p>');
  111. * </code>
  112. *
  113. * @param string $format
  114. * @return array
  115. */
  116. public function all($format = ':message')
  117. {
  118. $all = array();
  119. foreach ($this->messages as $messages)
  120. {
  121. $all = array_merge($all, $this->format($messages, $format));
  122. }
  123. return $all;
  124. }
  125. /**
  126. * Format an array of messages.
  127. *
  128. * @param array $messages
  129. * @param string $format
  130. * @return array
  131. */
  132. protected function format($messages, $format)
  133. {
  134. $messages = (array) $messages;
  135. foreach ($messages as $key => &$message)
  136. {
  137. $message = str_replace(':message', $message, $format);
  138. }
  139. return $messages;
  140. }
  141. }