Passed
Pull Request — master (#81)
by Def
01:23
created

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