|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure; |
|
4
|
|
|
|
|
5
|
|
|
use Assert\InvalidArgumentException; |
|
6
|
|
|
use Imedia\Ammit\Domain\DateValidation; |
|
7
|
|
|
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Guillaume MOREL <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
trait DateValidatorTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Exceptions are caught in order to be processed later |
|
16
|
|
|
* @param mixed $value Date Y-m-d ? |
|
17
|
|
|
* |
|
18
|
|
|
* @return \DateTime |
|
19
|
|
|
*/ |
|
20
|
|
|
public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
21
|
|
|
{ |
|
22
|
1 |
|
$dateValidation = new DateValidation(); |
|
23
|
1 |
|
$this->validationEngine->validateFieldValue( |
|
|
|
|
|
|
24
|
1 |
|
$parentValidator ?: $this, |
|
25
|
1 |
View Code Duplication |
function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) { |
|
|
|
|
|
|
26
|
1 |
|
if (null === $value || ! $dateValidation->isDateValid($value)) { |
|
27
|
1 |
|
throw new InvalidArgumentException( |
|
28
|
1 |
|
$exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value), |
|
29
|
1 |
|
0, |
|
30
|
|
|
$propertyPath, |
|
31
|
|
|
$value |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
1 |
|
} |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
1 |
|
if (null === $value) { |
|
38
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
$date = $dateValidation->createDateFromString($value); |
|
42
|
1 |
|
if ($date instanceof \DateTime) { |
|
43
|
1 |
|
return $date->setTime(0, 0, 0); // Valid |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Exceptions are caught in order to be processed later |
|
51
|
|
|
* @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ? |
|
52
|
|
|
* |
|
53
|
|
|
* @return \DateTime|false |
|
54
|
|
|
*/ |
|
55
|
|
|
public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
56
|
|
|
{ |
|
57
|
1 |
|
$dateValidation = new DateValidation(); |
|
58
|
|
|
|
|
59
|
1 |
|
$this->validationEngine->validateFieldValue( |
|
60
|
1 |
|
$parentValidator ?: $this, |
|
61
|
1 |
View Code Duplication |
function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) { |
|
|
|
|
|
|
62
|
1 |
|
if (null === $value || ! $dateValidation->isDateTimeValid($value)) { |
|
63
|
1 |
|
throw new InvalidArgumentException( |
|
64
|
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), |
|
65
|
1 |
|
0, |
|
66
|
|
|
$propertyPath, |
|
67
|
|
|
$value |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
1 |
|
} |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
1 |
|
if (null === $value) { |
|
74
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$date = $dateValidation->createDateTimeFromString($value); |
|
78
|
|
|
|
|
79
|
1 |
|
if ($date instanceof \DateTime) { |
|
80
|
1 |
|
return $date; // Valid |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function createDefaultDateTime(): \DateTime |
|
87
|
|
|
{ |
|
88
|
1 |
|
$date = new \DateTime(); |
|
89
|
|
|
|
|
90
|
1 |
|
return $date->setTime(0, 0, 0); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: