Passed
Pull Request — master (#426)
by Sergei
02:33
created

ValidationContext::getDataSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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
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