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 or equal to another value or attribute. |
14
|
|
|
* |
15
|
|
|
* The value being validated with {@see LessThanOrEqual::$targetValue} or {@see LessThanOrEqual::$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 LessThanOrEqual::$type} to |
20
|
|
|
* {@see LessThanOrEqual::TYPE_NUMBER} to enable numeric validation. |
21
|
|
|
*/ |
22
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
23
|
|
|
final class LessThanOrEqual extends Compare |
24
|
|
|
{ |
25
|
3 |
|
public function __construct( |
26
|
|
|
/** |
27
|
|
|
* @var mixed The constant value to be less than or equal to. 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 or 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
|
|
|
private bool $skipOnEmpty = false, |
45
|
|
|
private bool $skipOnError = false, |
46
|
|
|
/** |
47
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
48
|
|
|
*/ |
49
|
|
|
private ?Closure $when = null, |
50
|
|
|
) { |
51
|
3 |
|
if ($this->targetValue === null && $this->targetAttribute === null) { |
52
|
1 |
|
throw new RuntimeException('Either "targetValue" or "targetAttribute" must be specified.'); |
53
|
|
|
} |
54
|
2 |
|
parent::__construct( |
55
|
2 |
|
targetValue: $this->targetValue, |
56
|
2 |
|
targetAttribute: $this->targetAttribute, |
57
|
2 |
|
message: $this->message, |
58
|
2 |
|
type: $this->type, |
59
|
|
|
operator: '<=', |
60
|
2 |
|
skipOnEmpty: $this->skipOnEmpty, |
61
|
2 |
|
skipOnError: $this->skipOnError, |
62
|
2 |
|
when: $this->when |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|