556 lines
13 KiB
PHP
556 lines
13 KiB
PHP
<?php namespace Laravel\Validation;
|
|
|
|
use Closure;
|
|
use Laravel\IoC;
|
|
use Laravel\Str;
|
|
use Laravel\Lang;
|
|
use Laravel\Database\Manager as Database;
|
|
|
|
class Validator {
|
|
|
|
/**
|
|
* The validation rules.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $rules = array();
|
|
|
|
/**
|
|
* The validation messages.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $messages = array();
|
|
|
|
/**
|
|
* The language that should be used when retrieving error messages.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $language;
|
|
|
|
/**
|
|
* The size related validation rules.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $size_rules = array('size', 'between', 'min', 'max');
|
|
|
|
/**
|
|
* The numeric related validation rules.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $numeric_rules = array('numeric', 'integer');
|
|
|
|
/**
|
|
* The database connection that should be used by the validator.
|
|
*
|
|
* @var Database\Connection
|
|
*/
|
|
public $connection;
|
|
|
|
/**
|
|
* The array being validated.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $attributes;
|
|
|
|
/**
|
|
* The post-validation error messages.
|
|
*
|
|
* @var Messages
|
|
*/
|
|
public $errors;
|
|
|
|
/**
|
|
* Create a new validator instance.
|
|
*
|
|
* @param array $attributes
|
|
* @param array $rules
|
|
* @param array $messages
|
|
* @return void
|
|
*/
|
|
public function __construct($attributes, $rules, $messages = array())
|
|
{
|
|
foreach ($rules as $key => &$rule)
|
|
{
|
|
$rule = (is_string($rule)) ? explode('|', $rule) : $rule;
|
|
}
|
|
|
|
$this->rules = $rules;
|
|
$this->attributes = $attributes;
|
|
$this->messages = array_merge($this->messages, $messages);
|
|
}
|
|
|
|
/**
|
|
* Create a new validator instance.
|
|
*
|
|
* @param array $attributes
|
|
* @param array $rules
|
|
* @param array $messages
|
|
* @return Validator
|
|
*/
|
|
public static function make($attributes, $rules, $messages = array())
|
|
{
|
|
return new static($attributes, $rules, $messages);
|
|
}
|
|
|
|
/**
|
|
* Validate the target array using the specified validation rules.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function invalid()
|
|
{
|
|
return ! $this->valid();
|
|
}
|
|
|
|
/**
|
|
* Validate the target array using the specified validation rules.
|
|
*
|
|
* @return bool
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Evaluate an attribute against a validation rule.
|
|
*
|
|
* @param string $attribute
|
|
* @param string $rule
|
|
* @return void
|
|
*/
|
|
protected function check($attribute, $rule)
|
|
{
|
|
list($rule, $parameters) = $this->parse($rule);
|
|
|
|
if ( ! method_exists($this, $validator = 'validate_'.$rule))
|
|
{
|
|
throw new \Exception("Validation rule [$rule] doesn't exist.");
|
|
}
|
|
|
|
// No validation will be run for attributes that do not exist unless the rule being validated
|
|
// is "required" or "accepted". No other rules have implicit "required" checks.
|
|
if ( ! $this->validate_required($attribute) and ! in_array($rule, array('required', 'accepted'))) return;
|
|
|
|
if ( ! $this->$validator($attribute, $parameters, $this))
|
|
{
|
|
$message = $this->format_message($this->get_message($attribute, $rule), $attribute, $rule, $parameters);
|
|
|
|
$this->errors->add($attribute, $message, $attribute, $rule, $parameters);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate that a required attribute exists in the attributes array.
|
|
*
|
|
* @param string $attribute
|
|
* @return bool
|
|
*/
|
|
protected function validate_required($attribute)
|
|
{
|
|
if ( ! array_key_exists($attribute, $this->attributes)) return false;
|
|
|
|
if (is_string($this->attributes[$attribute]) and trim($this->attributes[$attribute]) === '') return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Validate that an attribute has a matching confirmation attribute.
|
|
*
|
|
* @param string $attribute
|
|
* @return bool
|
|
*/
|
|
protected function validate_confirmed($attribute)
|
|
{
|
|
$value = $this->attributes[$attribute];
|
|
|
|
$confirmation = $this->attributes[$attribute.'_confirmation'];
|
|
|
|
return array_key_exists($attribute.'_confirmation', $this->attributes) and $value == $confirmation;
|
|
}
|
|
|
|
/**
|
|
* Validate that an attribute was "accepted".
|
|
*
|
|
* This validation rule implies the attribute is "required".
|
|
*
|
|
* @param string $attribute
|
|
* @return bool
|
|
*/
|
|
protected function validate_accepted($attribute)
|
|
{
|
|
$value = $this->attributes[$attribute];
|
|
|
|
return $this->validate_required($attribute) and ($value == 'yes' or $value == '1');
|
|
}
|
|
|
|
/**
|
|
* Validate that an attribute is numeric.
|
|
*
|
|
* @param string $attribute
|
|
* @return bool
|
|
*/
|
|
protected function validate_numeric($attribute)
|
|
{
|
|
return is_numeric($this->attributes[$attribute]);
|
|
}
|
|
|
|
/**
|
|
* Validate that an attribute is an integer.
|
|
*
|
|
* @param string $attribute
|
|
* @return bool
|
|
*/
|
|
protected function validate_integer($attribute)
|
|
{
|
|
return filter_var($this->attributes[$attribute], FILTER_VALIDATE_INT) !== false;
|
|
}
|
|
|
|
/**
|
|
* Validate the size of an attribute.
|
|
*
|
|
* @param string $attribute
|
|
* @param array $parameters
|
|
* @return bool
|
|
*/
|
|
protected function validate_size($attribute, $parameters)
|
|
{
|
|
return $this->get_size($attribute) == $parameters[0];
|
|
}
|
|
|
|
/**
|
|
* Validate the size of an attribute is between a set of values.
|
|
*
|
|
* @param string $attribute
|
|
* @param array $parameters
|
|
* @return bool
|
|
*/
|
|
protected function validate_between($attribute, $parameters)
|
|
{
|
|
return $this->get_size($attribute) >= $parameters[0] and $this->get_size($attribute) <= $parameters[1];
|
|
}
|
|
|
|
/**
|
|
* Validate the size of an attribute is greater than a minimum value.
|
|
*
|
|
* @param string $attribute
|
|
* @param array $parameters
|
|
* @return bool
|
|
*/
|
|
protected function validate_min($attribute, $parameters)
|
|
{
|
|
return $this->get_size($attribute) >= $parameters[0];
|
|
}
|
|
|
|
/**
|
|
* Validate the size of an attribute is less than a maximum value.
|
|
*
|
|
* @param string $attribute
|
|
* @param array $parameters
|
|
* @return bool
|
|
*/
|
|
protected function validate_max($attribute, $parameters)
|
|
{
|
|
return $this->get_size($attribute) <= $parameters[0];
|
|
}
|
|
|
|
/**
|
|
* Get the size of an attribute.
|
|
*
|
|
* @param string $attribute
|
|
* @return mixed
|
|
*/
|
|
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];
|
|
|
|
$files = IoC::container()->resolve('laravel.input')->files();
|
|
|
|
return (array_key_exists($attribute, $files) ? $value['size'] / 1024 : Str::length(trim($value));
|
|
}
|
|
|
|
/**
|
|
* Validate an attribute is contained within a list of values.
|
|
*
|
|
* @param string $attribute
|
|
* @param array $parameters
|
|
* @return bool
|
|
*/
|
|