123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642 |
- <?php namespace Laravel\Validation;
- use Closure;
- use Laravel\Arr;
- use Laravel\IoC;
- use Laravel\Str;
- use Laravel\Lang;
- use Laravel\Input;
- use Laravel\Database\Manager as DB;
- class Validator {
-
- public $connection;
-
- public $attributes;
-
- public $errors;
-
- protected $rules = array();
-
- protected $messages = array();
-
- protected $language;
-
- protected $size_rules = array('size', 'between', 'min', 'max');
-
- protected $numeric_rules = array('numeric', 'integer');
-
- protected static $validators = array();
-
- public function __construct($attributes, $rules, $messages = array())
- {
- foreach ($rules as $key => &$rule)
- {
- $rule = (is_string($rule)) ? explode('|', $rule) : $rule;
- }
- $this->rules = $rules;
- $this->messages = $messages;
- $this->attributes = $attributes;
- }
-
- public static function make($attributes, $rules, $messages = array())
- {
- return new static($attributes, $rules, $messages);
- }
-
- public static function register($name, $validator)
- {
- static::$validators[$name] = $validator;
- }
-
- public function invalid()
- {
- return ! $this->valid();
- }
-
- public function valid()
- {
- $this->errors = new Messages;
- foreach ($this->rules as $attribute => $rules)
- {
- foreach ($rules as $rule)
- {
- $this->check($attribute, $rule);
- }
- }
- return count($this->errors->messages) == 0;
- }
-
- protected function check($attribute, $rule)
- {
- list($rule, $parameters) = $this->parse($rule);
- if ( ! method_exists($this, $validator = 'validate_'.$rule) and ! isset(static::$validators[$rule]))
- {
- throw new \Exception("Validation rule [$rule] doesn't exist.");
- }
-
-
- $value = Arr::get($this->attributes, $attribute);
-
-
-
- if ( ! $this->validate_required($attribute, $value) and ! in_array($rule, array('required', 'accepted')))
- {
- return;
- }
- if ( ! $this->$validator($attribute, $value, $parameters, $this))
- {
- $this->error($attribute, $rule, $parameters);
- }
- }
-
- protected function error($attribute, $rule, $parameters)
- {
- $message = $this->get_message($attribute, $rule);
- $message = $this->format_message($message, $attribute, $rule, $parameters);
- $this->errors->add($attribute, $message);
- }
-
- protected function validate_required($attribute, $value)
- {
- return (is_null($value) or (is_string($value) and trim($value) === ''));
- }
-
- protected function validate_confirmed($attribute, $value)
- {
- $confirmation = $this->attributes[$attribute.'_confirmation'];
- return array_key_exists($attribute.'_confirmation', $this->attributes) and $value == $confirmation;
- }
-
- protected function validate_accepted($attribute, $value)
- {
- return $this->validate_required($attribute) and ($value == 'yes' or $value == '1');
- }
-
- protected function validate_numeric($attribute, $value)
- {
- return is_numeric($value);
- }
-
- protected function validate_integer($attribute, $value)
- {
- return filter_var($value, FILTER_VALIDATE_INT) !== false;
- }
-
- protected function validate_size($attribute, $value, $parameters)
- {
- return $this->get_size($attribute) == $parameters[0];
- }
-
- protected function validate_between($attribute, $value, $parameters)
- {
- return $this->get_size($attribute) >= $parameters[0] and $this->get_size($attribute) <= $parameters[1];
- }
-
- protected function validate_min($attribute, $value, $parameters)
- {
- return $this->get_size($attribute) >= $parameters[0];
- }
-
- protected function validate_max($attribute, $value, $parameters)
- {
- return $this->get_size($attribute) <= $parameters[0];
- }
-
- protected function get_size($attribute)
- {
- if (is_numeric($this->attributes[$attribute]) and $this->has_rule($attribute, $this->numeric_rules))
- {
- return $this->attributes[$attribute];
- }
- $value = $this->attributes[$attribute];
- if (array_key_exists($attribute, Input::file()))
- {
- return $value['size'] / 1024;
- }
- else
- {
- return Str::length(trim($value));
- }
- }
-
- protected function validate_in($attribute, $value, $parameters)
- {
- return in_array($value, $parameters);
- }
-
- protected function validate_not_in($attribute, $value, $parameters)
- {
- return ! in_array($value, $parameters);
- }
-
- protected function validate_unique($attribute, $value, $parameters)
- {
- if ( ! isset($parameters[1])) $parameters[1] = $attribute;
- if (is_null($this->connection)) $this->connection = DB::connection();
- return $this->connection->table($parameters[0])->where($parameters[1], '=', $value)->count() == 0;
- }
-
- protected function validate_email($attribute, $value)
- {
- return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
- }
-
- protected function validate_url($attribute, $value)
- {
- return filter_var($value, FILTER_VALIDATE_URL) !== false;
- }
-
- protected function validate_active_url($attribute, $value)
- {
- $url = str_replace(array('http://', 'https://', 'ftp://'), '', Str::lower($value));
-
- return checkdnsrr($url);
- }
-
- protected function validate_image($attribute, $value)
- {
- return $this->validate_mimes($attribute, array('jpg', 'png', 'gif', 'bmp'));
- }
-
- protected function validate_alpha($attribute, $value)
- {
- return preg_match('/^([a-z])+$/i', $value);
- }
-
- protected function validate_alpha_num($attribute, $value)
- {
- return preg_match('/^([a-z0-9])+$/i', $value);
- }
-
- protected function validate_alpha_dash($attribute, $value)
- {
- return preg_match('/^([-a-z0-9_-])+$/i', $value);
- }
-
- protected function validate_mimes($attribute, $parameters)
- {
- foreach ($parameters as $extension)
- {
- if (File::is($extension, $this->attributes[$attribute]['tmp_name'])) return true;
- }
- return false;
- }
-
- protected function get_message($attribute, $rule)
- {
- if (array_key_exists($attribute.'_'.$rule, $this->messages))
- {
- return $this->messages[$attribute.'_'.$rule];
- }
- elseif (array_key_exists($rule, $this->messages))
- {
- return $this->messages[$rule];
- }
- else
- {
- $message = Lang::line('validation.'.$rule)->get($this->language);
-
-
- if (in_array($rule, $this->size_rules) and ! $this->has_rule($attribute, $this->numeric_rules))
- {
- return (array_key_exists($attribute, Input::file()))
- ? rtrim($message, '.').' '.Lang::line('validation.kilobytes')->get($this->language).'.'
- : rtrim($message, '.').' '.Lang::line('validation.characters')->get($this->language).'.';
- }
- return $message;
- }
- }
-
- protected function format_message($message, $attribute, $rule, $parameters)
- {
-
-
-
- $display = Lang::line('validation.attributes.'.$attribute)->get($this->language, str_replace('_', ' ', $attribute));
- $message = str_replace(':attribute', $display, $message);
-
-
- if (in_array($rule, $this->size_rules))
- {
- $max = ($rule == 'between') ? $parameters[1] : $parameters[0];
- $message = str_replace(array(':size', ':min', ':max'), array($parameters[0], $parameters[0], $max), $message);
- }
-
-
- elseif (in_array($rule, array('in', 'not_in', 'mimes')))
- {
- $message = str_replace(':values', implode(', ', $parameters), $message);
- }
- return $message;
- }
-
- protected function has_rule($attribute, $rules)
- {
- foreach ($this->rules[$attribute] as $rule)
- {
- list($rule, $parameters) = $this->parse($rule);
- if (in_array($rule, $rules)) return true;
- }
- return false;
- }
-
- protected function parse($rule)
- {
-
-
-
- $parameters = (($colon = strpos($rule, ':')) !== false) ? explode(',', substr($rule, $colon + 1)) : array();
- return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters);
- }
-
- public function speaks($language)
- {
- $this->language = $language;
- return $this;
- }
-
- public function connection(\Laravel\Database\Connection $connection)
- {
- $this->connection = $connection;
- return $this;
- }
-
- public function __call($method, $parameters)
- {
-
-
-
- if (isset(static::$validators[$method = substr($method, 9)]))
- {
- return call_user_func_array(static::$validators[$method], $parameters);
- }
- throw new \Exception("Call to undefined method [$method] on Validator instance.");
- }
- }
|