123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php namespace System\Validation;
- class Error_Collector {
-
- public $messages;
-
- public function __construct($messages = array())
- {
- $this->messages = $messages;
- }
-
- public function add($attribute, $message)
- {
-
-
-
-
-
-
-
- if ( ! array_key_exists($attribute, $this->messages) or ! is_array($this->messages[$attribute]) or ! in_array($message, $this->messages[$attribute]))
- {
- $this->messages[$attribute][] = $message;
- }
- }
-
- public function has($attribute)
- {
- return $this->first($attribute) !== '';
- }
-
- public function first($attribute)
- {
- return (count($messages = $this->get($attribute)) > 0) ? $messages[0] : '';
- }
-
- public function get($attribute = null, $format = ':message')
- {
- if (is_null($attribute))
- {
- return $this->all($format);
- }
- return (array_key_exists($attribute, $this->messages)) ? $this->format($this->messages[$attribute], $format) : array();
- }
-
- public function all($format = ':message')
- {
- $all = array();
-
-
-
-
- foreach ($this->messages as $messages)
- {
- $all = array_merge($all, $this->format($messages, $format));
- }
- return $all;
- }
-
- private function format($messages, $format)
- {
- array_walk($messages, function(&$message, $key) use ($format) { $message = str_replace(':message', $message, $format); });
- return $messages;
- }
- }
|