8
8
use DateTimeZone ;
9
9
use Safe \Exceptions \DatetimeException ;
10
10
11
- //this class is used to implement a safe version of the DatetimeImmutable class
11
+ /**
12
+ * This class is used to implement a safe version of the DatetimeImmutable class.
13
+ * While it technically overloads \DateTimeImmutable for typehint compatibility,
14
+ * it is actually used as a wrapper of \DateTimeImmutable, mostly to be able to overwrite functions like getTimestamp() while still being able to edit milliseconds via setTime().
15
+ */
12
16
class DateTimeImmutable extends \DateTimeImmutable
13
17
{
18
+ /**
19
+ * @var \DateTimeImmutable
20
+ */
21
+ private $ innerDateTime ;
22
+
23
+ public function __construct ($ time = 'now ' , $ timezone = null )
24
+ {
25
+ parent ::__construct ();
26
+ $ this ->innerDateTime = new parent ($ time , $ timezone );
27
+ }
28
+
14
29
//switch from regular datetime to safe version
15
30
private static function createFromRegular (\DateTimeImmutable $ datetime ): self
16
31
{
17
- return new self ($ datetime ->format ('Y-m-d H:i:s ' ), $ datetime ->getTimezone ());
32
+ $ safeDatetime = new self ();
33
+ $ safeDatetime ->innerDateTime = $ datetime ;
34
+ return $ safeDatetime ;
18
35
}
19
36
20
- public static function createFromFormat ($ format , $ time , DateTimeZone $ timezone = null ): self
37
+ /////////////////////////////////////////////////////////////////////////////
38
+ // overload functions with false errors
39
+
40
+ /**
41
+ * @param string $format
42
+ * @param string $time
43
+ * @param DateTimeZone|null $timezone
44
+ */
45
+ public static function createFromFormat ($ format , $ time , $ timezone = null ): self
21
46
{
22
47
$ datetime = parent ::createFromFormat ($ format , $ time , $ timezone );
23
48
if ($ datetime === false ) {
@@ -28,54 +53,38 @@ public static function createFromFormat($format, $time, DateTimeZone $timezone =
28
53
29
54
public function format ($ format ): string
30
55
{
31
- $ result = parent ::format ($ format );
56
+ /** @var string|false $result */
57
+ $ result = $ this ->innerDateTime ->format ($ format );
32
58
if ($ result === false ) {
33
59
throw DatetimeException::createFromPhpError ();
34
60
}
35
61
return $ result ;
36
62
}
37
63
38
- /**
39
- * @param DateTimeInterface $datetime2 <p>The date to compare to.</p>
40
- * @param bool $absolute [optional] <p>Should the interval be forced to be positive?</p>
41
- * @return DateInterval
42
- */
43
64
public function diff ($ datetime2 , $ absolute = false ): DateInterval
44
65
{
45
66
/** @var \DateInterval|false $result */
46
- $ result = parent :: diff ($ datetime2 , $ absolute );
67
+ $ result = $ this -> innerDateTime -> diff ($ datetime2 , $ absolute );
47
68
if ($ result === false ) {
48
69
throw DatetimeException::createFromPhpError ();
49
70
}
50
71
return $ result ;
51
72
}
52
73
53
- /**
54
- * @param string $modify <p>A date/time string. Valid formats are explained in
55
- * {@link https://secure.php.net/manual/en/datetime.formats.php Date and Time Formats}.</p>
56
- * @return DateTimeImmutable
57
- */
58
74
public function modify ($ modify ): self
59
75
{
60
76
/** @var \DateTimeImmutable|false $result */
61
- $ result = parent :: modify ($ modify );
77
+ $ result = $ this -> innerDateTime -> modify ($ modify );
62
78
if ($ result === false ) {
63
79
throw DatetimeException::createFromPhpError ();
64
80
}
65
81
return self ::createFromRegular ($ result ); //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable
66
82
}
67
83
68
- /**
69
- * @param int $year <p>Year of the date.</p>
70
- * @param int $month <p>Month of the date.</p>
71
- * @param int $day <p>Day of the date.</p>
72
- * @return DateTimeImmutable
73
- *
74
- */
75
84
public function setDate ($ year , $ month , $ day ): self
76
85
{
77
86
/** @var \DateTimeImmutable|false $result */
78
- $ result = parent :: setDate ($ year , $ month , $ day );
87
+ $ result = $ this -> innerDateTime -> setDate ($ year , $ month , $ day );
79
88
if ($ result === false ) {
80
89
throw DatetimeException::createFromPhpError ();
81
90
}
@@ -85,7 +94,7 @@ public function setDate($year, $month, $day): self
85
94
public function setISODate ($ year , $ week , $ day = 1 ): self
86
95
{
87
96
/** @var \DateTimeImmutable|false $result */
88
- $ result = parent :: setISODate ($ year , $ week , $ day );
97
+ $ result = $ this -> innerDateTime -> setISODate ($ year , $ week , $ day );
89
98
if ($ result === false ) {
90
99
throw DatetimeException::createFromPhpError ();
91
100
}
@@ -95,7 +104,7 @@ public function setISODate($year, $week, $day = 1): self
95
104
public function setTime ($ hour , $ minute , $ second = 0 , $ microseconds = 0 ): self
96
105
{
97
106
/** @var \DateTimeImmutable|false $result */
98
- $ result = parent :: setTime ($ hour , $ minute , $ second , $ microseconds );
107
+ $ result = $ this -> innerDateTime -> setTime ($ hour , $ minute , $ second , $ microseconds );
99
108
if ($ result === false ) {
100
109
throw DatetimeException::createFromPhpError ();
101
110
}
@@ -105,7 +114,7 @@ public function setTime($hour, $minute, $second = 0, $microseconds = 0): self
105
114
public function setTimestamp ($ unixtimestamp ): self
106
115
{
107
116
/** @var \DateTimeImmutable|false $result */
108
- $ result = parent :: setTimestamp ($ unixtimestamp );
117
+ $ result = $ this -> innerDateTime -> setTimestamp ($ unixtimestamp );
109
118
if ($ result === false ) {
110
119
throw DatetimeException::createFromPhpError ();
111
120
}
@@ -115,7 +124,7 @@ public function setTimestamp($unixtimestamp): self
115
124
public function setTimezone ($ timezone ): self
116
125
{
117
126
/** @var \DateTimeImmutable|false $result */
118
- $ result = parent :: setTimezone ($ timezone );
127
+ $ result = $ this -> innerDateTime -> setTimezone ($ timezone );
119
128
if ($ result === false ) {
120
129
throw DatetimeException::createFromPhpError ();
121
130
}
@@ -125,18 +134,29 @@ public function setTimezone($timezone): self
125
134
public function sub ($ interval ): self
126
135
{
127
136
/** @var \DateTimeImmutable|false $result */
128
- $ result = parent :: sub ($ interval );
137
+ $ result = $ this -> innerDateTime -> sub ($ interval );
129
138
if ($ result === false ) {
130
139
throw DatetimeException::createFromPhpError ();
131
140
}
132
141
return self ::createFromRegular ($ result );
133
142
}
134
143
135
- //theses functions are overload to actually return a safe instance, since datetimeimmutable re-instante itself
144
+ public function getOffset (): int
145
+ {
146
+ /** @var int|false $result */
147
+ $ result = $ this ->innerDateTime ->getOffset ();
148
+ if ($ result === false ) {
149
+ throw DatetimeException::createFromPhpError ();
150
+ }
151
+ return $ result ;
152
+ }
153
+
154
+ //////////////////////////////////////////////////////////////////////////////////////////
155
+ //overload getters to use the inner datetime immutable instead of itself
136
156
137
157
public function add ($ interval ): self
138
158
{
139
- return self ::createFromRegular (parent :: add ($ interval ));
159
+ return self ::createFromRegular ($ this -> innerDateTime -> add ($ interval ));
140
160
}
141
161
142
162
public static function createFromMutable ($ dateTime ): self
@@ -148,4 +168,14 @@ public static function __set_state(array $array): self
148
168
{
149
169
return self ::createFromRegular (parent ::__set_state ($ array ));
150
170
}
171
+
172
+ public function getTimezone (): DateTimeZone
173
+ {
174
+ return $ this ->innerDateTime ->getTimezone ();
175
+ }
176
+
177
+ public function getTimestamp (): int
178
+ {
179
+ return $ this ->innerDateTime ->getTimestamp ();
180
+ }
151
181
}
0 commit comments