messages.php 3.0 KB

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