为了账号安全,请及时绑定邮箱和手机立即绑定

Validator.php-2

标签:
PHP

/**

 * After an after validation callback.

 *

 * @param  callable|string  $callback

 * @return $this

 */

public function after($callback)

{

    $this->after[] = function () use ($callback) {

        return call_user_func_array($callback, [$this]);

    };// register after function // a closure function, a long include.

 // function name is $callback, and parameters it is array wrap this object

 // just get this result

 

    return $this;

}// a normal function that is a after

 

/**

 * Add conditions to a given field based on a Closure.

 *

 * @param  string  $attribute

 * @param  string|array  $rules

 * @param  callable  $callback

 * @return void

 */

public function sometimes($attribute, $rules, callable $callback)

{

    $payload = new Fluent($this->attributes());// get the parameters  a parameters wrap class

 

    if (call_user_func($callback, $payload)) {// if this function back is true

        foreach ((array) $attribute as $key) {// foreach every value

            $this->mergeRules($key, $rules);// add every value ,merge this rule

        }

    }// a crazy method

}//add conditions to a given field based on a Closure

 

/**

 * Define a set of rules that apply to each element in an array attribute.

 *

 * @param  string  $attribute

 * @param  string|array  $rules

 * @return void

 *

 * @throws \InvalidArgumentException

 */

public function each($attribute, $rules)

{// set a rules for each element

    $data = Arr::dot($this->initializeAttributeOnData($attribute));// use a special method to get the data that you want

 

    $pattern = str_replace('\*', '[^\.]+', preg_quote($attribute));//set a pattern

 

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

        if (Str::startsWith($key, $attribute) || (bool) preg_match('/^'.$pattern.'\z/', $key)) {

            foreach ((array) $rules as $ruleKey => $ruleValue) {

                if (! is_string($ruleKey) || Str::endsWith($key, $ruleKey)) {

                    $this->mergeRules($key, $ruleValue);

                }// mergeRules

            }//rules as rules key and value

        }// Str::startsWith determine the key and attribute

    }// loop all of this data

}

 

/**

 * Gather a copy of the data filled with any missing attributes.

 *

 * @param  string  $attribute

 * @return array

 */

protected function initializeAttributeOnData($attribute)

{

    if (! Str::contains($attribute, '*') || Str::endsWith($attribute, '*')) {

        return $this->data;

    }// if it is ok! just return it

 

    $data = $this->data;// a real copy

 

    return data_fill($data, $attribute, null);// a user method

}//Gather a copy of the data filled with any missing attributes

 

/**

 * Merge additional rules into a given attribute.

 *

 * @param  string  $attribute

 * @param  string|array  $rules

 * @return void

 */

public function mergeRules($attribute, $rules)

{

    $current = isset($this->rules[$attribute]) ? $this->rules[$attribute] : [];

 // set normal current array

 

    $merge = head($this->explodeRules([$rules]));// get merge ,use this head function

 

    $this->rules[$attribute] = array_merge($current, $merge);// set the rules

}//Merge additional rules into a given attribute

 

/**

 * Determine if the data passes the validation rules.

 *

 * @return bool

 */

public function passes()

{//Determine if the data passes the validation rules

    $this->messages = new MessageBag;// a instance about this Messages

 

    // We'll spin through each rule, validating the attributes attached to that

    // rule. Any error messages will be added to the containers with each of

    // the other error messages, returning true if we don't have messages.

    foreach ($this->rules as $attribute => $rules) {

        foreach ($rules as $rule) {

            $this->validate($attribute, $rule);// check or

 

            if ($this->shouldStopValidating($attribute)) {//break it

                break;

            }

        }

    }//two layer foreach

 

    // Here we will spin through all of the "after" hooks on this validator and

    // fire them off. This gives the callbacks a chance to perform all kinds

    // of other validation that needs to get wrapped up in this operation.

    foreach ($this->after as $after) {

        call_user_func($after);

    }//Here we will spin through all of the "after" hooks on this validator and fire them

 // off. this gives the callbacks a chance to perform all kinds of other validation that

 // needs to get wrapped up in this operation

 

    return count($this->messages->all()) === 0;

}

 

/**

 * Determine if the data fails the validation rules.

 *

 * @return bool

 */

public function fails()

{

    return ! $this->passes();

}//Determine if the data fails the validation rules.

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消