Passed
Push — master ( fe43ea...fddb9c )
by
unknown
12:44
created

IsTrue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 22
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 6
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 "true" boolean value or a value corresponding to it. Useful for user agreements etc.
20
 *
21
 * @see IsTrueHandler
22
 */
23
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
24
final class IsTrue implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
25
{
26
    use SkipOnEmptyTrait;
27
    use SkipOnErrorTrait;
28
    use WhenTrait;
29
30 2
    public function __construct(
31
        /**
32
         * @var mixed the value representing "true" status. Defaults to `1`.
33
         */
34
        private mixed $trueValue = '1',
35
        /**
36
         * @var bool whether the comparison to {@see $trueValue} is strict. When this is "true", the value and type must
37
         * both match {@see $trueValue}. 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 "{true}".',
41
42
        /**
43
         * @var bool|callable|null
44
         */
45
        private $skipOnEmpty = null,
46
        private bool $skipOnError = false,
47
        /**
48
         * @var Closure(mixed, ValidationContext):bool|null
49
         */
50
        private ?Closure $when = null,
51
    ) {
52
    }
53
54 1
    public function getName(): string
55
    {
56 1
        return 'isTrue';
57
    }
58
59 15
    public function getTrueValue(): mixed
60
    {
61 15
        return $this->trueValue;
62
    }
63
64 15
    public function isStrict(): bool
65
    {
66 15
        return $this->strict;
67
    }
68
69 11
    public function getMessage(): string
70
    {
71 11
        return $this->message;
72
    }
73
74 3
    public function getOptions(): array
75
    {
76
        return [
77 3
            'trueValue' => $this->trueValue,
78 3
            'strict' => $this->strict,
79
            'message' => [
80 3
                'message' => $this->message,
81
                'parameters' => [
82 3
                    'true' => $this->trueValue === true ? 'true' : $this->trueValue,
83
                ],
84
            ],
85 3
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
86 3
            'skipOnError' => $this->skipOnError,
87
        ];
88
    }
89
90 1
    public function getHandlerClassName(): string
91
    {
92 1
        return IsTrueHandler::class;
93
    }
94
}
95