【Laravel3.0.0源码阅读分析】验证器类validator.php

<?php namespace Laravel; use Closure;
// 验证类
class Validator {

	/**
	 * The array being validated.
	 * 正在验证的数组。
	 * @var array
	 */
	public $attributes;

	/**
	 * The post-validation error messages.
	 * 验证后错误消息。
	 * @var Messages
	 */
	public $errors;

	/**
	 * The validation rules.
	 * 验证规则。
	 * @var array
	 */
	protected $rules = array();

	/**
	 * The validation messages.
	 * 验证消息。
	 * @var array
	 */
	protected $messages = array();

	/**
	 * The database connection that should be used by the validator.
	 * 验证器应该使用的数据库连接。
	 * @var Database\Connection
	 */
	protected $db;

	/**
	 * The bundle for which the validation is being run.
	 * 正在运行验证的包。
	 * @var string
	 */
	protected $bundle = DEFAULT_BUNDLE;

	/**
	 * 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 registered custom validators.
	 * 注册的自定义验证器。
	 * @var array
	 */
	protected static $validators = array();

	/**
	 * 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->messages = $messages;
		$this->attributes = $attributes;
	}

	/**
	 * 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);
	}

	/**
	 * Register a custom validator.
	 * 注册自定义验证器。
	 * @param  string   $name
	 * @param  Closure  $validator
	 * @return void
	 */
	public static function register($name, $validator)
	{
		static::$validators[$name] = $validator;
	}

	/**
	 * Validate the target array using the specified validation rules.
	 * 使用指定的验证规则验证目标数组。
	 * @return bool
	 */
	public function passes()
	{
		return $this->valid();
	}

	/**
	 * Validate the target array using the specified validation rules.
	 * 使用指定的验证规则验证目标数组。
	 * @return bool
	 */
	public function fails()
	{
		return $this->invalid();
	}

	/**
	 * 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);

		$value = array_get($this->attributes, $attribute);

		// Before running the validator, we need to verify that the attribute and rule
		// combination is actually validatable. Only the "accepted" rule implies that
		// the attribute is "required", so if the attribute does not exist, the other
		// rules will not be run for the attribute.
        // 在运行验证器之前,我们需要验证属性和规则组合实际上是可验证的。
        // 只有“accepted”规则暗示该属性是“必需的”,因此如果该属性不存在,则不会为该属性运行其他规则。
		$validatable = $this->validatable($rule, $attribute, $value);

		if ($validatable and ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this))
		{
			$this->error($attribute, $rule, $parameters);
		}
	}

	/**
	 * Determine if an attribute is validatable.
	 * 确定属性是否可验证。
	 * To be considered validatable, the attribute must either exist, or the rule
	 * being checked must implicitly validate "required", such as the "required"
	 * rule or the "accepted" rule.
	 * 要被视为可验证,该属性必须存在,或者被检查的规则必须隐式验证“必需”,例如“必需”规则或“接受”规则。
	 * @param  string  $rule
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validatable($rule, $attribute, $value)
	{
		return $this->validate_required($attribute, $value) or $this->implicit($rule);
	}

	/**
	 * Determine if a given rule implies that the attribute is required.
	 * 确定给定规则是否暗示该属性是必需的。
	 * @param  string  $rule
	 * @return bool
	 */
	protected function implicit($rule)
	{
		return $rule == 'required' or $rule == 'accepted';
	}

	/**
	 * Add an error message to the validator's collection of messages.
	 * 将错误消息添加到验证器的消息集合中。
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return void
	 */
	protected function error($attribute, $rule, $parameters)
	{
		$message = $this->replace($this->message($attribute, $rule), $attribute, $rule, $parameters);

		$this->errors->add($attribute, $message);
	}

