Test Failed
Pull Request — master (#175)
by
unknown
12:57
created

Boolean::getFormattedMessage()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

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