|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Utility; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class DateTimePlus |
|
10
|
|
|
* Based on briannesbitt/Carbon |
|
11
|
|
|
* http://carbon.nesbot.com/ |
|
12
|
|
|
* @package Nip\Utility |
|
13
|
|
|
* |
|
14
|
|
|
* @property $day |
|
15
|
|
|
* @property $month |
|
16
|
|
|
* @property $year |
|
17
|
|
|
* @property $hour |
|
18
|
|
|
* @property $minute |
|
19
|
|
|
* @property $offset |
|
20
|
|
|
* @property $second |
|
21
|
|
|
*/ |
|
22
|
|
|
class DateTimePlus extends DateTime |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/////////////////////////////////////////////////////////////////// |
|
26
|
|
|
///////////////////////// GETTERS AND SETTERS ///////////////////// |
|
27
|
|
|
/////////////////////////////////////////////////////////////////// |
|
28
|
|
|
/** |
|
29
|
|
|
* Get a part of the Carbon object |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $name |
|
32
|
|
|
* |
|
33
|
|
|
* @throws InvalidArgumentException |
|
34
|
|
|
* |
|
35
|
|
|
* @return string|int|\DateTimeZone |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __get($name) |
|
38
|
|
|
{ |
|
39
|
|
|
switch (true) { |
|
40
|
|
|
case array_key_exists($name, $formats = [ |
|
41
|
|
|
'year' => 'Y', |
|
42
|
|
|
'yearIso' => 'o', |
|
43
|
|
|
'month' => 'n', |
|
44
|
|
|
'day' => 'j', |
|
45
|
|
|
'hour' => 'G', |
|
46
|
|
|
'minute' => 'i', |
|
47
|
|
|
'second' => 's', |
|
48
|
|
|
'micro' => 'u', |
|
49
|
|
|
'dayOfWeek' => 'w', |
|
50
|
|
|
'dayOfYear' => 'z', |
|
51
|
|
|
'weekOfYear' => 'W', |
|
52
|
|
|
'daysInMonth' => 't', |
|
53
|
|
|
'timestamp' => 'U', |
|
54
|
|
|
]): |
|
55
|
|
|
return (int)$this->format($formats[$name]); |
|
|
|
|
|
|
56
|
|
|
case $name === 'weekOfMonth': |
|
57
|
|
|
return (int)ceil($this->day / static::DAYS_PER_WEEK); |
|
|
|
|
|
|
58
|
|
|
case $name === 'age': |
|
59
|
|
|
return (int)$this->diffInYears(); |
|
|
|
|
|
|
60
|
|
|
case $name === 'quarter': |
|
61
|
|
|
return (int)ceil($this->month / 3); |
|
|
|
|
|
|
62
|
|
|
case $name === 'offset': |
|
63
|
|
|
return $this->getOffset(); |
|
64
|
|
|
case $name === 'offsetHours': |
|
65
|
|
|
return $this->getOffset() / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR; |
|
|
|
|
|
|
66
|
|
|
case $name === 'dst': |
|
67
|
|
|
return $this->format('I') === '1'; |
|
|
|
|
|
|
68
|
|
|
case $name === 'local': |
|
69
|
|
|
return $this->offset === $this->copy()->setTimezone(date_default_timezone_get())->offset; |
|
|
|
|
|
|
70
|
|
|
case $name === 'utc': |
|
71
|
|
|
return $this->offset === 0; |
|
|
|
|
|
|
72
|
|
|
case $name === 'timezone' || $name === 'tz': |
|
73
|
|
|
return $this->getTimezone(); |
|
74
|
|
|
case $name === 'timezoneName' || $name === 'tzName': |
|
75
|
|
|
return $this->getTimezone()->getName(); |
|
76
|
|
|
default: |
|
77
|
|
|
throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name)); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Check if an attribute exists on the object |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $name |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __isset($name) |
|
89
|
|
|
{ |
|
90
|
|
|
try { |
|
91
|
|
|
$this->__get($name); |
|
92
|
|
|
} catch (InvalidArgumentException $e) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set a part of the Date object |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
* @param string|int|\DateTimeZone $value |
|
103
|
|
|
* |
|
104
|
|
|
* @throws InvalidArgumentException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function __set($name, $value) |
|
107
|
|
|
{ |
|
108
|
|
|
switch ($name) { |
|
109
|
|
|
case 'year': |
|
110
|
|
|
$this->setDate($value, $this->month, $this->day); |
|
|
|
|
|
|
111
|
|
|
break; |
|
112
|
|
|
case 'month': |
|
113
|
|
|
$this->setDate($this->year, $value, $this->day); |
|
|
|
|
|
|
114
|
|
|
break; |
|
115
|
|
|
case 'day': |
|
116
|
|
|
$this->setDate($this->year, $this->month, $value); |
|
|
|
|
|
|
117
|
|
|
break; |
|
118
|
|
|
case 'hour': |
|
119
|
|
|
$this->setTime($value, $this->minute, $this->second); |
|
|
|
|
|
|
120
|
|
|
break; |
|
121
|
|
|
case 'minute': |
|
122
|
|
|
$this->setTime($this->hour, $value, $this->second); |
|
|
|
|
|
|
123
|
|
|
break; |
|
124
|
|
|
case 'second': |
|
125
|
|
|
$this->setTime($this->hour, $this->minute, $value); |
|
|
|
|
|
|
126
|
|
|
break; |
|
127
|
|
|
case 'timestamp': |
|
128
|
|
|
parent::setTimestamp($value); |
|
|
|
|
|
|
129
|
|
|
break; |
|
130
|
|
|
case 'timezone': |
|
131
|
|
|
case 'tz': |
|
132
|
|
|
$this->setTimezone($value); |
|
133
|
|
|
break; |
|
134
|
|
|
default: |
|
135
|
|
|
throw new InvalidArgumentException(sprintf("Unknown setter '%s'", $name)); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Set the instance's year |
|
142
|
|
|
* @param int $value |
|
143
|
|
|
* @return static |
|
144
|
|
|
*/ |
|
145
|
|
|
public function year($value) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->year = $value; |
|
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Set the instance's month |
|
153
|
|
|
* @param int $value |
|
154
|
|
|
* @return static |
|
155
|
|
|
*/ |
|
156
|
|
|
public function month($value) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->month = $value; |
|
|
|
|
|
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Set the instance's day |
|
164
|
|
|
* |
|
165
|
|
|
* @param int $value |
|
166
|
|
|
* @return static |
|
167
|
|
|
*/ |
|
168
|
|
|
public function day($value) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->day = $value; |
|
|
|
|
|
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Set the instance's hour |
|
176
|
|
|
* |
|
177
|
|
|
* @param int $value |
|
178
|
|
|
* @return static |
|
179
|
|
|
*/ |
|
180
|
|
|
public function hour($value) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->hour = $value; |
|
|
|
|
|
|
183
|
|
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Set the instance's minute |
|
188
|
|
|
* |
|
189
|
|
|
* @param int $value |
|
190
|
|
|
* @return static |
|
191
|
|
|
*/ |
|
192
|
|
|
public function minute($value) |
|
193
|
|
|
{ |
|
194
|
|
|
$this->minute = $value; |
|
|
|
|
|
|
195
|
|
|
return $this; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Set the instance's second |
|
200
|
|
|
* |
|
201
|
|
|
* @param int $value |
|
202
|
|
|
* @return static |
|
203
|
|
|
*/ |
|
204
|
|
|
public function second($value) |
|
205
|
|
|
{ |
|
206
|
|
|
$this->second = $value; |
|
|
|
|
|
|
207
|
|
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @inheritdoc |
|
213
|
|
|
*/ |
|
214
|
|
|
public static function createFromFormat($format, $time, $timezone = null) |
|
215
|
|
|
{ |
|
216
|
|
|
if ($timezone !== null) { |
|
217
|
|
|
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($timezone)); |
|
|
|
|
|
|
218
|
|
|
} else { |
|
219
|
|
|
$dt = parent::createFromFormat($format, $time); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
if ($dt instanceof DateTime) { |
|
223
|
|
|
return static::instance($dt); |
|
224
|
|
|
} |
|
225
|
|
|
$errors = static::getLastErrors(); |
|
226
|
|
|
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors'])); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Modify date to this year |
|
231
|
|
|
* |
|
232
|
|
|
* @return static |
|
233
|
|
|
*/ |
|
234
|
|
|
public function currentYear() |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->year(date('Y')); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Create a DatePlus instance from a DateTime one. |
|
241
|
|
|
* @param \DateTime $dt |
|
242
|
|
|
* @return static |
|
243
|
|
|
*/ |
|
244
|
|
|
public static function instance(\DateTime $dt) |
|
245
|
|
|
{ |
|
246
|
|
|
if ($dt instanceof static) { |
|
247
|
|
|
return clone $dt; |
|
248
|
|
|
} |
|
249
|
|
|
return new static($dt->format('Y-m-d H:i:s.u'), $dt->getTimeZone()); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.