1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digitonic\ApiTestSuite\DataGeneration\Rules; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Digitonic\ApiTestSuite\DataGeneration\Contracts\Rule; |
|
|
|
|
7
|
|
|
use Digitonic\ApiTestSuite\DataGeneration\Rules\Rule as BaseRule; |
8
|
|
|
|
9
|
|
|
class DateRule extends BaseRule implements Rule |
10
|
|
|
{ |
11
|
|
|
private $format = 'Y-m-d H:i:s'; |
12
|
|
|
private $before; |
13
|
|
|
private $after; |
14
|
|
|
private $strictAfter; |
15
|
|
|
private $strictBefore; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param array $payload |
19
|
|
|
* @param $field |
20
|
|
|
* @param array $rules |
21
|
|
|
* @param $newValueSeed |
22
|
|
|
* @param $class |
23
|
|
|
* @return mixed |
24
|
|
|
* @throws \Exception |
25
|
|
|
*/ |
26
|
|
|
public function handle(array &$payload, $field, array $rules, $newValueSeed, $class, $user) |
27
|
|
|
{ |
28
|
|
|
// todo set defaults if not set |
29
|
|
|
$this->after = empty($this->after) ? $this->getTimestamp('-80years') : $this->after; |
30
|
|
|
$this->before = empty($this->before) ? $this->getTimestamp('+80years') : $this->before; |
31
|
|
|
|
32
|
|
|
if (isset($payload[$field])) { |
33
|
|
|
$defaultTimestamp = $this->getTimestamp($payload[$field]); |
34
|
|
|
$after = max($defaultTimestamp, $this->after); |
35
|
|
|
$before = min($after, $this->before); |
36
|
|
|
} else { |
37
|
|
|
$after = $this->after; |
38
|
|
|
$before = $this->before; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$payload[$field] = Carbon::createFromTimestamp(random_int($after, $before))->format($this->format); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function getTimestamp($dateString) |
45
|
|
|
{ |
46
|
|
|
return Carbon::parse($dateString)->getTimestamp(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function getFormat() |
53
|
|
|
{ |
54
|
|
|
return $this->format; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed $format |
59
|
|
|
*/ |
60
|
|
|
public function setFormat($format): void |
61
|
|
|
{ |
62
|
|
|
$this->format = $format; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function getBefore() |
69
|
|
|
{ |
70
|
|
|
return $this->before; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $before |
75
|
|
|
*/ |
76
|
|
|
public function setBefore($before, $strict = true): void |
77
|
|
|
{ |
78
|
|
|
$this->before = $before; |
79
|
|
|
$this->strictBefore = $strict; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function getAfter() |
86
|
|
|
{ |
87
|
|
|
return $this->after; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $after |
92
|
|
|
*/ |
93
|
|
|
public function setAfter($after, $strict = true): void |
94
|
|
|
{ |
95
|
|
|
$this->after = $after; |
96
|
|
|
$this->strictAfter = $strict; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function getStrictAfter() |
103
|
|
|
{ |
104
|
|
|
return $this->strictAfter; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param mixed $strictAfter |
109
|
|
|
*/ |
110
|
|
|
public function setStrictAfter($strictAfter): void |
111
|
|
|
{ |
112
|
|
|
$this->strictAfter = $strictAfter; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function getStrictBefore() |
119
|
|
|
{ |
120
|
|
|
return $this->strictBefore; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param mixed $strictBefore |
125
|
|
|
*/ |
126
|
|
|
public function setStrictBefore($strictBefore): void |
127
|
|
|
{ |
128
|
|
|
$this->strictBefore = $strictBefore; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: