[李景山php]每天laravel-20161002|Validator.php-2
2016-08-01 09:06
417 查看
/** * 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.
本文出自 “专注php” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1832724
相关文章推荐
- [李景山php]每天laravel-20161002|Validator.php-2
- [李景山php]每天laravel-20161003|Validator.php-3
- [李景山php]每天laravel-20161005|Validator.php-5
- [李景山php]每天laravel-20161012|Validator.php-12
- [李景山php]每天laravel-20161014|Validator.php-14
- [李景山php]每天laravel-20161003|Validator.php-3
- [李景山php]每天laravel-20161010|Validator.php-10
- [李景山php]每天laravel-20161013|Validator.php-13
- [李景山php]每天laravel-20161011|Validator.php-11
- [李景山php]每天laravel-20161004|Validator.php-4
- [李景山php]每天laravel-20161009|Validator.php-9
- [李景山php]每天laravel-20161006|Validator.php-6
- [李景山php]每天laravel-20161013|Validator.php-13
- [李景山php]每天laravel-20161001|Validator.php-1
- [李景山php]每天laravel-20161005|Validator.php-5
- [李景山php]每天laravel-20161007|Validator.php-7
- [李景山php]每天laravel-20161008|Validator.php-8
- [李景山php]每天laravel-20161014|Validator.php-14
- [李景山php]每天laravel-20161012|Validator.php-12
- [李景山php]每天laravel-20161010|Validator.php-10