Passed
Push — master ( bb4195...327050 )
by Sergei
02:53
created

ValidationContext::getDataSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Validator\Helper\DataSetNormalizer;
9
10
/**
11
 * Validation context that might be taken into account when performing validation.
12
 *
13
 * @psalm-import-type RulesType from ValidatorInterface
14
 */
15
final class ValidationContext
16
{
17
    /**
18
     * @param DataSetInterface|null $dataSet Data set the attribute belongs to. Null if a single value is validated.
19 827
     * @param mixed $rawData The raw validated data.
20
     * @param string|null $attribute Validated attribute name. Null if a single value is validated.
21
     * @param array $parameters Arbitrary parameters.
22
     */
23
    public function __construct(
24
        private ValidatorInterface $validator,
25
        private mixed $rawData,
26
        private ?DataSetInterface $dataSet = null,
27 43
        private ?string $attribute = null,
28
        private array $parameters = []
29 43
    ) {
30
    }
31
32
    /**
33
     * Validate data in current context.
34
     *
35 11
     * @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 11
     * {@see RulesProviderInterface::getRules()}.
38
     * @param callable|iterable|object|string|null $rules Rules to apply. If specified, rules are not read from data set
39
     * even if it is an instance of {@see RulesProviderInterface}.
40
     *
41
     * @psalm-param RulesType $rules
42
     */
43 503
    public function validate(mixed $data, callable|iterable|object|string|null $rules = null): Result
44
    {
45 503
        $currentDataSet = $this->dataSet;
46
        $currentAttribute = $this->attribute;
47
48
        $dataSet = DataSetNormalizer::normalize($data);
49
        $this->dataSet = $dataSet;
50
        $this->attribute = null;
51 140
52
        $result = $this->validator->validate($dataSet, $rules, $this);
53 140
54 140
        $this->dataSet = $currentDataSet;
55
        $this->attribute = $currentAttribute;
56
57
        return $result;
58
    }
59
60 3
    /**
61
     * @return mixed The raw validated data.
62 3
     */
63
    public function getRawData(): mixed
64
    {
65
        return $this->rawData;
66
    }
67
68
    /**
69
     * @return DataSetInterface|null Data set the attribute belongs to. Null if a single value is validated.
70
     */
71
    public function getDataSet(): ?DataSetInterface
72
    {
73
        return $this->dataSet;
74
    }
75 6
76
    /**
77 6
     * @return string|null Validated attribute name. Null if a single value is validated.
78
     */
79
    public function getAttribute(): ?string
80 503
    {
81
        return $this->attribute;
82 503
    }
83
84
    /**
85 786
     * @param string|null $attribute Validated attribute name. Null if a single value is validated.
86
     */
87 786
    public function setAttribute(?string $attribute): self
88
    {
89
        $this->attribute = $attribute;
90
        return $this;
91
    }
92
93
    /**
94
     * @return array Arbitrary parameters.
95
     */
96
    public function getParameters(): array
97
    {
98
        return $this->parameters;
99
    }
100
101
    /**
102
     * Get named parameter.
103
     *
104
     * @param string $key Parameter name.
105
     * @param mixed $default Default value to return in case parameter with a given name does not exist.
106
     *
107
     * @return mixed Parameter value.
108
     *
109
     * @see ArrayHelper::getValue()
110
     */
111
    public function getParameter(string $key, mixed $default = null): mixed
112
    {
113
        return ArrayHelper::getValue($this->parameters, $key, $default);
114
    }
115
116
    public function setParameter(string $key, mixed $value): void
117
    {
118
        $this->parameters[$key] = $value;
119
    }
120
121
    public function isAttributeMissing(): bool
122
    {
123
        return $this->attribute !== null && $this->dataSet !== null && !$this->dataSet->hasAttribute($this->attribute);
124
    }
125
}
126