Passed
Pull Request — master (#287)
by
unknown
02:24
created

IsTrue   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 1
b 0
f 0
dl 0
loc 69
ccs 16
cts 16
cp 1
rs 10

6 Methods

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