123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769 |
- <?php namespace Laravel; use Closure;
- class Validator {
-
- public $attributes;
-
- public $errors;
-
- protected $rules = array();
-
- protected $messages = array();
-
- protected $db;
-
- protected $bundle = DEFAULT_BUNDLE;
-
- protected $language;
-
- protected $size_rules = array('size', 'between', 'min', 'max');
-
- protected $inclusion_rules = array('in', 'not_in', 'mimes');
-
- 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);
- $value = array_get($this->attributes, $attribute);
-
-
-
-
- $validatable = $this->validatable($rule, $attribute, $value);
- if ($validatable and ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this))
- {
- $this->error($attribute, $rule, $parameters);
- }
- }
-
- protected function validatable($rule, $attribute, $value)
- {
- return $this->validate_required($attribute, $value) or $this->implicit($rule);
- }
-
- protected function implicit($rule)
- {
- return $rule == 'required' or $rule == 'accepted';
- }
-
- protected function error($attribute, $rule, $parameters)
- {
- $message = $this->replace($this->message($attribute, $rule), $attribute, $rule, $parameters);
- $this->errors->add($attribute, $message);
- }
-
- protected function validate_required($attribute, $value)
- {
- if (is_null($value))
- {
- return false;
- }
- elseif (is_string($value) and trim($value) === '')
- {
- return false;
- }
- elseif ( ! is_null(Input::file($attribute)) and $value['tmp_name'] == '')
- {
- return false;
- }
- return true;
- }
-
- protected function validate_confirmed($attribute, $value)
- {
- $confirmed = $attribute.'_confirmation';
- return isset($this->attributes[$confirmed]) and $value == $this->attributes[$confirmed];
- }
-
- protected function validate_accepted($attribute, $value)
- {
- return $this->validate_required($attribute, $value) 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->size($attribute, $value) == $parameters[0];
- }
-
- protected function validate_between($attribute, $value, $parameters)
- {
- $size = $this->size($attribute, $value);
- return $size >= $parameters[0] and $size <= $parameters[1];
- }
-
- protected function validate_min($attribute, $value, $parameters)
- {
- return $this->size($attribute, $value) >= $parameters[0];
- }
-
- protected function validate_max($attribute, $value, $parameters)
- {
- return $this->size($attribute, $value) <= $parameters[0];
- }
-
- protected function size($attribute, $value)
- {
-
-
-
-
- if (is_numeric($value) and $this->has_rule($attribute, $this->numeric_rules))
- {
- return $this->attributes[$attribute];
- }
- elseif (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 (is_null($this->db)) $this->db = Database::connection();
- $query = $this->db->table($parameters[0])->where($attribute, '=', $value);
- if (isset($parameters[1]))
- {
- $query->where('id', '<>', $parameters[1]);
- }
- return $query->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, $value, 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, $value, $parameters)
- {
- if ( ! is_array($value) or array_get($value, 'tmp_name', '') == '') return true;
- foreach ($parameters as $extension)
- {
- if (File::is($extension, $value['tmp_name']))
- {
- return true;
- }
- }
- return false;
- }
-
- protected function message($attribute, $rule)
- {
- $bundle = Bundle::prefix($this->bundle);
-
-
-
- if (array_key_exists($attribute.'_'.$rule, $this->messages))
- {
- return $this->messages[$attribute.'_'.$rule];
- }
-
-
-
- elseif (array_key_exists($rule, $this->messages))
- {
- return $this->messages[$rule];
- }
-
-
-
- elseif (in_array($rule, $this->size_rules))
- {
- return $this->size_message($bundle, $attribute, $rule);
- }
-
-
-
- else
- {
- $line = "{$bundle}validation.{$rule}";
- return Lang::line($line)->get($this->language);
- }
- }
-
- protected function size_message($bundle, $attribute, $rule)
- {
-
-
-
-
-
- if ($this->has_rule($attribute, $this->numeric_rules))
- {
- $line = 'numeric';
- }
- elseif (array_key_exists($attribute, Input::file()))
- {
- $line = 'file';
- }
- else
- {
- $line = 'string';
- }
- return Lang::line("{$bundle}validation.{$rule}.{$line}")->get($this->language);
- }
-
- protected function replace($message, $attribute, $rule, $parameters)
- {
- $message = str_replace(':attribute', $this->attribute($attribute), $message);
- if (in_array($rule, $this->size_rules))
- {
-
-
-
-
- $max = ($rule == 'between') ? $parameters[1] : $parameters[0];
- $replace = array($parameters[0], $parameters[0], $max);
- $message = str_replace(array(':size', ':min', ':max'), $replace, $message);
- }
-
-
-
- elseif (in_array($rule, $this->inclusion_rules))
- {
- $message = str_replace(':values', implode(', ', $parameters), $message);
- }
- return $message;
- }
-
- protected function attribute($attribute)
- {
- $bundle = Bundle::prefix($this->bundle);
-
-
-
-
-
-
-
- $display = Lang::line("{$bundle}validation.attributes.{$attribute}")->get($this->language);
- return (is_null($display)) ? str_replace('_', ' ', $attribute) : $display;
- }
-
- 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 = array();
-
-
-
- if (($colon = strpos($rule, ':')) !== false)
- {
- $parameters = explode(',', substr($rule, $colon + 1));
- }
- return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters);
- }
-
- public function bundle($bundle)
- {
- $this->bundle = $bundle;
- return $this;
- }
-
- public function speaks($language)
- {
- $this->language = $language;
- return $this;
- }
-
- public function connection(Database\Connection $connection)
- {
- $this->db = $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.");
- }
- }
|