Passed
Pull Request — master (#222)
by Alexander
02:58
created

ValidationContext::getValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
    private ValidatorInterface $validator;
15
    private ?DataSetInterface $dataSet;
16
    private ?string $attribute;
17
    private array $parameters;
18
19
    /**
20
     * @param DataSetInterface|null $dataSet Data set the attribute belongs to. Null if a single value is validated.
21
     * @param string|null $attribute Validated attribute name. Null if a single value is validated.
22
     * @param array $parameters Arbitrary parameters.
23
     */
24 518
    public function __construct(
25
        ValidatorInterface $validator,
26
        ?DataSetInterface $dataSet,
27
        ?string $attribute = null,
28
        array $parameters = []
29
    ) {
30 518
        $this->validator = $validator;
31 518
        $this->dataSet = $dataSet;
32 518
        $this->attribute = $attribute;
33 518
        $this->parameters = $parameters;
34
    }
35
36 16
    public function getValidator(): ValidatorInterface
37
    {
38 16
        return $this->validator;
39
    }
40
41
    /**
42
     * @return DataSetInterface|null Data set the attribute belongs to. Null if a single value is validated.
43
     */
44 2
    public function getDataSet(): ?DataSetInterface
45
    {
46 2
        return $this->dataSet;
47
    }
48
49
    /**
50
     * @return string|null Validated attribute name. Null if a single value is validated.
51
     */
52 323
    public function getAttribute(): ?string
53
    {
54 323
        return $this->attribute;
55
    }
56
57
    /**
58
     * @param string|null $attribute Validated attribute name. Null if a single value is validated.
59
     *
60
     * @return self
61
     */
62 13
    public function withAttribute(?string $attribute): self
63
    {
64 13
        $new = clone $this;
65 13
        $new->attribute = $attribute;
66 13
        return $new;
67
    }
68
69
    /**
70
     * @return array Arbitrary parameters.
71
     */
72 3
    public function getParameters(): array
73
    {
74 3
        return $this->parameters;
75
    }
76
77
    /**
78
     * Get named parameter.
79
     *
80
     * @param string $key Parameter name.
81
     * @param mixed $default Default value to return in case parameter with a given name does not exist.
82
     *
83
     * @return mixed Parameter value.
84
     *
85
     * @see ArrayHelper::getValue()
86
     */
87 1
    public function getParameter(string $key, mixed $default = null): mixed
88
    {
89 1
        return ArrayHelper::getValue($this->parameters, $key, $default);
90
    }
91
92 15
    public function setParameter(string $key, $value): void
93
    {
94 15
        $this->parameters[$key] = $value;
95
    }
96
}
97