Passed
Pull Request — master (#81)
by Def
04:20
created

Boolean::getOptions()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 1
nop 0
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 6
rs 9.2222
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
    /**
82
     * @inheritDoc
83
     * @return string
84
     */
85 2
    public function getName(): string
86
    {
87 2
        return 'boolean';
88
    }
89
90
    /**
91
     * @inheritDoc
92
     * @return array
93
     */
94 8
    public function getOptions(): array
95
    {
96 8
        return array_merge(
97 8
            $this->strict ? ['strict' => true] : [],
98 8
            $this->trueValue !== '1' ? ['trueValue' => $this->trueValue] : [],
99 8
            $this->falseValue !== '0' ? ['falseValue' => $this->falseValue] : [],
100 8
            ['message' => $this->translateMessage(
101 8
                $this->message,
102
                [
103 8
                    'true' => $this->trueValue === true ? 'true' : $this->trueValue,
104 8
                    'false' => $this->falseValue === false ? 'false' : $this->falseValue
105
                ]
106
            )],
107 8
            parent::getOptions()
108
        );
109
    }
110
}
111