Validator.php-12

释放双眼,带上耳机,听听看~!

/**

 * Get the inline message for a rule if it exists.

 *

 * @param  string  $attribute

 * @param  string  $lowerRule

 * @param  array   $source

 * @return string|null

 */

protected function getInlineMessage($attribute, $lowerRule, $source = null)

{//Get the inline message for a rule if it exists

    $source = $source ?: $this->customMessages;// if has source ,just use it

 

    $keys = [“{$attribute}.{$lowerRule}”, $lowerRule];// get key ,i like this type to write a new array.

 

    // First we will check for a custom message for an attribute specific rule

    // message for the fields, then we will check for a general custom line

    // that is not attribute specific. If we find either we’ll return it.

    foreach ($keys as $key) {// loop keys

        foreach (array_keys($source) as $sourceKey) {// loop source key

            if (Str::is($sourceKey, $key)) {// if it is a string

                return $source[$sourceKey];// just return the source value, because it is right, and break it.

            }

        }

    }// this is i know ,the un better function ,

}// the bigger strange method

 

/**

 * Get the custom error message from translator.

 *

 * @param  string  $customKey

 * @return string

 */

protected function getCustomMessageFromTranslator($customKey)

{// get the custom error message from the translator

    $shortKey = str_replace(‘validation.custom.’, ”, $customKey);// use str replace to get the short key

 

    $customMessages = Arr::dot(

        (array) $this->translator->trans(‘validation.custom’)

    );// get the custom Messages

 

    foreach ($customMessages as $key => $message) {// loop message

        if ($key === $shortKey || (Str::contains($key, [‘*’]) && Str::is($key, $shortKey))) {

            return $message;

        }// or ,just return it

    }

 

    return $customKey;// return it

}

 

/**

 * Get the proper error message for an attribute and size rule.

 *

 * @param  string  $attribute

 * @param  string  $rule

 * @return string

 */

protected function getSizeMessage($attribute, $rule)

{//Get the proper error message for an attribute and size rule.

    $lowerRule = Str::snake($rule);// get a format rule

 

    // There are three different types of size validations. The attribute may be

    // either a number, file, or string so we will check a few things to know

    // which type of value it is and return the correct line for that type.

    $type = $this->getAttributeType($attribute);//get Attribute Type

 

    $key = “validation.{$lowerRule}.{$type}”;// combine a key

 

    return $this->translator->trans($key);// return the translator key

}

 

/**

 * Get the data type of the given attribute.

 *

 * @param  string  $attribute

 * @return string

 */

protected function getAttributeType($attribute)

{// get the data type of the given attribute.

    // We assume that the attributes present in the file array are files so that

    // means that if the attribute does not have a numeric rule and the files

    // list doesn’t have it we’ll just consider it a string by elimination.

    if ($this->hasRule($attribute, $this->numericRules)) {

        return ‘numeric’;// return a type

    } elseif ($this->hasRule($attribute, [‘Array’])) {

        return ‘array’;// type is array

    } elseif (array_key_exists($attribute, $this->files)) {

        return ‘file’;// type a file

    }

 

    return ‘string’;// normal this is a string,

 //every thing can be make like a sting

}

 

/**

 * 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 doReplacements($message, $attribute, $rule, $parameters)

{//replace all error message place-holders with actual values.

    $value = $this->getAttribute($attribute);// value this get attribute

 

    $message = str_replace(// str_replace  has a supper good type.

        [‘:ATTRIBUTE’, ‘:Attribute’, ‘:attribute’],

        [Str::upper($value), Str::ucfirst($value), $value],

        $message

    );

 

    if (isset($this->replacers[Str::snake($rule)])) {// if isset this replacer

        $message = $this->callReplacer($message, $attribute, Str::snake($rule), $parameters);

    } elseif (method_exists($this, $replacer = “replace{$rule}”)) {

        $message = $this->$replacer($message, $attribute, $rule, $parameters);

    }

 

    return $message;// return message

}

 

/**

 * Transform an array of attributes to their displayable form.

 *

 * @param  array  $values

 * @return array

 */

protected function getAttributeList(array $values)

{//Transform an array of attributes to their display able form.

    $attributes = [];// init this attributes

 

    // For each attribute in the list we will simply get its displayable form as

    // this is convenient when replacing lists of parameters like some of the

    // replacement functions do when formatting out the validation message.

    foreach ($values as $key => $value) {

        $attributes[$key] = $this->getAttribute($value);

    }

 

    return $attributes;

}

【转自慕课】https://www.imooc.com

PHP

PHP教程 php中0与空 Null false的区别

2022-3-1 14:41:15

PHP

Validator.php-2

2022-3-1 15:06:26

搜索