Passed
Pull Request — master (#222)
by Alexander
04:26 queued 02:14
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\PreValidatableRuleInterface;
11
use Yiisoft\Validator\Rule\Trait\PreValidatableTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 11 at column 27
Loading history...
12
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
14
use Yiisoft\Validator\ParametrizedRuleInterface;
15
16
/**
17
 * Checks if the value is a boolean value or a value corresponding to it.
18
 */
19
#[Attribute(Attribute::TARGET_PROPERTY)]
20
final class Boolean implements ParametrizedRuleInterface, PreValidatableRuleInterface
21
{
22
    use HandlerClassNameTrait;
23
    use PreValidatableTrait;
24
    use RuleNameTrait;
25
26 1
    public function __construct(
27
        /**
28
         * @var mixed the value representing true status. Defaults to '1'.
29
         */
30
        private $trueValue = '1',
31
        /**
32
         * @var mixed the value representing false status. Defaults to '0'.
33
         */
34
        private $falseValue = '0',
35
        /**
36
         * @var bool whether the comparison to {@see $trueValue} and {@see $falseValue} is strict.
37
         * When this is `true`, the value and type must both match those of {@see $trueValue} or
38
         * {@see $falseValue}. Defaults to `false`, meaning only the value needs to be matched.
39
         */
40
        private bool $strict = false,
41
        private string $message = 'The value must be either "{true}" or "{false}".',
42
        private bool $skipOnEmpty = false,
43
        private bool $skipOnError = false,
44
        private ?Closure $when = null,
45
    ) {
46
    }
47
48
    /**
49
     * @return mixed
50
     */
51 21
    public function getTrueValue(): mixed
52
    {
53 21
        return $this->trueValue;
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59 12
    public function getFalseValue(): mixed
60
    {
61 12
        return $this->falseValue;
62
    }
63
64
    /**
65
     * @return bool
66
     */
67 21
    public function isStrict(): bool
68
    {
69 21
        return $this->strict;
70
    }
71
72
    /**
73
     * @return string
74
     */
75 8
    public function getMessage(): string
76
    {
77 8
        return $this->message;
78
    }
79
80 7
    #[ArrayShape([
81
        'trueValue' => 'string',
82
        'falseValue' => 'string',
83
        'strict' => 'bool',
84
        'message' => 'array',
85
        'skipOnEmpty' => 'bool',
86
        'skipOnError' => 'bool',
87
    ])]
88
    public function getOptions(): array
89
    {
90
        return [
91 7
            'trueValue' => $this->trueValue,
92 7
            'falseValue' => $this->falseValue,
93 7
            'strict' => $this->strict,
94
            'message' => [
95 7
                'message' => $this->message,
96
                'parameters' => [
97
                    // TODO: get reasons to do like this
98
                    //  'true' => $this->trueValue === true ? 'true' : $this->trueValue,
99
                    //  'false' => $this->falseValue === false ? 'false' : $this->falseValue,
100 7
                    'true' => $this->trueValue,
101 7
                    'false' => $this->falseValue,
102
                ],
103
            ],
104 7
            'skipOnEmpty' => $this->skipOnEmpty,
105 7
            'skipOnError' => $this->skipOnError,
106
        ];
107
    }
108
}
109