Passed
Push — master ( 99ab46...a20a6f )
by Alexander
02:59
created

Boolean::rule()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\HasValidationErrorMessage;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule;
10
use Yiisoft\Validator\ValidationContext;
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
    /**
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 {@see trueValue()} and {@see falseValue()} is strict.
29
     * When this is true, the attribute value and type must both match those of {@see trueValue()} or {@see 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 2
    public static function rule(): self
37
    {
38 2
        return new self();
39
    }
40
41
    public function trueValue($value): self
42
    {
43
        $new = clone $this;
44
        $new->trueValue = $value;
45
        return $new;
46
    }
47
48
    public function falseValue($value): self
49
    {
50
        $new = clone $this;
51
        $new->falseValue = $value;
52
        return $new;
53
    }
54
55
    public function strict(bool $value): self
56
    {
57
        $new = clone $this;
58
        $new->strict = $value;
59
        return $new;
60
    }
61
62 20
    protected function validateValue($value, ValidationContext $context = null): Result
63
    {
64 20
        if ($this->strict) {
65 8
            $valid = $value === $this->trueValue || $value === $this->falseValue;
66
        } else {
67 12
            $valid = $value == $this->trueValue || $value == $this->falseValue;
68
        }
69
70 20
        $result = new Result();
71
72 20
        if (!$valid) {
73 7
            $result->addError(
74 7
                $this->formatMessage(
75 7
                    $this->message,
76
                    [
77 7
                        'true' => $this->trueValue === true ? 'true' : $this->trueValue,
78 7
                        'false' => $this->falseValue === false ? 'false' : $this->falseValue,
79
                    ]
80
                )
81
            );
82
        }
83
84 20
        return $result;
85
    }
86
87 7
    public function getOptions(): array
88
    {
89 7
        return array_merge(
90 7
            parent::getOptions(),
91
            [
92 7
                'strict' => $this->strict,
93 7
                'trueValue' => $this->trueValue,
94 7
                'falseValue' => $this->falseValue,
95 7
                'message' => $this->formatMessage(
96 7
                    $this->message,
97
                    [
98 7
                        'true' => $this->trueValue === true ? 'true' : $this->trueValue,
99 7
                        'false' => $this->falseValue === false ? 'false' : $this->falseValue,
100
                    ]
101
                ),
102
            ],
103
        );
104
    }
105
}
106