|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Validator; |
|
3
|
|
|
|
|
4
|
|
|
use Closure; |
|
5
|
|
|
use Wandu\Validator\Contracts\Rule; |
|
6
|
|
|
use Wandu\Validator\Contracts\RuleDefinition; |
|
7
|
|
|
|
|
8
|
|
|
class AssertRuleDefinition implements RuleDefinition |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var \Wandu\Validator\TesterFactory */ |
|
11
|
|
|
protected $tester; |
|
12
|
|
|
|
|
13
|
|
|
/** @var \Wandu\Validator\ErrorBag */ |
|
14
|
|
|
protected $errors; |
|
15
|
3 |
|
|
|
16
|
|
|
/** @var mixed $data */ |
|
17
|
3 |
|
protected $data; |
|
18
|
3 |
|
|
|
19
|
3 |
|
/** @var mixed $origin */ |
|
20
|
|
|
protected $origin; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(TesterFactory $tester, ErrorBag $errors, $data, $origin) |
|
23
|
|
|
{ |
|
24
|
3 |
|
$this->tester = $tester; |
|
25
|
|
|
$this->errors = $errors; |
|
26
|
3 |
|
$this->data = $data; |
|
27
|
3 |
|
$this->origin = $origin; |
|
28
|
|
|
} |
|
29
|
3 |
|
|
|
30
|
3 |
|
/** |
|
31
|
3 |
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
3 |
|
public function prop(string $target, ...$rules) |
|
34
|
3 |
|
{ |
|
35
|
3 |
|
$targetName = $target; |
|
36
|
|
|
$iterable = 0; |
|
37
|
3 |
|
$optional = false; |
|
38
|
1 |
|
|
|
39
|
1 |
|
if (strpos($targetName, '?') !== false) { |
|
40
|
|
|
$targetName = trim($targetName, '?'); |
|
41
|
|
|
$optional = true; |
|
42
|
3 |
|
} |
|
43
|
3 |
|
while (strpos($targetName, "[]") !== false) { |
|
44
|
2 |
|
$targetName = str_replace("[]", "", $targetName); |
|
45
|
2 |
|
$iterable++; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
if ($optional && !isset($this->data[$targetName])) return; |
|
49
|
|
|
if (!isset($this->data[$targetName])) { |
|
50
|
|
|
$this->errors->store("required", [$targetName]); |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
3 |
|
|
|
54
|
3 |
|
if ($iterable && !is_array($this->data[$targetName])) { |
|
55
|
1 |
|
$this->errors->store("array", [$targetName]); |
|
56
|
1 |
|
return; |
|
57
|
1 |
|
} |
|
58
|
1 |
|
|
|
59
|
1 |
|
$this->errors->pushPrefix($targetName); |
|
60
|
|
|
if ($iterable) { |
|
61
|
|
|
foreach ($this->data[$targetName] as $index => $subData) { |
|
62
|
1 |
|
$this->errors->pushPrefix($index); |
|
63
|
|
|
$this->checkRules($rules, $subData, $this->origin); |
|
64
|
|
|
$this->errors->popPrefix(); |
|
65
|
|
|
} |
|
66
|
1 |
|
} else { |
|
67
|
|
|
$this->checkRules($rules, $this->data[$targetName], $this->origin); |
|
68
|
|
|
} |
|
69
|
3 |
|
$this->errors->popPrefix(); |
|
70
|
3 |
|
} |
|
71
|
1 |
|
|
|
72
|
|
|
/** |
|
73
|
3 |
|
* @param string[]|\Wandu\Validator\Contracts\Rule[]|\Closure[] $rules |
|
74
|
3 |
|
* @param mixed $data |
|
75
|
|
|
* @param mixed $origin |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function checkRules(array $rules, $data, $origin) |
|
78
|
|
|
{ |
|
79
|
3 |
|
foreach ($rules as $rule) { |
|
80
|
3 |
|
if ($rule instanceof Rule) { |
|
81
|
|
|
$rule->define(new AssertRuleDefinition($this->tester, $this->errors, $data, $origin)); |
|
82
|
|
|
} elseif ($rule instanceof Closure) { |
|
83
|
|
|
$rule->__invoke(new AssertRuleDefinition($this->tester, $this->errors, $data, $origin)); |
|
84
|
|
|
} else { |
|
85
|
|
|
if (!$this->tester->parse($rule)->test($data, $origin)) { |
|
86
|
|
|
$this->errors->store($rule); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|