Passed
Push — master ( 89f940...819311 )
by Alexander
02:27
created

Boolean::getHandlerClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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