messages.php 2.9 KB

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