Passed
Pull Request — master (#333)
by Sergei
02:38
created

LessThan   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 42 3
A getName() 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 RuntimeException;
10
use Yiisoft\Validator\ValidationContext;
11
12
/**
13
 * Validates if the specified value is less than another value or attribute.
14
 *
15
 * The value being validated with {@see LessThan::$targetValue} or {@see LessThan::$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 LessThan::$type} to
20
 * {@see LessThan::TYPE_NUMBER} to enable numeric validation.
21
 */
22
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
23
final class LessThan extends Compare
24
{
25 3
    public function __construct(
26
        /**
27
         * @var mixed The constant value to be less than. When both this property
28
         * and {@see $targetAttribute} are set, this property takes precedence.
29
         */
30
        private $targetValue = null,
31
        /**
32
         * @var string|null The attribute to be less than. 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
        /**
46
         * @var bool|callable
47
         */
48
        $skipOnEmpty = false,
49
        private bool $skipOnError = false,
50
        /**
51
         * @var Closure(mixed, ValidationContext):bool|null
52
         */
53
        private ?Closure $when = null,
54
    ) {
55 3
        if ($this->targetValue === null && $this->targetAttribute === null) {
56 1
            throw new RuntimeException('Either "targetValue" or "targetAttribute" must be specified.');
57
        }
58 2
        parent::__construct(
59 2
            targetValue: $this->targetValue,
60 2
            targetAttribute: $this->targetAttribute,
61 2
            message: $this->message,
62 2
            type: $this->type,
63
            operator: '<',
64
            skipOnEmpty: $skipOnEmpty,
65 2
            skipOnError: $this->skipOnError,
66 2
            when: $this->when
67
        );
68
    }
69
70 1
    public function getName(): string
71
    {
72 1
        return 'lessThan';
73
    }
74
}
75