/**
* 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.
共同学习,写下你的评论
评论加载中...
作者其他优质文章