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 |