Skip to content

Commit

Permalink
[12.x] Compilable for Validation Contract (#54882)
Browse files Browse the repository at this point in the history
* Adds the compiled rules contract

* Transforms NestedRules to use a Contract

* style fix
  • Loading branch information
peterfox authored Mar 5, 2025
1 parent fd8cf8b commit 4284e61
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
17 changes: 17 additions & 0 deletions src/Illuminate/Contracts/Validation/CompilableRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Illuminate\Contracts\Validation;

interface CompilableRules
{
/**
* Compile the object into usable rules.
*
* @param string $attribute
* @param mixed $value
* @param mixed $data
* @param mixed $context
* @return \stdClass
*/
public function compile($attribute, $value, $data = null, $context = null);
}
22 changes: 3 additions & 19 deletions src/Illuminate/Validation/NestedRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Illuminate\Validation;

use Illuminate\Support\Arr;
use Illuminate\Contracts\Validation\CompilableRules;

class NestedRules
class NestedRules implements CompilableRules
{
/**
* The callback to execute.
Expand Down Expand Up @@ -37,22 +37,6 @@ public function compile($attribute, $value, $data = null, $context = null)
{
$rules = call_user_func($this->callback, $value, $attribute, $data, $context);

$parser = new ValidationRuleParser(
Arr::undot(Arr::wrap($data))
);

if (is_array($rules) && ! array_is_list($rules)) {
$nested = [];

foreach ($rules as $key => $rule) {
$nested[$attribute.'.'.$key] = $rule;
}

$rules = $nested;
} else {
$rules = [$attribute => $rules];
}

return $parser->explode(ValidationRuleParser::filterConditionalRules($rules, $data));
return Rule::compile($attribute, $rules, $data);
}
}
30 changes: 30 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Validation;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Validation\Rules\ArrayRule;
use Illuminate\Validation\Rules\Can;
Expand Down Expand Up @@ -244,4 +245,33 @@ public static function numeric()
{
return new Numeric;
}

/**
* Compile a set of rules for an attribute.
*
* @param string $attribute
* @param array $rules
* @param array|null $data
* @return object|\stdClass
*/
public static function compile($attribute, $rules, $data = null)
{
$parser = new ValidationRuleParser(
Arr::undot(Arr::wrap($data))
);

if (is_array($rules) && ! array_is_list($rules)) {
$nested = [];

foreach ($rules as $key => $rule) {
$nested[$attribute.'.'.$key] = $rule;
}

$rules = $nested;
} else {
$rules = [$attribute => $rules];
}

return $parser->explode(ValidationRuleParser::filterConditionalRules($rules, $data));
}
}
7 changes: 4 additions & 3 deletions src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Validation;

use Closure;
use Illuminate\Contracts\Validation\CompilableRules;
use Illuminate\Contracts\Validation\InvokableRule;
use Illuminate\Contracts\Validation\Rule as RuleContract;
use Illuminate\Contracts\Validation\ValidationRule;
Expand Down Expand Up @@ -138,7 +139,7 @@ protected function prepareRule($rule, $attribute)
return $rule;
}

if ($rule instanceof NestedRules) {
if ($rule instanceof CompilableRules) {
return $rule->compile(
$attribute, $this->data[$attribute] ?? null, Arr::dot($this->data), $this->data
)->rules[$attribute];
Expand All @@ -164,7 +165,7 @@ protected function explodeWildcardRules($results, $attribute, $rules)
foreach ($data as $key => $value) {
if (Str::startsWith($key, $attribute) || (bool) preg_match('/^'.$pattern.'\z/', $key)) {
foreach ((array) $rules as $rule) {
if ($rule instanceof NestedRules) {
if ($rule instanceof CompilableRules) {
$context = Arr::get($this->data, Str::beforeLast($key, '.'));

$compiled = $rule->compile($key, $value, $data, $context);
Expand Down Expand Up @@ -238,7 +239,7 @@ protected function mergeRulesForAttribute($results, $attribute, $rules)
*/
public static function parse($rule)
{
if ($rule instanceof RuleContract || $rule instanceof NestedRules) {
if ($rule instanceof RuleContract || $rule instanceof CompilableRules) {
return [$rule, []];
}

Expand Down

0 comments on commit 4284e61

Please sign in to comment.