[李景山php]每天laravel-20161012|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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值