Passed
Pull Request — master (#245)
by Rustam
12:07
created

Boolean   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 27
c 0
b 0
f 0
dl 0
loc 87
ccs 18
cts 18
cp 1
rs 10

6 Methods

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