123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php namespace Laravel\Validation;
- class Messages {
-
- public $messages;
-
- public function __construct($messages = array())
- {
- $this->messages = $messages;
- }
-
- public function add($key, $message)
- {
- if ($this->unique($key, $message)) $this->messages[$key][] = $message;
- }
-
- protected function unique($key, $message)
- {
- return ! isset($this->messages[$key]) or array_search($message, $this->messages[$key]) === false;
- }
-
- public function has($key)
- {
- return $this->first($key) !== '';
- }
-
- public function first($key, $format = ':message')
- {
- return (count($messages = $this->get($key, $format)) > 0) ? $messages[0] : '';
- }
-
- public function get($key = null, $format = ':message')
- {
- if (is_null($key)) return $this->all($format);
- if (array_key_exists($key, $this->messages))
- {
- return $this->format($this->messages[$key], $format);
- }
- return array();
- }
-
- public function all($format = ':message')
- {
- $all = array();
- foreach ($this->messages as $messages)
- {
- $all = array_merge($all, $this->format($messages, $format));
- }
- return $all;
- }
-
- protected function format($messages, $format)
- {
- foreach ($messages as $key => &$message)
- {
- $message = str_replace(':message', $message, $format);
- }
- return $messages;
- }
- }
|