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

Boolean   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 79
ccs 21
cts 33
cp 0.6364
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A trueValue() 0 5 1
A falseValue() 0 5 1
B validateValue() 0 21 7
A getOptions() 0 15 3
A strict() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Translator\TranslatorInterface;
8
use Yiisoft\Validator\DataSetInterface;
9
use Yiisoft\Validator\ErrorMessage;
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 7
            $result->addError(
70 7
                $this->message,
71
                [
72 7
                    'true' => $this->trueValue === true ? 'true' : $this->trueValue,
73 7
                    'false' => $this->falseValue === false ? 'false' : $this->falseValue,
74
                ]
75
            );
76
        }
77
78 18
        return $result;
79
    }
80
81 10
    public function getOptions(?TranslatorInterface $translator = null): array
82
    {
83 10
        return array_merge(
84 10
            parent::getOptions(),
85
            [
86 10
                'strict' => $this->strict,
87 10
                'trueValue' => $this->trueValue,
88 10
                'falseValue' => $this->falseValue,
89 10
                'message' => new ErrorMessage(
90 10
                    $this->message,
91
                    [
92 10
                        'true' => $this->trueValue === true ? 'true' : $this->trueValue,
93 10
                        'false' => $this->falseValue === false ? 'false' : $this->falseValue,
94
                    ],
95
                    $translator
96
                ),
97
            ],
98
        );
99
    }
100
}
101