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