您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天laravel-20161013|Validator.php-13

2016-08-16 09:03 423 查看
/**
* Get the displayable name of the attribute.
*
* @param  string  $attribute
* @return string
*/
protected function getAttribute($attribute)
{// Get the displayable name of the attribute
// The developer may dynamically specify the array of custom attributes
// on this Validator instance. If the attribute exists in this array
// it takes precedence over all other ways we can pull attributes.
if (isset($this->customAttributes[$attribute])) {
return $this->customAttributes[$attribute];
}// the first precedence  of attribute is the dynamically value.
// will over all other ways we can pull attribute

$key = "validation.attributes.{$attribute}";// get a complete key

// We allow for the developer to specify language lines for each of the
// attributes allowing for more displayable counterparts of each of
// the attributes. This provides the ability for simple formats.
if (($line = $this->translator->trans($key)) !== $key) {
return $line;
}// from language lines to translator key to a real key
// specify language lines , a 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 default versions of the attribute's displayable names.
return str_replace('_', ' ', Str::snake($attribute));// no language lines, so use the default
}

/**
* Get the displayable name of the value.
*
* @param  string  $attribute
* @param  mixed   $value
* @return string
*/
public function getDisplayableValue($attribute, $value)
{//Get the displayable name of the value.
if (isset($this->customValues[$attribute][$value])) {
return $this->customValues[$attribute][$value];
}// if it is been set,just return it

$key = "validation.values.{$attribute}.{$value}";// other get the complete key

if (($line = $this->translator->trans($key)) !== $key) {// and use the trans function to get the language
return $line;
}// just return it

return $value;// return value
}

/**
* Replace all place-holders for the between rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceBetween($message, $attribute, $rule, $parameters)
{//replace all place-holders for the between rule.
return str_replace([':min', ':max'], $parameters, $message);
}// return the real parameters

/**
* Replace all place-holders for the digits rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceDigits($message, $attribute, $rule, $parameters)
{
return str_replace(':digits', $parameters[0], $message);
}//replace all place-holders for the digits rule

/**
* Replace all place-holders for the digits (between) rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceDigitsBetween($message, $attribute, $rule, $parameters)
{
return $this->replaceBetween($message, $attribute, $rule, $parameters);
}// replace all place-holders for the digits (between) rule.

/**
* Replace all place-holders for the size rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceSize($message, $attribute, $rule, $parameters)
{
return str_replace(':size', $parameters[0], $message);
}// replace the :size

/**
* Replace all place-holders for the min rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceMin($message, $attribute, $rule, $parameters)
{
return str_replace(':min', $parameters[0], $message);
}// replace :min

/**
* Replace all place-holders for the max rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceMax($message, $attribute, $rule, $parameters)
{
return str_replace(':max', $parameters[0], $message);
}//Replace all place-holders for the max rule.

/**
* Replace all place-holders for the in rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceIn($message, $attribute, $rule, $parameters)
{
foreach ($parameters as &$parameter) {
$parameter = $this->getDisplayableValue($attribute, $parameter);
}// first loop it
// get the display able value
// just return it ,from the variable address

return str_replace(':values', implode(', ', $parameters), $message);// just :values
}//replaceIn

/**
* Replace all place-holders for the not_in rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceNotIn($message, $attribute, $rule, $parameters)
{
return $this->replaceIn($message, $attribute, $rule, $parameters);
}// replace all place-holders for the not_in role
// replace In

/**
* Replace all place-holders for the mimes rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceMimes($message, $attribute, $rule, $parameters)
{
return str_replace(':values', implode(', ', $parameters), $message);
}// new replace the :values

/**
* Replace all place-holders for the required_with rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceRequiredWith($message, $attribute, $rule, $parameters)
{
$parameters = $this->getAttributeList($parameters);

return str_replace(':values', implode(' / ', $parameters), $message);
}//replace all place-holders for implode

/**
* Replace all place-holders for the required_with_all rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceRequiredWithAll($message, $attribute, $rule, $parameters)
{
return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
}// wrap a function that be replace require with

/**
* Replace all place-holders for the required_without rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceRequiredWithout($message, $attribute, $rule, $parameters)
{
return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
}//replace all place-holders for the required_without rule.

/**
* Replace all place-holders for the required_without_all rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceRequiredWithoutAll($message, $attribute, $rule, $parameters)
{
return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
}// replace require with all

/**
* Replace all place-holders for the required_if rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceRequiredIf($message, $attribute, $rule, $parameters)
{
$parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));
// get parameters 1
$parameters[0] = $this->getAttribute($parameters[0]);
// get parameters 0
return str_replace([':other', ':value'], $parameters, $message);
}//replace all place-holders for the required_if rule.

/**
* Replace all place-holders for the required_unless rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceRequiredUnless($message, $attribute, $rule, $parameters)
{
$other = $this->getAttribute(array_shift($parameters));// get other attribute

return str_replace([':other', ':values'], [$other, implode(', ', $parameters)], $message);
}// replace all place-holders for the required_unless rule.

/**
* Replace all place-holders for the same rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceSame($message, $attribute, $rule, $parameters)
{
return str_replace(':other', $this->getAttribute($parameters[0]), $message);
}//replace other

/**
* Replace all place-holders for the different rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceDifferent($message, $attribute, $rule, $parameters)
{
return $this->replaceSame($message, $attribute, $rule, $parameters);
}// replace all of the same

/**
* Replace all place-holders for the date_format rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceDateFormat($message, $attribute, $rule, $parameters)
{
return str_replace(':format', $parameters[0], $message);
}// replace format

/**
* Replace all place-holders for the before rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceBefore($message, $attribute, $rule, $parameters)
{
if (! (strtotime($parameters[0]))) {
return str_replace(':date', $this->getAttribute($parameters[0]), $message);
}

return str_replace(':date', $parameters[0], $message);
}//replace before

/**
* Replace all place-holders for the after rule.
*
* @param  string  $message
* @param  string  $attribute
* @param  string  $rule
* @param  array   $parameters
* @return string
*/
protected function replaceAfter($message, $attribute, $rule, $parameters)
{
return $this->replaceBefore($message, $attribute, $rule, $parameters);
}//after

