-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathDate.php
More file actions
46 lines (36 loc) · 1.19 KB
/
Date.php
File metadata and controls
46 lines (36 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);
namespace Sirius\Validation\Rule;
class Date extends AbstractRule
{
const OPTION_FORMAT = 'format';
const MESSAGE = 'This input must be a date having the format {format}';
const LABELED_MESSAGE = '{label} must be a date having the format {format}';
protected array $options = [
'format' => 'Y-m-d'
];
protected array $optionsIndexMap = [
0 => self::OPTION_FORMAT
];
public function validate(mixed $value, ?string $valueIdentifier = null): bool
{
$this->value = $value;
$this->success = $value == date(
(string) $this->options['format'],
$this->getTimestampFromFormatedString($value, $this->options['format'])
);
return $this->success;
}
protected function getTimestampFromFormatedString(mixed $string, mixed $format): ?int
{
$result = date_parse_from_format($format, $string);
return mktime(
(int)$result['hour'],
(int)$result['minute'],
(int)$result['second'],
(int)$result['month'],
(int)$result['day'],
(int)$result['year']
) ?: null;
}
}