| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use DateInterval; |
| 6 |
use DateTime; |
| 7 |
use DateTimeInterface; |
| 8 |
use DateTimeZone; |
| 9 |
use Safe\Exceptions\DatetimeException; |
| 10 |
|
| 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 |
*/ |
| 16 |
class DateTimeImmutable extends \DateTimeImmutable |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @var \DateTimeImmutable |
| 20 |
*/ |
| 21 |
private $innerDateTime; |
| 22 |
|
| 23 |
/** |
| 24 |
* DateTimeImmutable constructor. |
| 25 |
* @param string $time |
| 26 |
* @param DateTimeZone|null $timezone |
| 27 |
* @throws \Exception |
| 28 |
*/ |
| 29 |
public function __construct($time = 'now', $timezone = null) |
| 30 |
{ |
| 31 |
parent::__construct($time, $timezone); |
| 32 |
$this->innerDateTime = new parent($time, $timezone); |
| 33 |
} |
| 34 |
|
| 35 |
//switch between regular datetime and safe version |
| 36 |
public static function createFromRegular(\DateTimeImmutable $datetime): self |
| 37 |
{ |
| 38 |
$safeDatetime = new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone()); //we need to also update the wrapper to not break the operators '<' and '>' |
| 39 |
$safeDatetime->innerDateTime = $datetime; //to make sure we don't lose information because of the format(). |
| 40 |
return $safeDatetime; |
| 41 |
} |
| 42 |
|
| 43 |
//usefull if you need to switch back to regular DateTimeImmutable (for example when using DatePeriod) |
| 44 |
public function getInnerDateTime(): \DateTimeImmutable |
| 45 |
{ |
| 46 |
return $this->innerDateTime; |
| 47 |
} |
| 48 |
|
| 49 |
///////////////////////////////////////////////////////////////////////////// |
| 50 |
// overload functions with false errors |
| 51 |
|
| 52 |
/** |
| 53 |
* @param string $format |
| 54 |
* @param string $time |
| 55 |
* @param DateTimeZone|null $timezone |
| 56 |
* @throws DatetimeException |
| 57 |
*/ |
| 58 |
public static function createFromFormat($format, $time, $timezone = null): self |
| 59 |
{ |
| 60 |
$datetime = parent::createFromFormat($format, $time, $timezone); |
| 61 |
if ($datetime === false) { |
| 62 |
throw DatetimeException::createFromPhpError(); |
| 63 |
} |
| 64 |
return self::createFromRegular($datetime); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param string $format |
| 69 |
* @return string |
| 70 |
* @throws DatetimeException |
| 71 |
*/ |
| 72 |
public function format($format): string |
| 73 |
{ |
| 74 |
/** @var string|false $result */ |
| 75 |
$result = $this->innerDateTime->format($format); |
| 76 |
if ($result === false) { |
| 77 |
throw DatetimeException::createFromPhpError(); |
| 78 |
} |
| 79 |
return $result; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param DateTimeInterface $datetime2 |
| 84 |
* @param bool $absolute |
| 85 |
* @return DateInterval |
| 86 |
* @throws DatetimeException |
| 87 |
*/ |
| 88 |
public function diff($datetime2, $absolute = false): DateInterval |
| 89 |
{ |
| 90 |
/** @var \DateInterval|false $result */ |
| 91 |
$result = $this->innerDateTime->diff($datetime2, $absolute); |
| 92 |
if ($result === false) { |
| 93 |
throw DatetimeException::createFromPhpError(); |
| 94 |
} |
| 95 |
return $result; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @param string $modify |
| 100 |
* @return DateTimeImmutable |
| 101 |
* @throws DatetimeException |
| 102 |
*/ |
| 103 |
public function modify($modify): self |
| 104 |
{ |
| 105 |
/** @var \DateTimeImmutable|false $result */ |
| 106 |
$result = $this->innerDateTime->modify($modify); |
| 107 |
if ($result === false) { |
| 108 |
throw DatetimeException::createFromPhpError(); |
| 109 |
} |
| 110 |
return self::createFromRegular($result); //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @param int $year |
| 115 |
* @param int $month |
| 116 |
* @param int $day |
| 117 |
* @return DateTimeImmutable |
| 118 |
* @throws DatetimeException |
| 119 |
*/ |
| 120 |
public function setDate($year, $month, $day): self |
| 121 |
{ |
| 122 |
/** @var \DateTimeImmutable|false $result */ |
| 123 |
$result = $this->innerDateTime->setDate($year, $month, $day); |
| 124 |
if ($result === false) { |
| 125 |
throw DatetimeException::createFromPhpError(); |
| 126 |
} |
| 127 |
return self::createFromRegular($result); //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @param int $year |
| 132 |
* @param int $week |
| 133 |
* @param int $day |
| 134 |
* @return DateTimeImmutable |
| 135 |
* @throws DatetimeException |
| 136 |
*/ |
| 137 |
public function setISODate($year, $week, $day = 1): self |
| 138 |
{ |
| 139 |
/** @var \DateTimeImmutable|false $result */ |
| 140 |
$result = $this->innerDateTime->setISODate($year, $week, $day); |
| 141 |
if ($result === false) { |
| 142 |
throw DatetimeException::createFromPhpError(); |
| 143 |
} |
| 144 |
return self::createFromRegular($result); //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @param int $hour |
| 149 |
* @param int $minute |
| 150 |
* @param int $second |
| 151 |
* @param int $microseconds |
| 152 |
* @return DateTimeImmutable |
| 153 |
* @throws DatetimeException |
| 154 |
*/ |
| 155 |
public function setTime($hour, $minute, $second = 0, $microseconds = 0): self |
| 156 |
{ |
| 157 |
/** @var \DateTimeImmutable|false $result */ |
| 158 |
$result = $this->innerDateTime->setTime($hour, $minute, $second, $microseconds); |
| 159 |
if ($result === false) { |
| 160 |
throw DatetimeException::createFromPhpError(); |
| 161 |
} |
| 162 |
return self::createFromRegular($result); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* @param int $unixtimestamp |
| 167 |
* @return DateTimeImmutable |
| 168 |
* @throws DatetimeException |
| 169 |
*/ |
| 170 |
public function setTimestamp($unixtimestamp): self |
| 171 |
{ |
| 172 |
/** @var \DateTimeImmutable|false $result */ |
| 173 |
$result = $this->innerDateTime->setTimestamp($unixtimestamp); |
| 174 |
if ($result === false) { |
| 175 |
throw DatetimeException::createFromPhpError(); |
| 176 |
} |
| 177 |
return self::createFromRegular($result); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* @param DateTimeZone $timezone |
| 182 |
* @return DateTimeImmutable |
| 183 |
* @throws DatetimeException |
| 184 |
*/ |
| 185 |
public function setTimezone($timezone): self |
| 186 |
{ |
| 187 |
/** @var \DateTimeImmutable|false $result */ |
| 188 |
$result = $this->innerDateTime->setTimezone($timezone); |
| 189 |
if ($result === false) { |
| 190 |
throw DatetimeException::createFromPhpError(); |
| 191 |
} |
| 192 |
return self::createFromRegular($result); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* @param DateInterval $interval |
| 197 |
* @return DateTimeImmutable |
| 198 |
* @throws DatetimeException |
| 199 |
*/ |
| 200 |
public function sub($interval): self |
| 201 |
{ |
| 202 |
/** @var \DateTimeImmutable|false $result */ |
| 203 |
$result = $this->innerDateTime->sub($interval); |
| 204 |
if ($result === false) { |
| 205 |
throw DatetimeException::createFromPhpError(); |
| 206 |
} |
| 207 |
return self::createFromRegular($result); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @throws DatetimeException |
| 212 |
*/ |
| 213 |
public function getOffset(): int |
| 214 |
{ |
| 215 |
/** @var int|false $result */ |
| 216 |
$result = $this->innerDateTime->getOffset(); |
| 217 |
if ($result === false) { |
| 218 |
throw DatetimeException::createFromPhpError(); |
| 219 |
} |
| 220 |
return $result; |
| 221 |
} |
| 222 |
|
| 223 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 224 |
//overload getters to use the inner datetime immutable instead of itself |
| 225 |
|
| 226 |
/** |
| 227 |
* @param DateInterval $interval |
| 228 |
* @return DateTimeImmutable |
| 229 |
*/ |
| 230 |
public function add($interval): self |
| 231 |
{ |
| 232 |
return self::createFromRegular($this->innerDateTime->add($interval)); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* @param DateTime $dateTime |
| 237 |
* @return DateTimeImmutable |
| 238 |
*/ |
| 239 |
public static function createFromMutable($dateTime): self |
| 240 |
{ |
| 241 |
return self::createFromRegular(parent::createFromMutable($dateTime)); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @param mixed[] $array |
| 246 |
* @return DateTimeImmutable |
| 247 |
*/ |
| 248 |
public static function __set_state($array): self |
| 249 |
{ |
| 250 |
return self::createFromRegular(parent::__set_state($array)); |
| 251 |
} |
| 252 |
|
| 253 |
public function getTimezone(): DateTimeZone |
| 254 |
{ |
| 255 |
return $this->innerDateTime->getTimezone(); |
| 256 |
} |
| 257 |
|
| 258 |
public function getTimestamp(): int |
| 259 |
{ |
| 260 |
return $this->innerDateTime->getTimestamp(); |
| 261 |
} |
| 262 |
} |
| 263 |
|