123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php namespace System\Validation;
- use System\Lang;
- abstract class Rule {
-
- public $attributes;
-
- public $message;
-
- protected $error;
-
- public function __construct($attributes)
- {
- $this->attributes = $attributes;
- }
-
- public function validate($attributes, &$errors)
- {
- foreach ($this->attributes as $attribute)
- {
- if ( ! $this->check($attribute, $attributes))
- {
- $errors[$attribute][] = $this->prepare_message($attribute);
- }
- }
- }
-
- private function prepare_message($attribute)
- {
- if (is_null($this->message))
- {
- throw new \Exception("An error message must be specified for every validation rule.");
- }
- $message = $this->message;
-
-
-
-
-
-
-
- if (strpos($message, ':attribute'))
- {
- $message = str_replace(':attribute', Lang::line('attributes.'.$attribute)->get($attribute), $message);
- }
- if ($this instanceof Rules\Size_Of)
- {
- $message = str_replace(':max', $this->maximum, $message);
- $message = str_replace(':min', $this->minimum, $message);
- $message = str_replace(':size', $this->length, $message);
- }
- return $message;
- }
-
- public function message($message)
- {
- $this->message = $message;
- return $this;
- }
- }
|