44
55namespace Yiisoft \Validator ;
66
7+ use RuntimeException ;
78use Yiisoft \Arrays \ArrayHelper ;
8- use Yiisoft \Validator \Helper \DataSetNormalizer ;
99
1010/**
1111 * Validation context that might be taken into account when performing validation.
1414 */
1515final class ValidationContext
1616{
17+ private ?ValidatorInterface $ validator = null ;
18+
19+ /**
20+ * @var mixed The raw validated data.
21+ */
22+ private mixed $ rawData = null ;
23+
24+ /**
25+ * @var DataSetInterface|null Data set the attribute belongs to. Null if data set not set.
26+ */
27+ private ?DataSetInterface $ dataSet = null ;
28+
29+ /**
30+ * @var string|null Validated attribute name. Null if a single value is validated.
31+ */
32+ private ?string $ attribute = null ;
33+
1734 /**
18- * @param DataSetInterface|null $dataSet Data set the attribute belongs to. Null if a single value is validated.
19- * @param mixed $rawData The raw validated data.
20- * @param string|null $attribute Validated attribute name. Null if a single value is validated.
2135 * @param array $parameters Arbitrary parameters.
2236 */
2337 public function __construct (
24- private ValidatorInterface $ validator ,
25- private mixed $ rawData ,
26- private ?DataSetInterface $ dataSet = null ,
27- private ?string $ attribute = null ,
2838 private array $ parameters = []
2939 ) {
3040 }
3141
42+ public function setValidatorAndRawDataOnce (ValidatorInterface $ validator , mixed $ rawData ): self
43+ {
44+ if ($ this ->validator !== null ) {
45+ return $ this ;
46+ }
47+
48+ $ this ->validator = $ validator ;
49+ $ this ->rawData = $ rawData ;
50+
51+ return $ this ;
52+ }
53+
3254 /**
3355 * Validate data in current context.
3456 *
35- * @param DataSetInterface|mixed|RulesProviderInterface $data Data set to validate. If {@see RulesProviderInterface}
36- * instance provided and rules are not specified explicitly, they are read from the
37- * {@see RulesProviderInterface::getRules()}.
57+ * @param mixed $data Data set to validate. If {@see RulesProviderInterface} instance provided and rules are
58+ * not specified explicitly, they are read from the {@see RulesProviderInterface::getRules()}.
3859 * @param callable|iterable|object|string|null $rules Rules to apply. If specified, rules are not read from data set
3960 * even if it is an instance of {@see RulesProviderInterface}.
4061 *
4162 * @psalm-param RulesType $rules
4263 */
4364 public function validate (mixed $ data , callable |iterable |object |string |null $ rules = null ): Result
4465 {
66+ $ this ->checkValidatorAndRawData ();
67+
4568 $ currentDataSet = $ this ->dataSet ;
4669 $ currentAttribute = $ this ->attribute ;
4770
48- $ dataSet = DataSetNormalizer::normalize ($ data );
49- $ this ->dataSet = $ dataSet ;
50- $ this ->attribute = null ;
51-
52- $ result = $ this ->validator ->validate ($ dataSet , $ rules , $ this );
71+ $ result = $ this ->validator ->validate ($ data , $ rules , $ this );
5372
5473 $ this ->dataSet = $ currentDataSet ;
5574 $ this ->attribute = $ currentAttribute ;
@@ -62,17 +81,31 @@ public function validate(mixed $data, callable|iterable|object|string|null $rule
6281 */
6382 public function getRawData (): mixed
6483 {
84+ $ this ->checkValidatorAndRawData ();
6585 return $ this ->rawData ;
6686 }
6787
6888 /**
69- * @return DataSetInterface|null Data set the attribute belongs to. Null if a single value is validated .
89+ * @return DataSetInterface Data set the attribute belongs to.
7090 */
71- public function getDataSet (): ? DataSetInterface
91+ public function getDataSet (): DataSetInterface
7292 {
93+ if ($ this ->dataSet === null ) {
94+ throw new RuntimeException ('Data set in validation context is not set. ' );
95+ }
96+
7397 return $ this ->dataSet ;
7498 }
7599
100+ /**
101+ * @param DataSetInterface $dataSet Data set the attribute belongs to.
102+ */
103+ public function setDataSet (DataSetInterface $ dataSet ): self
104+ {
105+ $ this ->dataSet = $ dataSet ;
106+ return $ this ;
107+ }
108+
76109 /**
77110 * @return string|null Validated attribute name. Null if a single value is validated.
78111 */
@@ -90,14 +123,6 @@ public function setAttribute(?string $attribute): self
90123 return $ this ;
91124 }
92125
93- /**
94- * @return array Arbitrary parameters.
95- */
96- public function getParameters (): array
97- {
98- return $ this ->parameters ;
99- }
100-
101126 /**
102127 * Get named parameter.
103128 *
@@ -113,13 +138,24 @@ public function getParameter(string $key, mixed $default = null): mixed
113138 return ArrayHelper::getValue ($ this ->parameters , $ key , $ default );
114139 }
115140
116- public function setParameter (string $ key , mixed $ value ): void
141+ public function setParameter (string $ key , mixed $ value ): self
117142 {
118143 $ this ->parameters [$ key ] = $ value ;
144+ return $ this ;
119145 }
120146
121147 public function isAttributeMissing (): bool
122148 {
123149 return $ this ->attribute !== null && $ this ->dataSet !== null && !$ this ->dataSet ->hasAttribute ($ this ->attribute );
124150 }
151+
152+ /**
153+ * @psalm-assert ValidatorInterface $this->validator
154+ */
155+ private function checkValidatorAndRawData (): void
156+ {
157+ if ($ this ->validator === null ) {
158+ throw new RuntimeException ('Validator and raw data in validation context is not set. ' );
159+ }
160+ }
125161}
0 commit comments