Passed
Pull Request — master (#248)
by Rustam
12:42
created

Equal::getHandlerClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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 RuntimeException;
11
use Yiisoft\Validator\ParametrizedRuleInterface;
12
use Yiisoft\Validator\BeforeValidationInterface;
13
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
14
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
15
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
16
use Yiisoft\Validator\ValidationContext;
17
18
/**
19
 * Validates if the specified value is equal to another value or attribute.
20
 *
21
 * The value being validated with {@see Equal::$targetValue} or {@see Equal::$targetAttribute}, which
22
 * is set in the constructor.
23
 *
24
 * The default validation function is based on string values, which means the values
25
 * are checked byte by byte. When validating numbers, make sure to change {@see Equal::$type} to
26
 * {@see Equal::TYPE_NUMBER} to enable numeric validation.
27
 */
28
#[Attribute(Attribute::TARGET_PROPERTY)]
29
final class Equal extends Compare
30
{
31 1
    public function __construct(
32
        /**
33
         * @var mixed the constant value to be equal to. When both this property
34
         * and {@see $targetAttribute} are set, this property takes precedence.
35
         */
36
        private $targetValue = null,
37
        /**
38
         * @var mixed the attribute to be equal to. When both this property
39
         * and {@see $targetValue} are set, the {@see $targetValue} takes precedence.
40
         */
41
        private ?string $targetAttribute = null,
42
        /**
43
         * @var string|null user-defined error message
44
         */
45
        private ?string $message = null,
46
        /**
47
         * @var string the type of the values being compared.
48
         */
49
        private string $type = self::TYPE_STRING,
50
        /**
51
         * @var bool Whether this validator strictly check.
52
         */
53
        private bool $strict = false,
54
        private bool $skipOnEmpty = false,
55
        private bool $skipOnError = false,
56
        /**
57
         * @var Closure(mixed, ValidationContext):bool|null
58
         */
59
        private ?Closure $when = null,
60
    ) {
61 1
        if ($this->targetValue === null && $this->targetAttribute === null) {
62
            throw new RuntimeException('Either "targetValue" or "targetAttribute" must be specified.');
63
        }
64 1
        parent::__construct(
65 1
            targetValue: $this->targetValue,
66 1
            targetAttribute: $this->targetAttribute,
67 1
            message: $this->message,
68 1
            type: $this->type,
69 1
            operator: $this->strict ? '===' : '==',
70 1
            skipOnEmpty: $this->skipOnEmpty,
71 1
            skipOnError: $this->skipOnError,
72 1
            when: $this->when
73
        );
74
    }
75
76
    public function getHandlerClassName(): string
77
    {
78
        return CompareHandler::class;
79
    }
80
}
81