Passed
Push — master ( 2b6758...acf834 )
by Aleksei
06:59
created

Boolean::getOptions()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 1
nop 0
dl 0
loc 13
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\DataSetInterface;
8
use Yiisoft\Validator\HasValidationErrorMessage;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\Rule;
11
12
/**
13
 * BooleanValidator checks if the attribute value is a boolean value or a value corresponding to it.
14
 */
15
class Boolean extends Rule
16
{
17
    use HasValidationErrorMessage;
18
    /**
19
     * @var mixed the value representing true status. Defaults to '1'.
20
     */
21
    private $trueValue = '1';
22
    /**
23
     * @var mixed the value representing false status. Defaults to '0'.
24
     */
25
    private $falseValue = '0';
26
    /**
27
     * @var bool whether the comparison to [[trueValue]] and [[falseValue]] is strict.
28
     * When this is true, the attribute value and type must both match those of [[trueValue]] or [[falseValue]].
29
     * Defaults to false, meaning only the value needs to be matched.
30
     */
31
    private bool $strict = false;
32
33
    private string $message = 'The value must be either "{true}" or "{false}".';
34
35
    public function trueValue($value): self
36
    {
37
        $new = clone $this;
38
        $new->trueValue = $value;
39
        return $new;
40
    }
41
42
    public function falseValue($value): self
43
    {
44
        $new = clone $this;
45
        $new->falseValue = $value;
46
        return $new;
47
    }
48
49
    public function strict(bool $value): self
50
    {
51
        $new = clone $this;
52
        $new->strict = $value;
53
        return $new;
54
    }
55
56 17
    protected function validateValue($value, DataSetInterface $dataSet = null): Result
57
    {
58 17
        if ($this->strict) {
59 8
            $valid = $value === $this->trueValue || $value === $this->falseValue;
60
        } else {
61 9
            $valid = $value == $this->trueValue || $value == $this->falseValue;
62
        }
63
64 17
        $result = new Result();
65
66 17
        if (!$valid) {
67 7
            $result->addError(
68 7
                $this->translateMessage(
69 7
                    $this->message,
70
                    [
71 7
                        'true' => $this->trueValue === true ? 'true' : $this->trueValue,
72 7
                        'false' => $this->falseValue === false ? 'false' : $this->falseValue,
73
                    ]
74
                )
75
            );
76
        }
77
78 17
        return $result;
79
    }
80
81 9
    public function getOptions(): array
82
    {
83 9
        return array_merge(
84 9
            parent::getOptions(),
85
            [
86 9
                'strict' => $this->strict,
87 9
                'trueValue' => $this->trueValue,
88 9
                'falseValue' => $this->falseValue,
89 9
                'message' => $this->translateMessage(
90 9
                    $this->message,
91
                    [
92 9
                        'true' => $this->trueValue === true ? 'true' : $this->trueValue,
93 9
                        'false' => $this->falseValue === false ? 'false' : $this->falseValue,
94
                    ]
95
                ),
96
            ],
97
        );
98
    }
99
}
100