PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / trunk
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress vtrunk
4.17.2 4.17.1 4.17.0 4.16.19 4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / third-party / vendor / thecodingmachine / safe / lib / DateTimeImmutable.php
wp-user-avatar / third-party / vendor / thecodingmachine / safe / lib Last commit date
Exceptions 1 week ago DateTime.php 1 week ago DateTimeImmutable.php 1 week ago special_cases.php 1 week ago
DateTimeImmutable.php
246 lines
1 <?php
2
3 namespace ProfilePressVendor\Safe;
4
5 use DateInterval;
6 use DateTime;
7 use DateTimeInterface;
8 use DateTimeZone;
9 use ProfilePressVendor\Safe\Exceptions\DatetimeException;
10 /**
11 * This class is used to implement a safe version of the DatetimeImmutable class.
12 * While it technically overloads \DateTimeImmutable for typehint compatibility,
13 * 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().
14 */
15 class DateTimeImmutable extends \DateTimeImmutable
16 {
17 /**
18 * @var \DateTimeImmutable
19 */
20 private $innerDateTime;
21 /**
22 * DateTimeImmutable constructor.
23 * @param string $time
24 * @param DateTimeZone|null $timezone
25 * @throws \Exception
26 */
27 public function __construct($time = 'now', $timezone = null)
28 {
29 parent::__construct($time, $timezone);
30 $this->innerDateTime = new parent($time, $timezone);
31 }
32 //switch between regular datetime and safe version
33 public static function createFromRegular(\DateTimeImmutable $datetime): self
34 {
35 $safeDatetime = new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone());
36 //we need to also update the wrapper to not break the operators '<' and '>'
37 $safeDatetime->innerDateTime = $datetime;
38 //to make sure we don't lose information because of the format().
39 return $safeDatetime;
40 }
41 //usefull if you need to switch back to regular DateTimeImmutable (for example when using DatePeriod)
42 public function getInnerDateTime(): \DateTimeImmutable
43 {
44 return $this->innerDateTime;
45 }
46 /////////////////////////////////////////////////////////////////////////////
47 // overload functions with false errors
48 /**
49 * @param string $format
50 * @param string $time
51 * @param DateTimeZone|null $timezone
52 * @throws DatetimeException
53 */
54 public static function createFromFormat($format, $time, $timezone = null): self
55 {
56 $datetime = parent::createFromFormat($format, $time, $timezone);
57 if ($datetime === \false) {
58 throw DatetimeException::createFromPhpError();
59 }
60 return self::createFromRegular($datetime);
61 }
62 /**
63 * @param string $format
64 * @return string
65 * @throws DatetimeException
66 */
67 public function format($format): string
68 {
69 /** @var string|false $result */
70 $result = $this->innerDateTime->format($format);
71 if ($result === \false) {
72 throw DatetimeException::createFromPhpError();
73 }
74 return $result;
75 }
76 /**
77 * @param DateTimeInterface $datetime2
78 * @param bool $absolute
79 * @return DateInterval
80 * @throws DatetimeException
81 */
82 public function diff($datetime2, $absolute = \false): DateInterval
83 {
84 /** @var \DateInterval|false $result */
85 $result = $this->innerDateTime->diff($datetime2, $absolute);
86 if ($result === \false) {
87 throw DatetimeException::createFromPhpError();
88 }
89 return $result;
90 }
91 /**
92 * @param string $modify
93 * @return DateTimeImmutable
94 * @throws DatetimeException
95 */
96 public function modify($modify): self
97 {
98 /** @var \DateTimeImmutable|false $result */
99 $result = $this->innerDateTime->modify($modify);
100 if ($result === \false) {
101 throw DatetimeException::createFromPhpError();
102 }
103 return self::createFromRegular($result);
104 //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable
105 }
106 /**
107 * @param int $year
108 * @param int $month
109 * @param int $day
110 * @return DateTimeImmutable
111 * @throws DatetimeException
112 */
113 public function setDate($year, $month, $day): self
114 {
115 /** @var \DateTimeImmutable|false $result */
116 $result = $this->innerDateTime->setDate($year, $month, $day);
117 if ($result === \false) {
118 throw DatetimeException::createFromPhpError();
119 }
120 return self::createFromRegular($result);
121 //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable
122 }
123 /**
124 * @param int $year
125 * @param int $week
126 * @param int $day
127 * @return DateTimeImmutable
128 * @throws DatetimeException
129 */
130 public function setISODate($year, $week, $day = 1): self
131 {
132 /** @var \DateTimeImmutable|false $result */
133 $result = $this->innerDateTime->setISODate($year, $week, $day);
134 if ($result === \false) {
135 throw DatetimeException::createFromPhpError();
136 }
137 return self::createFromRegular($result);
138 //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable
139 }
140 /**
141 * @param int $hour
142 * @param int $minute
143 * @param int $second
144 * @param int $microseconds
145 * @return DateTimeImmutable
146 * @throws DatetimeException
147 */
148 public function setTime($hour, $minute, $second = 0, $microseconds = 0): self
149 {
150 /** @var \DateTimeImmutable|false $result */
151 $result = $this->innerDateTime->setTime($hour, $minute, $second, $microseconds);
152 if ($result === \false) {
153 throw DatetimeException::createFromPhpError();
154 }
155 return self::createFromRegular($result);
156 }
157 /**
158 * @param int $unixtimestamp
159 * @return DateTimeImmutable
160 * @throws DatetimeException
161 */
162 public function setTimestamp($unixtimestamp): self
163 {
164 /** @var \DateTimeImmutable|false $result */
165 $result = $this->innerDateTime->setTimestamp($unixtimestamp);
166 if ($result === \false) {
167 throw DatetimeException::createFromPhpError();
168 }
169 return self::createFromRegular($result);
170 }
171 /**
172 * @param DateTimeZone $timezone
173 * @return DateTimeImmutable
174 * @throws DatetimeException
175 */
176 public function setTimezone($timezone): self
177 {
178 /** @var \DateTimeImmutable|false $result */
179 $result = $this->innerDateTime->setTimezone($timezone);
180 if ($result === \false) {
181 throw DatetimeException::createFromPhpError();
182 }
183 return self::createFromRegular($result);
184 }
185 /**
186 * @param DateInterval $interval
187 * @return DateTimeImmutable
188 * @throws DatetimeException
189 */
190 public function sub($interval): self
191 {
192 /** @var \DateTimeImmutable|false $result */
193 $result = $this->innerDateTime->sub($interval);
194 if ($result === \false) {
195 throw DatetimeException::createFromPhpError();
196 }
197 return self::createFromRegular($result);
198 }
199 /**
200 * @throws DatetimeException
201 */
202 public function getOffset(): int
203 {
204 /** @var int|false $result */
205 $result = $this->innerDateTime->getOffset();
206 if ($result === \false) {
207 throw DatetimeException::createFromPhpError();
208 }
209 return $result;
210 }
211 //////////////////////////////////////////////////////////////////////////////////////////
212 //overload getters to use the inner datetime immutable instead of itself
213 /**
214 * @param DateInterval $interval
215 * @return DateTimeImmutable
216 */
217 public function add($interval): self
218 {
219 return self::createFromRegular($this->innerDateTime->add($interval));
220 }
221 /**
222 * @param DateTime $dateTime
223 * @return DateTimeImmutable
224 */
225 public static function createFromMutable($dateTime): self
226 {
227 return self::createFromRegular(parent::createFromMutable($dateTime));
228 }
229 /**
230 * @param mixed[] $array
231 * @return DateTimeImmutable
232 */
233 public static function __set_state($array): self
234 {
235 return self::createFromRegular(parent::__set_state($array));
236 }
237 public function getTimezone(): DateTimeZone
238 {
239 return $this->innerDateTime->getTimezone();
240 }
241 public function getTimestamp(): int
242 {
243 return $this->innerDateTime->getTimestamp();
244 }
245 }
246