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

Boolean   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 85
ccs 24
cts 36
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A trueValue() 0 5 1
A rule() 0 3 1
A falseValue() 0 5 1
B validateValue() 0 23 7
A getOptions() 0 13 3
A strict() 0 5 1
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