Passed
Pull Request — master (#322)
by
unknown
02:36
created

ValidationContext::isAttributeMissing()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

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 3
nc 3
nop 0
crap 3
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 679
    public function __construct(
25
        ValidatorInterface $validator,
26
        ?DataSetInterface $dataSet,
27
        ?string $attribute = null,
28
        array $parameters = []
29
    ) {
30 679
        $this->validator = $validator;
31 679
        $this->dataSet = $dataSet;
32 679
        $this->attribute = $attribute;
33 679
        $this->parameters = $parameters;
34
    }
35
36 35
    public function getValidator(): ValidatorInterface
37
    {
38 35
        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 6
    public function getDataSet(): ?DataSetInterface
45
    {
46 6
        return $this->dataSet;
47
    }
48
49
    /**
50
     * @return string|null Validated attribute name. Null if a single value is validated.
51
     */
52 423
    public function getAttribute(): ?string
53
    {
54 423
        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 84
    public function withAttribute(?string $attribute): self
63
    {
64 84
        $new = clone $this;
65 84
        $new->attribute = $attribute;
66 84
        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 4
    public function getParameter(string $key, mixed $default = null): mixed
88
    {
89 4
        return ArrayHelper::getValue($this->parameters, $key, $default);
90
    }
91
92 87
    public function setParameter(string $key, $value): void
93
    {
94 87
        $this->parameters[$key] = $value;
95
    }
96
97 58
    public function isAttributeMissing(): bool
98
    {
99 58
        return $this->attribute !== null && $this->dataSet !== null && !$this->dataSet->hasAttribute($this->attribute);
100
    }
101
}
102