messages.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 message collector
  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 ( ! isset($this->messages[$key]) or array_search($message, $this->messages[$key]) === false)
  37. {
  38. $this->messages[$key][] = $message;
  39. }
  40. }
  41. /**
  42. * Determine if messages exist for a given key.
  43. *
  44. * @param string $key
  45. * @return bool
  46. */
  47. public function has($key)
  48. {
  49. return $this->first($key) !== '';
  50. }
  51. /**
  52. * Get the first message for a given key.
  53. *
  54. * Optionally, a format may be specified for the returned message.
  55. *
  56. * <code>
  57. * // Get the first message for the e-mail attribute
  58. * echo $messages->first('email');
  59. *
  60. * // Get the first message for the e-mail attribute using a format
  61. * echo $messages->first('email', '<p>:message</p>');
  62. * </code>
  63. *
  64. * @param string $key
  65. * @param string $format
  66. * @return string
  67. */
  68. public function first($key, $format = ':message')
  69. {
  70. return (count($messages = $this->get($key, $format)) > 0) ? $messages[0] : '';
  71. }
  72. /**
  73. * Get all of the messages for a key.
  74. *
  75. * Optionally, a format may be specified for the returned messages.
  76. *
  77. * <code>
  78. * // Get all of the messages for the e-mail attribute
  79. * $messages = $messages->get('email');
  80. *
  81. * // Get all of the messages for the e-mail attribute using a format
  82. * $messages = $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 = null, $format = ':message')
  90. {
  91. if (is_null($key)) return $this->all($format);
  92. return (array_key_exists($key, $this->messages)) ? $this->format($this->messages[$key], $format) : array();
  93. }
  94. /**
  95. * Get all of the messages for every key.
  96. *
  97. * <code>
  98. * // Get all of the error messages using a format
  99. * $messages = $messages->all('<p>:message</p>');
  100. * </code>
  101. *
  102. * @param string $format
  103. * @return array
  104. */
  105. public function all($format = ':message')
  106. {
  107. $all = array();
  108. foreach ($this->messages as $messages)
  109. {
  110. $all = array_merge($all, $this->format($messages, $format));
  111. }
  112. return $all;
  113. }
  114. /**
  115. * Format an array of messages.
  116. *
  117. * @param array $messages
  118. * @param string $format
  119. * @return array
  120. */
  121. protected function format($messages, $format)
  122. {
  123. foreach ($messages as $key => &$message)
  124. {
  125. $message = str_replace(':message', $message, $format);
  126. }
  127. return $messages;
  128. }
  129. }