/**
* Get all attributes.
*
* @return array
*/
public function attributes()
{
return array_merge($this->data, $this->files);
}// attribute

/**
* Checks if an attribute exists.
*
* @param  string  $attribute
* @return bool
*/
public function hasAttribute($attribute)
{
return Arr::has($this->attributes(), $attribute);
}// attribute

/**
* Determine if the given attribute has a rule in the given set.
*
* @param  string  $attribute
* @param  string|array  $rules
* @return bool
*/
protected function hasRule($attribute, $rules)
{
return ! is_null($this->getRule($attribute, $rules));
}// rule

/**
* Get a rule and its parameters for a given attribute.
*
* @param  string  $attribute
* @param  string|array  $rules
* @return array|null
*/
protected function getRule($attribute, $rules)
{
if (! array_key_exists($attribute, $this->rules)) {
return;
}// get rule

$rules = (array) $rules;// change to array

foreach ($this->rules[$attribute] as $rule) {
list($rule, $parameters) = $this->parseRule($rule);// get the rule and parameter

if (in_array($rule, $rules)) {// if in array
return [$rule, $parameters];// just return it
}
}// loop the rule
}

/**
* Extract the rule name and parameters from a rule.
*
* @param  array|string  $rules
* @return array
*/
protected function parseRule($rules)
{
if (is_array($rules)) {
$rules = $this->parseArrayRule($rules);// Array
} else {
$rules = $this->parseStringRule($rules);// String
}

$rules[0] = $this->normalizeRule($rules[0]);// normal

return $rules;
}//parse Rule
// use a wrap function

/**
* Parse an array based rule.
*
* @param  array  $rules
* @return array
*/
protected function parseArrayRule(array $rules)
{
return [Str::studly(trim(Arr::get($rules, 0))), array_slice($rules, 1)];
}//parse Array rule
// too complex

/**
* Parse a string based rule.
*
* @param  string  $rules
* @return array
*/
protected function parseStringRule($rules)
{
$parameters = [];//parse String Rule ,just set the parameters

// The format for specifying validation rules and parameters follows an
// easy {rule}:{parameters} formatting convention. For instance the
// rule "Max:3" states that the value may only be three letters.
if (strpos($rules, ':') !== false) {
list($rules, $parameter) = explode(':', $rules, 2);

$parameters = $this->parseParameters($rules, $parameter);
}// the format for specifying validation rules and parameters follows an
// easy like {rule}:{parameters} formatting convention.
// for instance the rule "Max:3"
//states that the value may only be three letters.

return [Str::studly(trim($rules)), $parameters];
}// return a format
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  return developer function