|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Laudis Neo4j package. |
| 5 | + * |
| 6 | + * (c) Laudis technologies <http://laudis.tech> |
| 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 Laudis\Neo4j\Types; |
| 13 | + |
| 14 | +use Bolt\structures\IStructure; |
| 15 | +use DateTimeImmutable; |
| 16 | +use DateTimeZone; |
| 17 | +use Exception; |
| 18 | +use Laudis\Neo4j\Contracts\BoltConvertibleInterface; |
| 19 | +use function sprintf; |
| 20 | + |
| 21 | +/** |
| 22 | + * A date represented by seconds and nanoseconds since unix epoch, enriched with a timezone identifier. |
| 23 | + * |
| 24 | + * @psalm-immutable |
| 25 | + * |
| 26 | + * @extends AbstractPropertyObject<int|string, int|string> |
| 27 | + */ |
| 28 | +final class DateTimezoneId extends AbstractPropertyObject implements BoltConvertibleInterface |
| 29 | +{ |
| 30 | + private int $seconds; |
| 31 | + private int $nanoseconds; |
| 32 | + private string $tzId; |
| 33 | + |
| 34 | + public function __construct(int $seconds, int $nanoseconds, string $tzId) |
| 35 | + { |
| 36 | + $this->seconds = $seconds; |
| 37 | + $this->nanoseconds = $nanoseconds; |
| 38 | + $this->tzId = $tzId; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns the amount of seconds since unix epoch. |
| 43 | + */ |
| 44 | + public function getSeconds(): int |
| 45 | + { |
| 46 | + return $this->seconds; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns the amount of nanoseconds after the seconds have passed. |
| 51 | + */ |
| 52 | + public function getNanoseconds(): int |
| 53 | + { |
| 54 | + return $this->nanoseconds; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns the timezone identifier. |
| 59 | + */ |
| 60 | + public function getTimezoneIdentifier(): string |
| 61 | + { |
| 62 | + return $this->tzId; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Casts to an immutable date time. |
| 67 | + * |
| 68 | + * @throws Exception |
| 69 | + */ |
| 70 | + public function toDateTime(): DateTimeImmutable |
| 71 | + { |
| 72 | + return (new DateTimeImmutable(sprintf('@%s', $this->getSeconds()))) |
| 73 | + ->modify(sprintf('+%s microseconds', $this->nanoseconds / 1000)) |
| 74 | + ->setTimezone(new DateTimeZone($this->tzId)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return array{seconds: int, nanoseconds: int, tzId: string} |
| 79 | + */ |
| 80 | + public function toArray(): array |
| 81 | + { |
| 82 | + return [ |
| 83 | + 'seconds' => $this->seconds, |
| 84 | + 'nanoseconds' => $this->nanoseconds, |
| 85 | + 'tzId' => $this->tzId, |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return CypherMap<string|int> |
| 91 | + */ |
| 92 | + public function getProperties(): CypherMap |
| 93 | + { |
| 94 | + return new CypherMap($this); |
| 95 | + } |
| 96 | + |
| 97 | + public function convertToBolt(): IStructure |
| 98 | + { |
| 99 | + return new \Bolt\structures\DateTimeZoneId($this->getSeconds(), $this->getNanoseconds(), $this->getTimezoneIdentifier()); |
| 100 | + } |
| 101 | +} |
0 commit comments