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 might be taken into account when performing validation. |
11
|
|
|
*/ |
12
|
|
|
final class ValidationContext |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param DataSetInterface|null $dataSet Data set the attribute belongs to. Null if a single value is validated. |
16
|
|
|
* @param mixed $rawData The raw validated data. |
17
|
|
|
* @param string|null $attribute Validated attribute name. Null if a single value is validated. |
18
|
|
|
* @param array $parameters Arbitrary parameters. |
19
|
827 |
|
*/ |
20
|
|
|
public function __construct( |
21
|
|
|
private ValidatorInterface $validator, |
22
|
|
|
private mixed $rawData, |
23
|
|
|
private ?DataSetInterface $dataSet = null, |
24
|
|
|
private ?string $attribute = null, |
25
|
|
|
private array $parameters = [] |
26
|
|
|
) { |
27
|
43 |
|
} |
28
|
|
|
|
29
|
43 |
|
public function getValidator(): ValidatorInterface |
30
|
|
|
{ |
31
|
|
|
return $this->validator; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
11 |
|
* @return mixed The raw validated data. |
36
|
|
|
*/ |
37
|
11 |
|
public function getRawData(): mixed |
38
|
|
|
{ |
39
|
|
|
return $this->rawData; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
503 |
|
* @return DataSetInterface|null Data set the attribute belongs to. Null if a single value is validated. |
44
|
|
|
*/ |
45
|
503 |
|
public function getDataSet(): ?DataSetInterface |
46
|
|
|
{ |
47
|
|
|
return $this->dataSet; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
140 |
|
* @return string|null Validated attribute name. Null if a single value is validated. |
52
|
|
|
*/ |
53
|
140 |
|
public function getAttribute(): ?string |
54
|
140 |
|
{ |
55
|
|
|
return $this->attribute; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string|null $attribute Validated attribute name. Null if a single value is validated. |
60
|
3 |
|
*/ |
61
|
|
|
public function setAttribute(?string $attribute): self |
62
|
3 |
|
{ |
63
|
|
|
$this->attribute = $attribute; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array Arbitrary parameters. |
69
|
|
|
*/ |
70
|
|
|
public function getParameters(): array |
71
|
|
|
{ |
72
|
|
|
return $this->parameters; |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
/** |
76
|
|
|
* Get named parameter. |
77
|
6 |
|
* |
78
|
|
|
* @param string $key Parameter name. |
79
|
|
|
* @param mixed $default Default value to return in case parameter with a given name does not exist. |
80
|
503 |
|
* |
81
|
|
|
* @return mixed Parameter value. |
82
|
503 |
|
* |
83
|
|
|
* @see ArrayHelper::getValue() |
84
|
|
|
*/ |
85
|
786 |
|
public function getParameter(string $key, mixed $default = null): mixed |
86
|
|
|
{ |
87
|
786 |
|
return ArrayHelper::getValue($this->parameters, $key, $default); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function setParameter(string $key, mixed $value): void |
91
|
|
|
{ |
92
|
|
|
$this->parameters[$key] = $value; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function isAttributeMissing(): bool |
96
|
|
|
{ |
97
|
|
|
return $this->attribute !== null && $this->dataSet !== null && !$this->dataSet->hasAttribute($this->attribute); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|