Skip to content

Commit 4284e61

Browse files
authored
[12.x] Compilable for Validation Contract (#54882)
* Adds the compiled rules contract * Transforms NestedRules to use a Contract * style fix
1 parent fd8cf8b commit 4284e61

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Illuminate\Contracts\Validation;
4+
5+
interface CompilableRules
6+
{
7+
/**
8+
* Compile the object into usable rules.
9+
*
10+
* @param string $attribute
11+
* @param mixed $value
12+
* @param mixed $data
13+
* @param mixed $context
14+
* @return \stdClass
15+
*/
16+
public function compile($attribute, $value, $data = null, $context = null);
17+
}

src/Illuminate/Validation/NestedRules.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Illuminate\Validation;
44

5-
use Illuminate\Support\Arr;
5+
use Illuminate\Contracts\Validation\CompilableRules;
66

7-
class NestedRules
7+
class NestedRules implements CompilableRules
88
{
99
/**
1010
* The callback to execute.
@@ -37,22 +37,6 @@ public function compile($attribute, $value, $data = null, $context = null)
3737
{
3838
$rules = call_user_func($this->callback, $value, $attribute, $data, $context);
3939

40-
$parser = new ValidationRuleParser(
41-
Arr::undot(Arr::wrap($data))
42-
);
43-
44-
if (is_array($rules) && ! array_is_list($rules)) {
45-
$nested = [];
46-
47-
foreach ($rules as $key => $rule) {
48-
$nested[$attribute.'.'.$key] = $rule;
49-
}
50-
51-
$rules = $nested;
52-
} else {
53-
$rules = [$attribute => $rules];
54-
}
55-
56-
return $parser->explode(ValidationRuleParser::filterConditionalRules($rules, $data));
40+
return Rule::compile($attribute, $rules, $data);
5741
}
5842
}

src/Illuminate/Validation/Rule.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Validation;
44

55
use Illuminate\Contracts\Support\Arrayable;
6+
use Illuminate\Support\Arr;
67
use Illuminate\Support\Traits\Macroable;
78
use Illuminate\Validation\Rules\ArrayRule;
89
use Illuminate\Validation\Rules\Can;
@@ -244,4 +245,33 @@ public static function numeric()
244245
{
245246
return new Numeric;
246247
}
248+
249+
/**
250+
* Compile a set of rules for an attribute.
251+
*
252+
* @param string $attribute
253+
* @param array $rules
254+
* @param array|null $data
255+
* @return object|\stdClass
256+
*/
257+
public static function compile($attribute, $rules, $data = null)
258+
{
259+
$parser = new ValidationRuleParser(
260+
Arr::undot(Arr::wrap($data))
261+
);
262+
263+
if (is_array($rules) && ! array_is_list($rules)) {
264+
$nested = [];
265+
266+
foreach ($rules as $key => $rule) {
267+
$nested[$attribute.'.'.$key] = $rule;
268+
}
269+
270+
$rules = $nested;
271+
} else {
272+
$rules = [$attribute => $rules];
273+
}
274+
275+
return $parser->explode(ValidationRuleParser::filterConditionalRules($rules, $data));
276+
}
247277
}

src/Illuminate/Validation/ValidationRuleParser.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Validation;
44

55
use Closure;
6+
use Illuminate\Contracts\Validation\CompilableRules;
67
use Illuminate\Contracts\Validation\InvokableRule;
78
use Illuminate\Contracts\Validation\Rule as RuleContract;
89
use Illuminate\Contracts\Validation\ValidationRule;
@@ -138,7 +139,7 @@ protected function prepareRule($rule, $attribute)
138139
return $rule;
139140
}
140141

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

170171
$compiled = $rule->compile($key, $value, $data, $context);
@@ -238,7 +239,7 @@ protected function mergeRulesForAttribute($results, $attribute, $rules)
238239
*/
239240
public static function parse($rule)
240241
{
241-
if ($rule instanceof RuleContract || $rule instanceof NestedRules) {
242+
if ($rule instanceof RuleContract || $rule instanceof CompilableRules) {
242243
return [$rule, []];
243244
}
244245

0 commit comments

Comments
 (0)