|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Validation context that rule may take into account when performing validation. |
|
11
|
|
|
*/ |
|
12
|
|
|
final class ValidationContext |
|
13
|
|
|
{ |
|
14
|
|
|
private ValidatorInterface $validator; |
|
15
|
|
|
private ?DataSetInterface $dataSet; |
|
16
|
|
|
private ?string $attribute; |
|
17
|
|
|
private array $parameters; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param DataSetInterface|null $dataSet Data set the attribute belongs to. Null if a single value is validated. |
|
21
|
|
|
* @param string|null $attribute Validated attribute name. Null if a single value is validated. |
|
22
|
|
|
* @param array $parameters Arbitrary parameters. |
|
23
|
|
|
*/ |
|
24
|
518 |
|
public function __construct( |
|
25
|
|
|
ValidatorInterface $validator, |
|
26
|
|
|
?DataSetInterface $dataSet, |
|
27
|
|
|
?string $attribute = null, |
|
28
|
|
|
array $parameters = [] |
|
29
|
|
|
) { |
|
30
|
518 |
|
$this->validator = $validator; |
|
31
|
518 |
|
$this->dataSet = $dataSet; |
|
32
|
518 |
|
$this->attribute = $attribute; |
|
33
|
518 |
|
$this->parameters = $parameters; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
16 |
|
public function getValidator(): ValidatorInterface |
|
37
|
|
|
{ |
|
38
|
16 |
|
return $this->validator; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return DataSetInterface|null Data set the attribute belongs to. Null if a single value is validated. |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function getDataSet(): ?DataSetInterface |
|
45
|
|
|
{ |
|
46
|
2 |
|
return $this->dataSet; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string|null Validated attribute name. Null if a single value is validated. |
|
51
|
|
|
*/ |
|
52
|
323 |
|
public function getAttribute(): ?string |
|
53
|
|
|
{ |
|
54
|
323 |
|
return $this->attribute; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string|null $attribute Validated attribute name. Null if a single value is validated. |
|
59
|
|
|
* |
|
60
|
|
|
* @return self |
|
61
|
|
|
*/ |
|
62
|
13 |
|
public function withAttribute(?string $attribute): self |
|
63
|
|
|
{ |
|
64
|
13 |
|
$new = clone $this; |
|
65
|
13 |
|
$new->attribute = $attribute; |
|
66
|
13 |
|
return $new; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return array Arbitrary parameters. |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function getParameters(): array |
|
73
|
|
|
{ |
|
74
|
3 |
|
return $this->parameters; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get named parameter. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $key Parameter name. |
|
81
|
|
|
* @param mixed $default Default value to return in case parameter with a given name does not exist. |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed Parameter value. |
|
84
|
|
|
* |
|
85
|
|
|
* @see ArrayHelper::getValue() |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getParameter(string $key, mixed $default = null): mixed |
|
88
|
|
|
{ |
|
89
|
1 |
|
return ArrayHelper::getValue($this->parameters, $key, $default); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
15 |
|
public function setParameter(string $key, $value): void |
|
93
|
|
|
{ |
|
94
|
15 |
|
$this->parameters[$key] = $value; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|