-
Notifications
You must be signed in to change notification settings - Fork 68
/
NestedRules.php
58 lines (48 loc) · 1.28 KB
/
NestedRules.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Illuminate\Validation;
use Illuminate\Support\Arr;
class NestedRules
{
/**
* The callback to execute.
*
* @var callable
*/
protected $callback;
/**
* Create a new nested rule instance.
*
* @param callable $callback
* @return void
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
}
/**
* Compile the callback into an array of rules.
*
* @param string $attribute
* @param mixed $value
* @param mixed $data
* @param mixed $context
* @return \stdClass
*/
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));
}
}