|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Attribute; |
|
9
|
|
|
use DateTimeImmutable; |
|
10
|
|
|
use DateTimeInterface; |
|
11
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
12
|
|
|
use Yiisoft\Validator\DumpedRuleInterface; |
|
13
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
|
14
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
|
15
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
|
16
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
|
17
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
|
18
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @see DateTimeHandler |
|
22
|
|
|
* @psalm-import-type WhenType from WhenInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
25
|
|
|
final class DateTime implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use SkipOnEmptyTrait; |
|
28
|
|
|
use SkipOnErrorTrait; |
|
29
|
|
|
use WhenTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private array $formats = []; |
|
35
|
|
|
private DateTimeInterface|false|null $min; |
|
36
|
|
|
private DateTimeInterface|false|null $max; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor for the class. |
|
40
|
|
|
* |
|
41
|
|
|
* @param int|DateTimeInterface|null $min The minimum value allowed. |
|
42
|
|
|
* @param int|DateTimeInterface|null $max The maximum value allowed. |
|
43
|
|
|
* @param string $message The validation error message for invalid DateTime value. |
|
44
|
|
|
* @param string $lessThanMinMessage The validation error message for values less than the minimum. |
|
45
|
|
|
* @param string $greaterThanMaxMessage The validation error message for values greater than the maximum. |
|
46
|
|
|
* @param mixed $skipOnEmpty Determines if the validation should be skipped when the value is empty. |
|
47
|
|
|
* @param bool $skipOnError Determines if the validation should be skipped when there is an error. |
|
48
|
|
|
* @param Closure|null $when The condition that determines when the validation should be applied. |
|
49
|
|
|
* @param string ...$formats The allowed date formats. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct( |
|
52
|
|
|
int|DateTimeInterface|null $min = null, |
|
53
|
|
|
int|DateTimeInterface|null $max = null, |
|
54
|
|
|
private string $message = '{attribute} value is not a valid DateTime.', |
|
55
|
|
|
private string $lessThanMinMessage = '{attribute} must be no less than {min}.', |
|
56
|
|
|
private string $greaterThanMaxMessage = '{attribute} must be no greater than {max}.', |
|
57
|
|
|
private mixed $skipOnEmpty = null, |
|
58
|
|
|
private bool $skipOnError = false, |
|
59
|
|
|
private ?Closure $when = null, |
|
60
|
|
|
string ...$formats |
|
61
|
|
|
) { |
|
62
|
|
|
$this->formats = $formats; |
|
63
|
|
|
$this->min = is_int($min) ? DateTimeImmutable::createFromFormat('U', (string) $min) : $min; |
|
64
|
|
|
$this->max = is_int($max) ? DateTimeImmutable::createFromFormat('U', (string) $max) : $max; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string[] |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getFormats(): array |
|
71
|
|
|
{ |
|
72
|
|
|
if ($this->formats) { |
|
|
|
|
|
|
73
|
|
|
return $this->formats; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return [ |
|
77
|
|
|
DateTimeInterface::ATOM, |
|
78
|
|
|
DateTimeInterface::COOKIE, |
|
79
|
|
|
DateTimeInterface::RFC822, |
|
80
|
|
|
DateTimeInterface::RFC850, |
|
81
|
|
|
DateTimeInterface::RFC1036, |
|
82
|
|
|
DateTimeInterface::RFC1123, |
|
83
|
|
|
DateTimeInterface::RFC7231, |
|
84
|
|
|
DateTimeInterface::RFC2822, |
|
85
|
|
|
DateTimeInterface::RFC3339, |
|
86
|
|
|
DateTimeInterface::RFC3339_EXTENDED, |
|
87
|
|
|
DateTimeInterface::RSS, |
|
88
|
|
|
DateTimeInterface::W3C, |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
public function getLessThanMinMessage(): string |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->lessThanMinMessage; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getGreaterThanMaxMessage(): string |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->greaterThanMaxMessage; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getMin(): DateTimeInterface|false|null |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->min; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getMax(): DateTimeInterface|false|null |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->max; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
public function getOptions(): array |
|
115
|
|
|
{ |
|
116
|
|
|
return [ |
|
117
|
|
|
'formats' => $this->formats, |
|
118
|
|
|
'min' => $this->min, |
|
119
|
|
|
'max' => $this->max, |
|
120
|
|
|
'lessThanMinMessage' => [ |
|
121
|
|
|
'template' => $this->lessThanMinMessage, |
|
122
|
|
|
'parameters' => [], |
|
123
|
|
|
], |
|
124
|
|
|
'greaterThanMaxMessage' => [ |
|
125
|
|
|
'template' => $this->greaterThanMaxMessage, |
|
126
|
|
|
'parameters' => [], |
|
127
|
|
|
], |
|
128
|
|
|
'message' => [ |
|
129
|
|
|
'template' => $this->message, |
|
130
|
|
|
'parameters' => [], |
|
131
|
|
|
], |
|
132
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
133
|
|
|
'skipOnError' => $this->skipOnError, |
|
134
|
|
|
]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getMessage(): string |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->message; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function getName(): string |
|
143
|
|
|
{ |
|
144
|
|
|
return self::class; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function getHandler(): string|RuleHandlerInterface |
|
148
|
|
|
{ |
|
149
|
|
|
return DateTimeHandler::class; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.