Passed
Push — master ( 9562b0...4734f7 )
by Sergei
04:27 queued 02:02
created

IsTrue::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
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 Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\RuleWithOptionsInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\WhenInterface;
16
17
/**
18
 * Checks if the value is a "true" boolean value or a value corresponding to it. Useful for user agreements etc.
19
 *
20
 * @see IsTrueHandler
21
 *
22
 * @psalm-import-type WhenType from WhenInterface
23
 */
24
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
25
final class IsTrue implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
26
{
27
    use SkipOnEmptyTrait;
28
    use SkipOnErrorTrait;
29
    use WhenTrait;
30 4
31
    public function __construct(
32
        /**
33
         * @var scalar the value representing "true" status. Defaults to `1`.
34
         */
35
        private int|float|string|bool $trueValue = '1',
36
        /**
37
         * @var bool whether the comparison to {@see $trueValue} is strict. When this is "true", the value and type must
38
         * both match {@see $trueValue}. 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 "{true}".',
42
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed in a multi-line function declaration
Loading history...
43
        /**
44
         * @var bool|callable|null
45
         */
46
        private $skipOnEmpty = null,
47
        private bool $skipOnError = false,
48
        /**
49
         * @var WhenType
50
         */
51
        private Closure|null $when = null,
52
    ) {
53
    }
54 1
55
    public function getName(): string
56 1
    {
57
        return 'isTrue';
58
    }
59 16
60
    public function getTrueValue(): int|float|string|bool
61 16
    {
62
        return $this->trueValue;
63
    }
64 16
65
    public function isStrict(): bool
66 16
    {
67
        return $this->strict;
68
    }
69 12
70
    public function getMessage(): string
71 12
    {
72
        return $this->message;
73
    }
74 3
75
    public function getOptions(): array
76
    {
77 3
        return [
78 3
            'trueValue' => $this->trueValue,
79
            'strict' => $this->strict,
80 3
            'message' => [
81
                'template' => $this->message,
82 3
                'parameters' => [
83
                    'true' => $this->trueValue === true ? 'true' : $this->trueValue,
84
                ],
85 3
            ],
86 3
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
87
            'skipOnError' => $this->skipOnError,
88
        ];
89
    }
90 16
91
    public function getHandler(): string
92 16
    {
93
        return IsTrueHandler::class;
94
    }
95
}
96