|
| 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 | +//this class is used to implement a safe version of the DatetimeImmutable class |
| 12 | +class DateTimeImmutable extends \DateTimeImmutable |
| 13 | +{ |
| 14 | + //switch from regular datetime to safe version |
| 15 | + private static function createFromRegular(\DateTimeImmutable $datetime): self |
| 16 | + { |
| 17 | + return new self($datetime->format('Y-m-d H:i:s'), $datetime->getTimezone()); |
| 18 | + } |
| 19 | + |
| 20 | + public static function createFromFormat($format, $time, DateTimeZone $timezone = null): self |
| 21 | + { |
| 22 | + $datetime = parent::createFromFormat($format, $time, $timezone); |
| 23 | + if ($datetime === false) { |
| 24 | + throw DatetimeException::createFromPhpError(); |
| 25 | + } |
| 26 | + return self::createFromRegular($datetime); |
| 27 | + } |
| 28 | + |
| 29 | + public function format($format): string |
| 30 | + { |
| 31 | + $result = parent::format($format); |
| 32 | + if ($result === false) { |
| 33 | + throw DatetimeException::createFromPhpError(); |
| 34 | + } |
| 35 | + return $result; |
| 36 | + } |
| 37 | + |
| 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 | + public function diff($datetime2, $absolute = false): DateInterval |
| 44 | + { |
| 45 | + /** @var \DateInterval|false $result */ |
| 46 | + $result = parent::diff($datetime2, $absolute); |
| 47 | + if ($result === false) { |
| 48 | + throw DatetimeException::createFromPhpError(); |
| 49 | + } |
| 50 | + return $result; |
| 51 | + } |
| 52 | + |
| 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 | + public function modify($modify): self |
| 59 | + { |
| 60 | + /** @var \DateTimeImmutable|false $result */ |
| 61 | + $result = parent::modify($modify); |
| 62 | + if ($result === false) { |
| 63 | + throw DatetimeException::createFromPhpError(); |
| 64 | + } |
| 65 | + return self::createFromRegular($result); //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable |
| 66 | + } |
| 67 | + |
| 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 | + public function setDate($year, $month, $day): self |
| 76 | + { |
| 77 | + /** @var \DateTimeImmutable|false $result */ |
| 78 | + $result = parent::setDate($year, $month, $day); |
| 79 | + if ($result === false) { |
| 80 | + throw DatetimeException::createFromPhpError(); |
| 81 | + } |
| 82 | + return self::createFromRegular($result); //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable |
| 83 | + } |
| 84 | + |
| 85 | + public function setISODate($year, $week, $day = 1): self |
| 86 | + { |
| 87 | + /** @var \DateTimeImmutable|false $result */ |
| 88 | + $result = parent::setISODate($year, $week, $day); |
| 89 | + if ($result === false) { |
| 90 | + throw DatetimeException::createFromPhpError(); |
| 91 | + } |
| 92 | + return self::createFromRegular($result); //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable |
| 93 | + } |
| 94 | + |
| 95 | + public function setTime($hour, $minute, $second = 0, $microseconds = 0): self |
| 96 | + { |
| 97 | + /** @var \DateTimeImmutable|false $result */ |
| 98 | + $result = parent::setTime($hour, $minute, $second, $microseconds); |
| 99 | + if ($result === false) { |
| 100 | + throw DatetimeException::createFromPhpError(); |
| 101 | + } |
| 102 | + return self::createFromRegular($result); |
| 103 | + } |
| 104 | + |
| 105 | + public function setTimestamp($unixtimestamp): self |
| 106 | + { |
| 107 | + /** @var \DateTimeImmutable|false $result */ |
| 108 | + $result = parent::setTimestamp($unixtimestamp); |
| 109 | + if ($result === false) { |
| 110 | + throw DatetimeException::createFromPhpError(); |
| 111 | + } |
| 112 | + return self::createFromRegular($result); |
| 113 | + } |
| 114 | + |
| 115 | + public function setTimezone($timezone): self |
| 116 | + { |
| 117 | + /** @var \DateTimeImmutable|false $result */ |
| 118 | + $result = parent::setTimezone($timezone); |
| 119 | + if ($result === false) { |
| 120 | + throw DatetimeException::createFromPhpError(); |
| 121 | + } |
| 122 | + return self::createFromRegular($result); |
| 123 | + } |
| 124 | + |
| 125 | + public function sub($interval): self |
| 126 | + { |
| 127 | + /** @var \DateTimeImmutable|false $result */ |
| 128 | + $result = parent::sub($interval); |
| 129 | + if ($result === false) { |
| 130 | + throw DatetimeException::createFromPhpError(); |
| 131 | + } |
| 132 | + return self::createFromRegular($result); |
| 133 | + } |
| 134 | + |
| 135 | + //theses functions are overload to actually return a safe instance, since datetimeimmutable re-instante itself |
| 136 | + |
| 137 | + public function add($interval): self |
| 138 | + { |
| 139 | + return self::createFromRegular(parent::add($interval)); |
| 140 | + } |
| 141 | + |
| 142 | + public static function createFromMutable($dateTime): self |
| 143 | + { |
| 144 | + return self::createFromRegular(parent::createFromMutable($dateTime)); |
| 145 | + } |
| 146 | + |
| 147 | + public static function __set_state(array $array): self |
| 148 | + { |
| 149 | + return self::createFromRegular(parent::__set_state($array)); |
| 150 | + } |
| 151 | +} |
0 commit comments