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

LessThan::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 38
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 38
ccs 10
cts 11
cp 0.9091
rs 9.9
cc 3
nc 2
nop 7
crap 3.0067
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 less than another value or attribute.
20
 *
21
 * The value being validated with {@see LessThan::$targetValue} or {@see LessThan::$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 LessThan::$type} to
26
 * {@see LessThan::TYPE_NUMBER} to enable numeric validation.
27
 */
28
#[Attribute(Attribute::TARGET_PROPERTY)]
29
final class LessThan extends Compare
30
{
31 1
    public function __construct(
32
        /**
33
         * @var mixed the constant value to be less than. When both this property
34
         * and {@see $targetAttribute} are set, this property takes precedence.
35
         */
36
        private $targetValue = null,
37
        /**
38
         * @var string|null the attribute to be less than. 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 validated.
48
         */
49
        private string $type = self::TYPE_STRING,
50
        private bool $skipOnEmpty = false,
51
        private bool $skipOnError = false,
52
        /**
53
         * @var Closure(mixed, ValidationContext):bool|null
54
         */
55
        private ?Closure $when = null,
56
    ) {
57 1
        if ($this->targetValue === null && $this->targetAttribute === null) {
58
            throw new RuntimeException('Either "targetValue" or "targetAttribute" must be specified.');
59
        }
60 1
        parent::__construct(
61 1
            targetValue: $this->targetValue,
62 1
            targetAttribute: $this->targetAttribute,
63 1
            message: $this->message,
64 1
            type: $this->type,
65
            operator: '<',
66 1
            skipOnEmpty: $this->skipOnEmpty,
67 1
            skipOnError: $this->skipOnError,
68 1
            when: $this->when
69
        );
70
    }
71
72
    public function getHandlerClassName(): string
73
    {
74
        return CompareHandler::class;
75
    }
76
}
77