Test Failed
Pull Request — master (#222)
by Rustam
02:30
created

Boolean   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 62
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 10 at column 27
Loading history...
11
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
12
use Yiisoft\Validator\ParametrizedRuleInterface;
13
14
/**
15
 * Checks if the value is a boolean value or a value corresponding to it.
16
 */
17
#[Attribute(Attribute::TARGET_PROPERTY)]
18
final class Boolean implements ParametrizedRuleInterface
19 2
{
20
    use HandlerClassNameTrait;
21
    use RuleNameTrait;
22
23
    public function __construct(
24
        /**
25
         * @var mixed the value representing true status. Defaults to '1'.
26
         */
27
        private $trueValue = '1',
28
        /**
29
         * @var mixed the value representing false status. Defaults to '0'.
30
         */
31
        private $falseValue = '0',
32
        /**
33
         * @var bool whether the comparison to {@see $trueValue} and {@see $falseValue} is strict.
34
         * When this is `true`, the value and type must both match those of {@see $trueValue} or
35
         * {@see $falseValue}. Defaults to `false`, meaning only the value needs to be matched.
36
         */
37
        private bool $strict = false,
38
        private string $message = 'The value must be either "{true}" or "{false}".',
39
        private bool $skipOnEmpty = false,
40 2
        private bool $skipOnError = false,
41
        private ?Closure $when = null,
42
    ) {
43 20
    }
44
45 20
    /**
46 8
     * @return mixed
47
     */
48 12
    public function getTrueValue(): mixed
49
    {
50
        return $this->trueValue;
51 20
    }
52
53 20
    /**
54 13
     * @return mixed
55
     */
56
    public function getFalseValue(): mixed
57 7
    {
58 7
        return $this->falseValue;
59
    }
60 7
61
    /**
62
     * @return bool
63 14
     */
64
    public function isStrict(): bool
65 14
    {
66 14
        return $this->strict;
67 14
    }
68
69
    /**
70
     * @return string
71 7
     */
72
    public function getMessage(): string
73 7
    {
74 7
        return $this->message;
75 7
    }
76 7
77 7
    /**
78
     * @return bool
79
     */
80
    public function isSkipOnEmpty(): bool
81
    {
82
        return $this->skipOnEmpty;
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    public function isSkipOnError(): bool
89
    {
90
        return $this->skipOnError;
91
    }
92
93
    /**
94
     * @return Closure|null
95
     */
96
    public function getWhen(): ?Closure
97
    {
98
        return $this->when;
99
    }
100
101
    #[ArrayShape([
102
        'trueValue' => 'string',
103
        'falseValue' => 'string',
104
        'strict' => 'bool',
105
        'message' => 'array',
106
        'skipOnEmpty' => 'bool',
107
        'skipOnError' => 'bool',
108
    ])]
109
    public function getOptions(): array
110
    {
111
        return [
112
            'trueValue' => $this->trueValue,
113
            'falseValue' => $this->falseValue,
114
            'strict' => $this->strict,
115
            'message' => [
116
                'message' => $this->message,
117
                'parameters' => [
118
                    // TODO: get reasons to do like this
119
                    //  'true' => $this->trueValue === true ? 'true' : $this->trueValue,
120
                    //  'false' => $this->falseValue === false ? 'false' : $this->falseValue,
121
                    'true' => $this->trueValue,
122
                    'false' => $this->falseValue,
123
                ],
124
            ],
125
            'skipOnEmpty' => $this->skipOnEmpty,
126
            'skipOnError' => $this->skipOnError,
127
        ];
128
    }
129
}
130