messages.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php namespace Laravel\Validation;
  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 = $messages;
  19. }
  20. /**
  21. * Add a message to the collector.
  22. *
  23. * Duplicate messages will not be added.
  24. *
  25. * <code>
  26. * // Add a message to the collector for the "email" attribute
  27. * $messages->add('email', 'The e-mail address is invalid.');
  28. * </code>
  29. *
  30. * @param string $key
  31. * @param string $message
  32. * @return void
  33. */
  34. public function add($key, $message)
  35. {
  36. if ($this->unique($key, $message)) $this->messages[$key][] = $message;
  37. }
  38. /**
  39. * Determine if a key and message combination already exists.
  40. *
  41. * @param string $key
  42. * @param string $message
  43. * @return bool
  44. */
  45. protected function unique($key, $message)
  46. {
  47. return ! isset($this->messages[$key]) or array_search($message, $this->messages[$key]) === false;
  48. }
  49. /**
  50. * Determine if messages exist for a given key.
  51. *
  52. * @param string $key
  53. * @return bool
  54. */
  55. public function has($key)
  56. {
  57. return $this->first($key) !== '';
  58. }
  59. /**
  60. * Get the first message for a given key.
  61. *
  62. * <code>
  63. * // Get the first message for the e-mail attribute
  64. * $email = $messages->first('email');
  65. *
  66. * // Format the first message for the e-mail attribute
  67. * $email = $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, $format = ':message')
  75. {
  76. return (count($messages = $this->get($key, $format)) > 0) ? $messages[0] : '';
  77. }
  78. /**
  79. * Get all of the messages for a key.
  80. *
  81. * <code>
  82. * // Get all of the messages for the e-mail attribute
  83. * $email = $messages->get('email');
  84. *
  85. * // Format all of the messages for the e-mail attribute
  86. * $email = $messages->get('email', '<p>:message</p>');
  87. * </code>
  88. *
  89. * @param string $key
  90. * @param string $format
  91. * @return array
  92. */
  93. public function get($key = null, $format = ':message')
  94. {
  95. if (is_null($key)) return $this->all($format);
  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.
  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. foreach ($messages as $key => &$message)
  135. {
  136. $message = str_replace(':message', $message, $format);
  137. }
  138. return $messages;
  139. }
  140. }