Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Performance improvements #32532

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Illuminate/Support/MessageBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,21 @@ public function getMessageBag()
return $this;
}

/**
* Generate an array of all attributes that have messages.
*
* @return array
*/
public function attributes()
{
$attributes = [];
foreach ($this->messages as $key => $message) {
$attributes[explode('.', $key)[0]] = $key;
}

return $attributes;
}

/**
* Get the default message format.
*
Expand Down
25 changes: 24 additions & 1 deletion src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ protected function explodeWildcardRules($results, $attribute, $rules)
foreach ((array) $rules as $rule) {
$this->implicitAttributes[$attribute][] = strval($key);

$results = $this->mergeRules($results, $key, $rule);
foreach ($this->getRules($key, $rule) as $rKey => $rVal) {
$results[$rKey] = $rVal;
}
}
}
}
Expand Down Expand Up @@ -164,6 +166,27 @@ public function mergeRules($results, $attribute, $rules = [])
);
}

/**
* Get rules of a given attribute(s).
*
* @param string|array $attribute
* @param string|array $rules
* @return array
*/
public function getRules($attribute, $rules = [])
{
if (is_array($attribute)) {
$all = [];
foreach ((array) $attribute as $innerAttribute => $innerRules) {
$all[$innerRules] = head($this->explodeRules([$innerRules]));
}

return $all;
}

return [$attribute => head($this->explodeRules([$rules]))];
}

/**
* Merge additional rules into a given attribute.
*
Expand Down
37 changes: 29 additions & 8 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class Validator implements ValidatorContract
use Concerns\FormatsMessages,
Concerns\ValidatesAttributes;

/**
* Maximum number of translated error messages.
*
* @var int
*/
public static $maxMessages = 2000;

/**
* The Translator implementation.
*
Expand Down Expand Up @@ -97,6 +104,13 @@ class Validator implements ValidatorContract
*/
protected $implicitAttributes = [];

/**
* The dictionary of wildcard attributes with their asterisks expanded.
*
* @var array
*/
protected $implicitDictionary = [];

/**
* The callback that should be used to format the attribute.
*
Expand Down Expand Up @@ -520,8 +534,8 @@ protected function getExplicitKeys($attribute)
*/
protected function getPrimaryAttribute($attribute)
{
foreach ($this->implicitAttributes as $unparsed => $parsed) {
if (in_array($attribute, $parsed)) {
foreach ($this->implicitDictionary as $unparsed => $parsed) {
if (array_key_exists($attribute, $parsed)) {
return $unparsed;
}
}
Expand Down Expand Up @@ -709,9 +723,13 @@ public function addFailure($attribute, $rule, $parameters = [])
return $this->excludeAttribute($attribute);
}

$this->messages->add($attribute, $this->makeReplacements(
$this->getMessage($attribute, $rule), $attribute, $rule, $parameters
));
if (count($this->failedRules) < static::$maxMessages) {
$this->messages->add($attribute, $this->makeReplacements(
$this->getMessage($attribute, $rule), $attribute, $rule, $parameters
));
} else {
$this->messages->add($attribute, 'Invalid');
}

$this->failedRules[$attribute][$rule] = $parameters;
}
Expand Down Expand Up @@ -778,9 +796,7 @@ public function invalid()
*/
protected function attributesThatHaveMessages()
{
return collect($this->messages()->toArray())->map(function ($message, $key) {
return explode('.', $key)[0];
})->unique()->flip()->all();
return $this->messages()->attributes();
}

/**
Expand Down Expand Up @@ -957,6 +973,11 @@ public function addRules($rules)
$this->implicitAttributes = array_merge(
$this->implicitAttributes, $response->implicitAttributes
);
$this->implicitDictionary = array_merge(
$this->implicitDictionary, array_map(function ($value) {
return array_flip(array_values($value));
}, $response->implicitAttributes)
);
}

/**
Expand Down