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