|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Clock; |
| 13 | + |
| 14 | +/** |
| 15 | + * An immmutable DateTime with stricter error handling and return types than the native one. |
| 16 | + * |
| 17 | + * @author Nicolas Grekas <[email protected]> |
| 18 | + */ |
| 19 | +final class Moment extends \DateTimeImmutable |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @throws \DateMalformedStringException When $datetime is invalid |
| 23 | + */ |
| 24 | + public function __construct(string $datetime = 'now', \DateTimeZone $timezone = null, parent $reference = null) |
| 25 | + { |
| 26 | + $now = $reference ?? Clock::get()->now(); |
| 27 | + |
| 28 | + if ('now' !== $datetime) { |
| 29 | + if (!$now instanceof static) { |
| 30 | + $now = static::createFromInterface($now); |
| 31 | + } |
| 32 | + |
| 33 | + if (\PHP_VERSION_ID < 80300) { |
| 34 | + try { |
| 35 | + $timezone = (new parent($datetime, $timezone ?? $now->getTimezone()))->getTimezone(); |
| 36 | + } catch (\Exception $e) { |
| 37 | + throw new \DateMalformedStringException($e->getMessage(), $e->getCode(), $e); |
| 38 | + } |
| 39 | + } else { |
| 40 | + $timezone = (new parent($datetime, $timezone ?? $now->getTimezone()))->getTimezone(); |
| 41 | + } |
| 42 | + |
| 43 | + $now = $now->setTimezone($timezone)->modify($datetime); |
| 44 | + } elseif (null !== $timezone) { |
| 45 | + $now = $now->setTimezone($timezone); |
| 46 | + } |
| 47 | + |
| 48 | + if (\PHP_VERSION_ID < 80200) { |
| 49 | + $now = (array) $now; |
| 50 | + $this->date = $now['date']; |
| 51 | + $this->timezone_type = $now['timezone_type']; |
| 52 | + $this->timezone = $now['timezone']; |
| 53 | + $this->__wakeup(); |
| 54 | + |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $this->__unserialize((array) $now); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @throws \DateMalformedStringException When $format or $datetime are invalid |
| 63 | + */ |
| 64 | + public static function createFromFormat(string $format, string $datetime, \DateTimeZone $timezone = null): static |
| 65 | + { |
| 66 | + return parent::createFromFormat($format, $datetime, $timezone) ?: throw new \DateMalformedStringException(static::getLastErrors()['errors'][0] ?? 'Invalid date string or format.'); |
| 67 | + } |
| 68 | + |
| 69 | + public static function createFromInterface(\DateTimeInterface $object): static |
| 70 | + { |
| 71 | + return parent::createFromInterface($object); |
| 72 | + } |
| 73 | + |
| 74 | + public static function createFromMutable(\DateTime $object): static |
| 75 | + { |
| 76 | + return parent::createFromMutable($object); |
| 77 | + } |
| 78 | + |
| 79 | + public function add(\DateInterval $interval): static |
| 80 | + { |
| 81 | + return parent::add($interval); |
| 82 | + } |
| 83 | + |
| 84 | + public function sub(\DateInterval $interval): static |
| 85 | + { |
| 86 | + return parent::sub($interval); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @throws \DateMalformedStringException When $modifier is invalid |
| 91 | + */ |
| 92 | + public function modify(string $modifier): static |
| 93 | + { |
| 94 | + if (\PHP_VERSION_ID < 80300) { |
| 95 | + return @parent::modify($modifier) ?: throw new \DateMalformedStringException(error_get_last()['message'] ?? sprintf('Invalid modifier: "%s".', $modifier)); |
| 96 | + } |
| 97 | + |
| 98 | + return parent::modify($modifier); |
| 99 | + } |
| 100 | + |
| 101 | + public function setTimestamp(int $value): static |
| 102 | + { |
| 103 | + return parent::setTimestamp($value); |
| 104 | + } |
| 105 | + |
| 106 | + public function setDate(int $year, int $month, int $day): static |
| 107 | + { |
| 108 | + return parent::setDate($year, $month, $day); |
| 109 | + } |
| 110 | + |
| 111 | + public function setISODate(int $year, int $week, int $day = 1): static |
| 112 | + { |
| 113 | + return parent::setISODate($year, $week, $day); |
| 114 | + } |
| 115 | + |
| 116 | + public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): static |
| 117 | + { |
| 118 | + return parent::setTime($hour, $minute, $second, $microsecond); |
| 119 | + } |
| 120 | + |
| 121 | + public function setTimezone(\DateTimeZone $timezone): static |
| 122 | + { |
| 123 | + return parent::setTimezone($timezone); |
| 124 | + } |
| 125 | + |
| 126 | + public function getTimezone(): \DateTimeZone |
| 127 | + { |
| 128 | + return parent::getTimezone() ?: throw new \DateInvalidTimeZoneException('The Moment object has no timezone.'); |
| 129 | + } |
| 130 | +} |
0 commit comments