|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pure; |
|
4
|
|
|
|
|
5
|
|
|
use AmmitPhp\Ammit\UI\Resolver\UIValidationEngine; |
|
6
|
|
|
use AmmitPhp\Ammit\UI\Resolver\Validator\InvalidArgumentException; |
|
7
|
|
|
use AmmitPhp\Ammit\Domain\DateValidation; |
|
8
|
|
|
use AmmitPhp\Ammit\UI\Resolver\Validator\UIValidatorInterface; |
|
9
|
|
|
|
|
10
|
|
|
trait DateValidatorTrait |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var UIValidationEngine */ |
|
13
|
|
|
protected $validationEngine; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Exceptions are caught in order to be processed later |
|
17
|
|
|
* @param mixed $value Date Y-m-d ? |
|
18
|
|
|
* |
|
19
|
|
|
* @return \DateTime |
|
20
|
|
|
*/ |
|
21
|
|
|
public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
22
|
|
|
{ |
|
23
|
1 |
|
$dateValidation = new DateValidation(); |
|
24
|
1 |
|
$this->validationEngine->validateFieldValue( |
|
25
|
1 |
|
$parentValidator ?: $this, |
|
26
|
1 |
|
function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) { |
|
27
|
1 |
|
if (null === $value || ! $dateValidation->isDateValid($value)) { |
|
28
|
1 |
|
throw new InvalidArgumentException( |
|
29
|
1 |
|
$exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value), |
|
30
|
1 |
|
0, |
|
31
|
|
|
$propertyPath, |
|
32
|
|
|
$value |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
1 |
|
} |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
1 |
|
if (null === $value) { |
|
39
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
$date = $dateValidation->createDateFromString($value); |
|
43
|
1 |
|
if ($date instanceof \DateTime) { |
|
44
|
1 |
|
return $date->setTime(0, 0, 0); // Valid |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Exceptions are caught in order to be processed later |
|
52
|
|
|
* @param mixed $value null|DateTime Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 or null ? |
|
53
|
|
|
* |
|
54
|
|
|
* @return \DateTime|null |
|
|
|
|
|
|
55
|
|
|
*/ |
|
56
|
|
View Code Duplication |
public function mustBeDateTimeOrEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
1 |
|
if (empty($value)) { |
|
59
|
1 |
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return $this->mustBeDateTime($value, $propertyPath, $parentValidator, $exceptionMessage); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Exceptions are caught in order to be processed later |
|
67
|
|
|
* @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ? |
|
68
|
|
|
* |
|
69
|
|
|
* @return \DateTime|false |
|
70
|
|
|
*/ |
|
71
|
|
|
public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$dateValidation = new DateValidation(); |
|
74
|
|
|
|
|
75
|
1 |
|
$this->validationEngine->validateFieldValue( |
|
76
|
1 |
|
$parentValidator ?: $this, |
|
77
|
1 |
|
function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) { |
|
78
|
1 |
|
if (null === $value || ! $dateValidation->isDateTimeValid($value)) { |
|
79
|
1 |
|
throw new InvalidArgumentException( |
|
80
|
1 |
|
$exceptionMessage ?: sprintf('Datetime "%s" format invalid, must be Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00.', $value), |
|
81
|
1 |
|
0, |
|
82
|
|
|
$propertyPath, |
|
83
|
|
|
$value |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
1 |
|
} |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
1 |
|
if (null === $value) { |
|
90
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
$date = $dateValidation->createDateTimeFromString($value); |
|
94
|
|
|
|
|
95
|
1 |
|
if ($date instanceof \DateTime) { |
|
96
|
1 |
|
return $date; // Valid |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function createDefaultDateTime(): \DateTime |
|
103
|
|
|
{ |
|
104
|
1 |
|
$date = new \DateTime(); |
|
105
|
|
|
|
|
106
|
1 |
|
return $date->setTime(0, 0, 0); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.