	/**
	 * Validate that a required attribute exists in the attributes array.
	 * 验证属性数组中是否存在必需的属性。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	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;
	}

	/**
	 * Validate that an attribute has a matching confirmation attribute.
	 * 验证属性是否具有匹配的确认属性。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_confirmed($attribute, $value)
	{
		return $this->validate_same($attribute, $value, array($attribute.'_confirmation'));
	}

	/**
	 * Validate that an attribute was "accepted".
	 * 验证属性是否被“接受”。
	 * This validation rule implies the attribute is "required".
	 * 此验证规则暗示该属性是“必需的”。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_accepted($attribute, $value)
	{
		return $this->validate_required($attribute, $value) and ($value == 'yes' or $value == '1');
	}

	/**
	 * Validate that an attribute is the same as another attribute.
	 * 验证一个属性是否与另一个属性相同。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_same($attribute, $value, $parameters)
	{
		$other = $parameters[0];

		return isset($this->attributes[$other]) and $value == $this->attributes[$other];
	}

	/**
	 * Validate that an attribute is different from another attribute.
	 * 验证一个属性是否不同于另一个属性。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_different($attribute, $value, $parameters)
	{
		$other = $parameters[0];

		return isset($this->attributes[$other]) and $value != $this->attributes[$other];
	}

	/**
	 * Validate that an attribute is numeric.
	 * 验证属性是否为数字。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_numeric($attribute, $value)
	{
		return is_numeric($value);
	}

	/**
	 * Validate that an attribute is an integer.
	 * 验证属性是否为整数。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_integer($attribute, $value)
	{
		return filter_var($value, FILTER_VALIDATE_INT) !== false;
	}

	/**
	 * Validate the size of an attribute.
	 * 验证属性的大小。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_size($attribute, $value, $parameters)
	{
		return $this->size($attribute, $value) == $parameters[0];
	}

	/**
	 * Validate the size of an attribute is between a set of values.
	 * 验证属性的大小是否在一组值之间。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_between($attribute, $value, $parameters)
	{
		$size = $this->size($attribute, $value);

		return $size >= $parameters[0] and $size <= $parameters[1];
	}

	/**
	 * Validate the size of an attribute is greater than a minimum value.
	 * 验证属性的大小是否大于最小值。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_min($attribute, $value, $parameters)
	{
		return $this->size($attribute, $value) >= $parameters[0];
	}

	/**
	 * Validate the size of an attribute is less than a maximum value.
	 * 验证属性的大小是否小于最大值
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_max($attribute, $value, $parameters)
	{
		return $this->size($attribute, $value) <= $parameters[0];
	}

	/**
	 * Get the size of an attribute.
	 * 获取属性的大小。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return mixed
	 */
	protected function size($attribute, $value)
	{
	 	// This method will determine if the attribute is a number, string, or file and
	 	// return the proper size accordingly. If it is a number, then number itself is
	 	// the size; if it is a file, the size is kilobytes in the size; if it is a
	 	// string, the length is the size.
        // 此方法将确定属性是数字、字符串还是文件,并相应地返回正确的大小。
        // 如果是数字,则数字本身就是大小; 如果是文件,则大小以千字节为单位; 如果是字符串,则长度为大小。
		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));
		}
	}

	/**
	 * Validate an attribute is contained within a list of values.
	 * 验证属性是否包含在值列表中。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_in($attribute, $value, $parameters)
	{
		return in_array($value, $parameters);
	}

	/**
	 * Validate an attribute is not contained within a list of values.
	 * 验证属性不包含在值列表中。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_not_in($attribute, $value, $parameters)
	{
		return ! in_array($value, $parameters);
	}

	/**
	 * Validate the uniqueness of an attribute value on a given database table.
	 * 验证给定数据库表上属性值的唯一性。
	 * If a database column is not specified, the attribute will be used.
	 * 如果未指定数据库列,则将使用该属性。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_unique($attribute, $value, $parameters)
	{
		// We allow the table column to be specified just in case the column does
		// not have the same name as the attribute. It must be within the second
		// parameter position, right after the database table name.
        // 我们允许指定表列,以防列与属性的名称不同。 它必须在第二个参数位置内,紧跟在数据库表名之后。
		if (isset($parameters[1]))
		{
			$attribute = $parameters[1];
		}

		$query = $this->db()->table($parameters[0])->where($attribute, '=', $value);

		// We also allow an ID to be specified that will not be included in the
		// uniqueness check. This makes updating columns easier since it is
		// fine for the given ID to exist in the table.
        // 我们还允许指定不会包含在唯一性检查中的 ID。 这使得更新列更容易,因为给定 ID 存在于表中是可以的。
		if (isset($parameters[2]))
		{
			$id = (isset($parameters[3])) ? $parameters[3] : 'id';

			$query->where($id, '<>', $parameters[2]);
		}

		return $query->count() == 0;
	}

	/**
	 * Validate the existence of an attribute value in a database table.
	 * 验证数据库表中属性值的存在。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	protected function validate_exists($attribute, $value, $parameters)
	{
		if (isset($parameters[1])) $attribute = $parameters[1];

		// Grab the number of elements we are looking for. If the given value is
		// in array, we'll count all of the values in the array, otherwise we
		// can just make sure the count is greater or equal to one.
        // 获取我们正在寻找的元素数量。 如果给定的值在数组中,我们将计算数组中的所有值,
        // 否则我们可以确保计数大于或等于 1。
		$count = (is_array($value)) ? count($value) : 1;

		$query = $this->db()->table($parameters[0]);

		// If the given value is an array, we will check for the existence of
		// all the values in the database, otherwise we'll check for the
		// presence of the single given value in the database.
        // 如果给定值是一个数组,我们将检查数据库中所有值是否存在,否则我们将检查数据库中是否存在单个给定值。
		if (is_array($value))
		{
			$query = $query->where_in($attribute, $value);
		}
		else
		{
			$query = $query->where($attribute, '=', $value);
		}

		return $query->count() >= $count;
	}

	/**
	 * Validate that an attribute is a valid IP.
	 * 验证属性是否为有效 IP。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_ip($attribute, $value)
	{
		return filter_var($value, FILTER_VALIDATE_IP) !== false;
	}

	/**
	 * Validate that an attribute is a valid e-mail address.
	 * 验证属性是否为有效的电子邮件地址。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_email($attribute, $value)
	{
		return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
	}

	/**
	 * Validate that an attribute is a valid URL.
	 * 验证属性是否是有效的 URL。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_url($attribute, $value)
	{
		return filter_var($value, FILTER_VALIDATE_URL) !== false;
	}

	/**
	 * Validate that an attribute is an active URL.
	 * 验证属性是否为活动 URL。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_active_url($attribute, $value)
	{
		$url = str_replace(array('http://', 'https://', 'ftp://'), '', Str::lower($value));
		
		return checkdnsrr($url);
	}

	/**
	 * Validate the MIME type of a file is an image MIME type.
	 * 验证文件的 MIME 类型是图像 MIME 类型。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_image($attribute, $value)
	{
		return $this->validate_mimes($attribute, $value, array('jpg', 'png', 'gif', 'bmp'));
	}

	/**
	 * Validate that an attribute contains only alphabetic characters.
	 * 验证属性是否仅包含字母字符。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_alpha($attribute, $value)
	{
		return preg_match('/^([a-z])+$/i', $value);
	}

	/**
	 * Validate that an attribute contains only alpha-numeric characters.
	 * 验证属性是否仅包含字母数字字符。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_alpha_num($attribute, $value)
	{
		return preg_match('/^([a-z0-9])+$/i', $value);
	}

	/**
	 * Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
	 * 验证属性是否仅包含字母数字字符、破折号和下划线。
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_alpha_dash($attribute, $value)
	{
		return preg_match('/^([-a-z0-9_-])+$/i', $value);	
	}

	/**
	 * Validate the MIME type of a file upload attribute is in a set of MIME types.
	 * 验证文件上传属性的 MIME 类型是否在一组 MIME 类型中。
	 * @param  string  $attribute
	 * @param  array   $value
	 * @param  array   $parameters
	 * @return bool
	 */
	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;
	}

	/**
	 * Get the proper error message for an attribute and rule.
	 * 获取属性和规则的正确错误消息。
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @return string
	 */
	protected function message($attribute, $rule)
	{
		$bundle = Bundle::prefix($this->bundle);

		// First we'll check for developer specified, attribute specific messages.
		// These messages take first priority. They allow the fine-grained tuning
		// of error messages for each rule.
        // 首先,我们将检查开发人员指定的、特定于属性的消息。 这些消息具有第一优先级。
        // 它们允许对每个规则的错误消息进行细粒度调整
		$custom = $attribute.'_'.$rule;

		if (array_key_exists($custom, $this->messages))
		{
			return $this->messages[$custom];
		}
		elseif (Lang::has($custom = "validation.custom.{$custom}", $this->language))
		{
			return Lang::line($custom)->get($this->language);
		}

		// Next we'll check for developer specified, rule specific error messages.
		// These allow the developer to override the error message for an entire
		// rule, regardless of the attribute being validated by that rule.
        // 接下来,我们将检查开发人员指定的、特定于规则的错误消息。
        // 这些允许开发人员覆盖整个规则的错误消息,而不管该规则验证的属性如何。
		elseif (array_key_exists($rule, $this->messages))
		{
			return $this->messages[$rule];
		}

		// If the rule being validated is a "size" rule, we will need to gather
		// the specific size message for the type of attribute being validated,
		// either a number, file, or string.
        // 如果被验证的规则是“大小”规则,我们将需要为被验证的属性类型收集特定的大小消息,可以是数字、文件或字符串。
		elseif (in_array($rule, $this->size_rules))
		{
			return $this->size_message($bundle, $attribute, $rule);
		}

		// If no developer specified messages have been set, and no other special
		// messages apply to the rule, we will just pull the default validation
		// message from the validation language file.
        // 如果没有设置开发人员指定的消息,并且没有其他特殊消息适用于规则,我们将只从验证语言文件中提取默认验证消息。
		else
		{
			$line = "{$bundle}validation.{$rule}";

			return Lang::line($line)->get($this->language);
		}
	}

	/**
	 * Get the proper error message for an attribute and size rule.
	 * 获取属性和大小规则的正确错误消息。
	 * @param  string  $bundle
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @return string
	 */
	protected function size_message($bundle, $attribute, $rule)
	{
		// There are three different types of size validations. The attribute
		// may be either a number, file, or a string, so we'll check a few
		// things to figure out which one it is.
        // 共有三种不同类型的大小验证。 该属性可以是数字、文件或字符串,因此我们将检查一些内容以确定它是哪一个。
		if ($this->has_rule($attribute, $this->numeric_rules))
		{
			$line = 'numeric';
		}
		// We assume that attributes present in the $_FILES array are files,
		// which makes sense. If the attribute doesn't have numeric rules
		// and isn't as file, it's a string.
        // 我们假设 $_FILES 数组中存在的属性是文件,这是有道理的。 如果属性没有数字规则并且不是文件,则它是一个字符串。
		elseif (array_key_exists($attribute, Input::file()))
		{
			$line = 'file';
		}
		else
		{
			$line = 'string';
		}

		return Lang::line("{$bundle}validation.{$rule}.{$line}")->get($this->language);	
	}

	/**
	 * Replace all error message place-holders with actual values.
	 * 用实际值替换所有错误消息占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace($message, $attribute, $rule, $parameters)
	{
		$message = str_replace(':attribute', $this->attribute($attribute), $message);

		if (method_exists($this, $replacer = 'replace_'.$rule))
		{
			$message = $this->$replacer($message, $attribute, $rule, $parameters);
		}

		return $message;
	}

	/**
	 * Replace all place-holders for the between rule.
	 * 替换规则之间的所有占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_between($message, $attribute, $rule, $parameters)
	{
		return str_replace(array(':min', ':max'), $parameters, $message);
	}

	/**
	 * Replace all place-holders for the size rule.
	 * 替换大小规则的所有占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_size($message, $attribute, $rule, $parameters)
	{
		return str_replace(':size', $parameters[0], $message);
	}

	/**
	 * Replace all place-holders for the min rule.
	 * 替换 min 规则的所有占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_min($message, $attribute, $rule, $parameters)
	{
		return str_replace(':min', $parameters[0], $message);
	}

	/**
	 * Replace all place-holders for the max rule.
	 * 替换最大规则的所有占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_max($message, $attribute, $rule, $parameters)
	{
		return str_replace(':max', $parameters[0], $message);
	}

	/**
	 * Replace all place-holders for the in rule.
	 * 替换 in 规则的所有占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_in($message, $attribute, $rule, $parameters)
	{
		return str_replace(':values', implode(', ', $parameters), $message);
	}

	/**
	 * Replace all place-holders for the not_in rule.
	 * 替换 not_in 规则的所有占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_not_in($message, $attribute, $rule, $parameters)
	{
		return str_replace(':values', implode(', ', $parameters), $message);
	}

	/**
	 * Replace all place-holders for the not_in rule.
	 * 替换 not_in 规则的所有占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_mimes($message, $attribute, $rule, $parameters)
	{
		return str_replace(':values', implode(', ', $parameters), $message);
	}

	/**
	 * Replace all place-holders for the same rule.
	 * 替换相同规则的所有占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_same($message, $attribute, $rule, $parameters)
	{
		return str_replace(':other', $parameters[0], $message);
	}

	/**
	 * Replace all place-holders for the different rule.
	 * 替换不同规则的所有占位符。
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_different($message, $attribute, $rule, $parameters)
	{
		return str_replace(':other', $parameters[0], $message);
	}

	/**
	 * Get the displayable name for a given attribute.
	 * 获取给定属性的可显示名称。
	 * @param  string  $attribute
	 * @return string
	 */
	protected function attribute($attribute)
	{
		$bundle = Bundle::prefix($this->bundle);

		// More reader friendly versions of the attribute names may be stored
		// in the validation language file, allowing a more readable version
		// of the attribute name to be used in the message.
        // 属性名称的更易于阅读的版本可以存储在验证语言文件中,从而允许在消息中使用更易读的属性名称版本。
		$line = "{$bundle}validation.attributes.{$attribute}";

		$display = Lang::line($line)->get($this->language);

		// If no language line has been specified for the attribute, all of
		// the underscores are removed from the attribute name and that
		// will be used as the attribtue name.
        // 如果没有为属性指定语言行,则从属性名称中删除所有下划线,并将其用作属性名称。
		if (is_null($display))
		{
			return str_replace('_', ' ', $attribute);
		}

		return $display;
	}

	/**
	 * Determine if an attribute has a rule assigned to it.
	 * 确定属性是否具有分配给它的规则。
	 * @param  string  $attribute
	 * @param  array   $rules
	 * @return bool
	 */
	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;
	}

	/**
	 * Extract the rule name and parameters from a rule.
	 * 从规则中提取规则名称和参数。
	 * @param  string  $rule
	 * @return array
	 */
	protected function parse($rule)
	{
		$parameters = array();

		// The format for specifying validation rules and parameters follows a 
		// {rule}:{parameters} formatting convention. For instance, the rule
		// "max:3" specifies that the value may only be 3 characters long.
        // 指定验证规则和参数的格式遵循 {rule}:{parameters} 格式约定。 例如,规则“max:3”指定该值可能只有 3 个字符长。
		if (($colon = strpos($rule, ':')) !== false)
		{
			$parameters = explode(',', substr($rule, $colon + 1));
		}

		return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters);
	}

	/**
	 * Set the bundle that the validator is running for.
	 * 设置验证器正在运行的包。
	 * The bundle determines which bundle the language lines will be loaded from.
	 * 包确定将从哪个包中加载语言行。
	 * @param  string     $bundle
	 * @return Validator
	 */
	public function bundle($bundle)
	{
		$this->bundle = $bundle;
		return $this;
	}

	/**
	 * Set the language that should be used when retrieving error messages.
	 * 设置检索错误消息时应使用的语言。
	 * @param  string     $language
	 * @return Validator
	 */
	public function speaks($language)
	{
		$this->language = $language;
		return $this;
	}

	/**
	 * Set the database connection that should be used by the validator.
	 * 设置验证器应使用的数据库连接。
	 * @param  Database\Connection  $connection
	 * @return Validator
	 */
	public function connection(Database\Connection $connection)
	{
		$this->db = $connection;
		return $this;
	}

	/**
	 * Get the database connection for the Validator.
	 * 获取验证器的数据库连接。
	 * @return Database\Connection
	 */
	protected function db()
	{
		if ( ! is_null($this->db)) return $this->db;

		return $this->db = Database::connection();
	}

	/**
	 * Dynamically handle calls to custom registered validators.
     * 动态处理对自定义注册验证器的调用。
	 */
	public function __call($method, $parameters)
	{
		// First we will slice the "validate_" prefix off of the validator since
		// custom validators aren't registered with such a prefix, then we can
		// just call the method with the given parameters.
        // 首先,我们将切掉验证器的“validate_”前缀,因为自定义验证器没有使用这样的前缀注册,然后我们可以使用给定的参数调用该方法。
		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.");
	}

}

github地址: https://github.com/liu-shilong/laravel3-scr   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值