messages.php 3.0 KB

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