Exceptions
4 days ago
DateTime.php
4 days ago
DateTimeImmutable.php
4 days ago
special_cases.php
4 days ago
DateTime.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePressVendor\Safe; |
| 4 | |
| 5 | use DateInterval; |
| 6 | use DateTimeInterface; |
| 7 | use DateTimeZone; |
| 8 | use ProfilePressVendor\Safe\Exceptions\DatetimeException; |
| 9 | /** this class implements a safe version of the Datetime class */ |
| 10 | class DateTime extends \DateTime |
| 11 | { |
| 12 | //switch from regular datetime to safe version |
| 13 | private static function createFromRegular(\DateTime $datetime): self |
| 14 | { |
| 15 | return new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone()); |
| 16 | } |
| 17 | /** |
| 18 | * @param string $format |
| 19 | * @param string $time |
| 20 | * @param DateTimeZone|null $timezone |
| 21 | * @throws DatetimeException |
| 22 | */ |
| 23 | public static function createFromFormat($format, $time, $timezone = null): self |
| 24 | { |
| 25 | $datetime = parent::createFromFormat($format, $time, $timezone); |
| 26 | if ($datetime === \false) { |
| 27 | throw DatetimeException::createFromPhpError(); |
| 28 | } |
| 29 | return self::createFromRegular($datetime); |
| 30 | } |
| 31 | /** |
| 32 | * @param DateTimeInterface $datetime2 The date to compare to. |
| 33 | * @param boolean $absolute [optional] Whether to return absolute difference. |
| 34 | * @return DateInterval The DateInterval object representing the difference between the two dates. |
| 35 | * @throws DatetimeException |
| 36 | */ |
| 37 | public function diff($datetime2, $absolute = \false): DateInterval |
| 38 | { |
| 39 | /** @var \DateInterval|false $result */ |
| 40 | $result = parent::diff($datetime2, $absolute); |
| 41 | if ($result === \false) { |
| 42 | throw DatetimeException::createFromPhpError(); |
| 43 | } |
| 44 | return $result; |
| 45 | } |
| 46 | /** |
| 47 | * @param string $modify A date/time string. Valid formats are explained in <a href="https://secure.php.net/manual/en/datetime.formats.php">Date and Time Formats</a>. |
| 48 | * @return DateTime Returns the DateTime object for method chaining. |
| 49 | * @throws DatetimeException |
| 50 | */ |
| 51 | public function modify($modify): self |
| 52 | { |
| 53 | /** @var DateTime|false $result */ |
| 54 | $result = parent::modify($modify); |
| 55 | if ($result === \false) { |
| 56 | throw DatetimeException::createFromPhpError(); |
| 57 | } |
| 58 | return $result; |
| 59 | } |
| 60 | /** |
| 61 | * @param int $year |
| 62 | * @param int $month |
| 63 | * @param int $day |
| 64 | * @return DateTime |
| 65 | * @throws DatetimeException |
| 66 | */ |
| 67 | public function setDate($year, $month, $day): self |
| 68 | { |
| 69 | /** @var DateTime|false $result */ |
| 70 | $result = parent::setDate($year, $month, $day); |
| 71 | if ($result === \false) { |
| 72 | throw DatetimeException::createFromPhpError(); |
| 73 | } |
| 74 | return $result; |
| 75 | } |
| 76 | } |
| 77 |