Passed
Pull Request — master (#248)
by Rustam
02:32
created

NotEqual   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 51
ccs 12
cts 14
cp 0.8571
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandlerClassName() 0 3 1
A __construct() 0 42 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use RuntimeException;
10
use Yiisoft\Validator\ValidationContext;
11
12
/**
13
 * Validates if the specified value is not equal to another value or attribute.
14
 *
15
 * The value being validated with {@see NotEqual::$targetValue} or {@see NotEqual::$targetAttribute}, which
16
 * is set in the constructor.
17
 *
18
 * The default validation function is based on string values, which means the values
19
 * are checked byte by byte. When validating numbers, make sure to change {@see NotEqual::$type} to
20
 * {@see NotEqual::TYPE_NUMBER} to enable numeric validation.
21
 */
22
#[Attribute(Attribute::TARGET_PROPERTY)]
23
final class NotEqual extends Compare
24
{
25 2
    public function __construct(
26
        /**
27
         * @var mixed the constant value to not be equal to. When both this property
28
         * and {@see $targetAttribute} are set, this property takes precedence.
29
         */
30
        private $targetValue = null,
31
        /**
32
         * @var mixed the constant value to not be equal to. When both this property
33
         * and {@see $targetValue} are set, the {@see $targetValue} takes precedence.
34
         */
35
        private ?string $targetAttribute = null,
36
        /**
37
         * @var string|null user-defined error message
38
         */
39
        private ?string $message = null,
40
        /**
41
         * @var string the type of the values being validated.
42
         */
43
        private string $type = self::TYPE_STRING,
44
        /**
45
         * @var bool Whether this validator strictly check.
46
         */
47
        private bool $strict = false,
48
        private bool $skipOnEmpty = false,
49
        private bool $skipOnError = false,
50
        /**
51
         * @var Closure(mixed, ValidationContext):bool|null
52
         */
53
        private ?Closure $when = null,
54
    ) {
55 2
        if ($this->targetValue === null && $this->targetAttribute === null) {
56 1
            throw new RuntimeException('Either "targetValue" or "targetAttribute" must be specified.');
57
        }
58 1
        parent::__construct(
59 1
            targetValue: $this->targetValue,
60 1
            targetAttribute: $this->targetAttribute,
61 1
            message: $this->message,
62 1
            type: $this->type,
63 1
            operator: $this->strict ? '!==' : '!=',
64 1
            skipOnEmpty: $this->skipOnEmpty,
65 1
            skipOnError: $this->skipOnError,
66 1
            when: $this->when
67
        );
68
    }
69
70
    public function getHandlerClassName(): string
71
    {
72
        return CompareHandler::class;
73
    }
74
}
75