|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure; |
|
4
|
|
|
|
|
5
|
|
|
use Imedia\Ammit\UI\Resolver\UIValidationEngine; |
|
6
|
|
|
use Imedia\Ammit\UI\Resolver\Validator\InvalidArgumentException; |
|
7
|
|
|
use Imedia\Ammit\Domain\DateValidation; |
|
8
|
|
|
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Guillaume MOREL <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
trait DateValidatorTrait |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var UIValidationEngine */ |
|
16
|
|
|
protected $validationEngine; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Exceptions are caught in order to be processed later |
|
20
|
|
|
* @param mixed $value Date Y-m-d ? |
|
21
|
|
|
* |
|
22
|
|
|
* @return \DateTime |
|
23
|
|
|
*/ |
|
24
|
|
|
public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
25
|
|
|
{ |
|
26
|
1 |
|
$dateValidation = new DateValidation(); |
|
27
|
1 |
|
$this->validationEngine->validateFieldValue( |
|
28
|
1 |
|
$parentValidator ?: $this, |
|
29
|
1 |
|
function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) { |
|
30
|
1 |
View Code Duplication |
if (null === $value || ! $dateValidation->isDateValid($value)) { |
|
|
|
|
|
|
31
|
1 |
|
throw new InvalidArgumentException( |
|
32
|
1 |
|
$exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value), |
|
33
|
1 |
|
0, |
|
34
|
|
|
$propertyPath, |
|
35
|
|
|
$value |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
1 |
|
} |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
1 |
|
if (null === $value) { |
|
42
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
$date = $dateValidation->createDateFromString($value); |
|
46
|
1 |
|
if ($date instanceof \DateTime) { |
|
47
|
1 |
|
return $date->setTime(0, 0, 0); // Valid |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Exceptions are caught in order to be processed later |
|
55
|
|
|
* @param mixed $value null|DateTime Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 or null ? |
|
56
|
|
|
* |
|
57
|
|
|
* @return \DateTime|null |
|
|
|
|
|
|
58
|
|
|
*/ |
|
59
|
|
|
public function mustBeDateTimeOrEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
60
|
|
|
{ |
|
61
|
1 |
|
if (empty($value)) { |
|
62
|
1 |
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
return $this->mustBeDateTime($value, $propertyPath, $parentValidator, $exceptionMessage); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Exceptions are caught in order to be processed later |
|
70
|
|
|
* @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ? |
|
71
|
|
|
* |
|
72
|
|
|
* @return \DateTime|false |
|
73
|
|
|
*/ |
|
74
|
|
|
public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$dateValidation = new DateValidation(); |
|
77
|
|
|
|
|
78
|
1 |
|
$this->validationEngine->validateFieldValue( |
|
79
|
1 |
|
$parentValidator ?: $this, |
|
80
|
1 |
|
function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) { |
|
81
|
1 |
View Code Duplication |
if (null === $value || ! $dateValidation->isDateTimeValid($value)) { |
|
|
|
|
|
|
82
|
1 |
|
throw new InvalidArgumentException( |
|
83
|
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), |
|
84
|
1 |
|
0, |
|
85
|
|
|
$propertyPath, |
|
86
|
|
|
$value |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
1 |
|
} |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
1 |
|
if (null === $value) { |
|
93
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
$date = $dateValidation->createDateTimeFromString($value); |
|
97
|
|
|
|
|
98
|
1 |
|
if ($date instanceof \DateTime) { |
|
99
|
1 |
|
return $date; // Valid |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
return $this->createDefaultDateTime(); // Invalid |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function createDefaultDateTime(): \DateTime |
|
106
|
|
|
{ |
|
107
|
1 |
|
$date = new \DateTime(); |
|
108
|
|
|
|
|
109
|
1 |
|
return $date->setTime(0, 0, 0); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.