Passed
Pull Request — master (#199)
by Alexander
02:44
created

Boolean::validateValue()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 8
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Yiisoft\Validator\FormatterInterface;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\Rule;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * Checks if the value is a boolean value or a value corresponding to it.
15
 */
16
#[Attribute(Attribute::TARGET_PROPERTY)]
17
final class Boolean extends Rule
18
{
19 2
    public function __construct(
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 {@see $trueValue} and {@see $falseValue} is strict.
30
         * When this is `true`, the value and type must both match those of {@see $trueValue} or
31
         * {@see $falseValue}. Defaults to `false`, meaning only the value needs to be matched.
32
         */
33
        private bool $strict = false,
34
        private string $message = 'The value must be either "{true}" or "{false}".',
35
        ?FormatterInterface $formatter = null,
36
        bool $skipOnEmpty = false,
37
        bool $skipOnError = false,
38
        $when = null
39
    ) {
40 2
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
41
    }
42
43 20
    protected function validateValue($value, ?ValidationContext $context = null): Result
44
    {
45 20
        if ($this->strict) {
46 8
            $valid = $value === $this->trueValue || $value === $this->falseValue;
47
        } else {
48 12
            $valid = $value == $this->trueValue || $value == $this->falseValue;
49
        }
50
51 20
        $result = new Result();
52
53 20
        if ($valid) {
54 13
            return $result;
55
        }
56
57 7
        $message = $this->getFormattedMessage();
58 7
        $result->addError($message);
59
60 7
        return $result;
61
    }
62
63 14
    private function getFormattedMessage(): string
64
    {
65 14
        return $this->formatMessage($this->message, [
66 14
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
67 14
            'false' => $this->falseValue === false ? 'false' : $this->falseValue,
68
        ]);
69
    }
70
71 7
    public function getOptions(): array
72
    {
73 7
        return array_merge(parent::getOptions(), [
74 7
            'trueValue' => $this->trueValue,
75 7
            'falseValue' => $this->falseValue,
76 7
            'strict' => $this->strict,
77 7
            'message' => $this->getFormattedMessage(),
78
        ]);
79
    }
80
}
81