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