Passed
Pull Request — master (#99)
by Def
02:08
created

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