1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace jin2chen\YiiValidator; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\ResultSet; |
8
|
|
|
use Yiisoft\Validator\Rule; |
9
|
|
|
use Yiisoft\Validator\RuleInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @psalm-import-type AggregateRule from Validator |
13
|
|
|
*/ |
14
|
|
|
abstract class NestRule extends Rule implements NestRuleInterface |
15
|
|
|
{ |
16
|
|
|
private ?ResultSet $resultSet = null; |
17
|
|
|
private ?Validator $validator = null; |
18
|
|
|
/** |
19
|
|
|
* @var RuleInterface[][] |
20
|
|
|
* @psalm-var AggregateRule |
21
|
|
|
*/ |
22
|
|
|
private iterable $rules = []; |
23
|
|
|
|
24
|
2 |
|
public function getResultSet(): ResultSet |
25
|
|
|
{ |
26
|
2 |
|
if (null === $this->resultSet) { |
27
|
2 |
|
$this->resultSet = new ResultSet(); |
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
|
return $this->resultSet; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function getValidator(): Validator |
34
|
|
|
{ |
35
|
2 |
|
if (null === $this->validator) { |
36
|
|
|
$this->validator = new Validator(); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
return $this->validator; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function setValidator(Validator $validator): NestRuleInterface |
43
|
|
|
{ |
44
|
2 |
|
$this->validator = $validator; |
45
|
2 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Rule[][] |
50
|
|
|
* @psalm-return AggregateRule |
51
|
|
|
*/ |
52
|
2 |
|
public function getRules(): iterable |
53
|
|
|
{ |
54
|
2 |
|
return $this->rules; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param RuleInterface[][] $rules |
59
|
|
|
* @psalm-param AggregateRule $rules |
60
|
|
|
* @return NestRuleInterface |
61
|
|
|
*/ |
62
|
1 |
|
public function withRules(iterable $rules): NestRuleInterface |
63
|
|
|
{ |
64
|
1 |
|
$new = clone $this; |
65
|
1 |
|
$new->rules = $rules; |
|
|
|
|
66
|
1 |
|
return $new; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
protected function addResultSet(ResultSet $resultSet, string $prefix): void |
70
|
|
|
{ |
71
|
2 |
|
$comResultSet = $this->getResultSet(); |
72
|
2 |
|
foreach ($resultSet as $attribute => $result) { |
73
|
2 |
|
$key = $prefix . '.' . $attribute; |
74
|
2 |
|
$comResultSet->addResult($key, $result); |
75
|
|
|
} |
76
|
2 |
|
} |
77
|
|
|
} |
78
|
|
|
|