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 or equal to another value or attribute. |
20
|
|
|
* |
21
|
|
|
* The value being validated with {@see LessThanOrEqual::$targetValue} or {@see LessThanOrEqual::$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 LessThanOrEqual::$type} to |
26
|
|
|
* {@see LessThanOrEqual::TYPE_NUMBER} to enable numeric validation. |
27
|
|
|
*/ |
28
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
29
|
|
|
final class LessThanOrEqual extends Compare |
30
|
|
|
{ |
31
|
1 |
|
public function __construct( |
32
|
|
|
/** |
33
|
|
|
* @var mixed the constant value to be less than or equal to. 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 or 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 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
|
|
|
|