Completed
Pull Request — master (#175)
by
unknown
02:02 queued 02:02
created

Boolean   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 62
ccs 12
cts 24
cp 0.5
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A getFormattedMessage() 0 5 3
A validateValue() 0 18 5
A getOptions() 0 7 1
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
    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 2
        bool $skipOnEmpty = false,
37
        bool $skipOnError = false,
38 2
        $when = null
39
    ) {
40
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
41
    }
42
43
    protected function validateValue($value, ?ValidationContext $context = null): Result
44
    {
45
        if ($this->strict) {
46
            $valid = $value === $this->trueValue || $value === $this->falseValue;
47
        } else {
48
            $valid = $value == $this->trueValue || $value == $this->falseValue;
49
        }
50
51
        $result = new Result();
52
53
        if ($valid) {
54
            return $result;
55
        }
56
57
        $message = $this->getFormattedMessage();
58
        $result->addError($message);
59
60
        return $result;
61
    }
62 20
63
    private function getFormattedMessage(): string
64 20
    {
65 8
        return $this->formatMessage($this->message, [
66
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
67 12
            'false' => $this->falseValue === false ? 'false' : $this->falseValue,
68
        ]);
69
    }
70 20
71
    public function getOptions(): array
72 20
    {
73 7
        return array_merge(parent::getOptions(), [
74 7
            'trueValue' => $this->trueValue,
75 7
            'falseValue' => $this->falseValue,
76
            'strict' => $this->strict,
77 7
            'message' => $this->getFormattedMessage(),
78 7
        ]);
79
    }
80
}
81