Passed
Pull Request — master (#333)
by Sergei
02:38
created

Boolean::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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