From 1db9c9dfd041d27811ad70d20999cdc6171a6e6e Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 14 Mar 2026 18:49:54 +0100 Subject: [PATCH] Add toArray() method to Chronos, ChronosDate, and ChronosTime Adds a toArray() method to return date/time components as an associative array. This is the inverse of the existing createFromArray() method. - Chronos::toArray() returns year, month, day, hour, minute, second, microsecond, and timezone - ChronosDate::toArray() returns year, month, and day - ChronosTime::toArray() returns hour, minute, second, and microsecond --- src/Chronos.php | 19 +++++++++++++++++++ src/ChronosDate.php | 14 ++++++++++++++ src/ChronosTime.php | 15 +++++++++++++++ tests/TestCase/ChronosTimeTest.php | 12 ++++++++++++ tests/TestCase/Date/GettersTest.php | 11 +++++++++++ tests/TestCase/DateTime/GettersTest.php | 15 +++++++++++++++ 6 files changed, 86 insertions(+) diff --git a/src/Chronos.php b/src/Chronos.php index eadcbb1..c27966e 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -2642,6 +2642,25 @@ public function toNative(): DateTimeImmutable return new DateTimeImmutable($this->format('Y-m-d H:i:s.u'), $this->getTimezone()); } + /** + * Returns the date and time as an associative array. + * + * @return array{year: int, month: int, day: int, hour: int, minute: int, second: int, microsecond: int, timezone: string} + */ + public function toArray(): array + { + return [ + 'year' => $this->year, + 'month' => $this->month, + 'day' => $this->day, + 'hour' => $this->hour, + 'minute' => $this->minute, + 'second' => $this->second, + 'microsecond' => $this->microsecond, + 'timezone' => $this->timezone->getName(), + ]; + } + /** * Get a part of the object * diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 85bad79..f5b5ad0 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -1608,6 +1608,20 @@ public function toNative(DateTimeZone|string|null $timezone = null): DateTimeImm return $this->toDateTimeImmutable($timezone); } + /** + * Returns the date as an associative array. + * + * @return array{year: int, month: int, day: int} + */ + public function toArray(): array + { + return [ + 'year' => $this->year, + 'month' => $this->month, + 'day' => $this->day, + ]; + } + /** * Get a part of the object * diff --git a/src/ChronosTime.php b/src/ChronosTime.php index 7d65dd2..6f0d5cc 100644 --- a/src/ChronosTime.php +++ b/src/ChronosTime.php @@ -490,4 +490,19 @@ public function toNative(DateTimeZone|string|null $timezone = null): DateTimeImm { return $this->toDateTimeImmutable($timezone); } + + /** + * Returns the time as an associative array. + * + * @return array{hour: int, minute: int, second: int, microsecond: int} + */ + public function toArray(): array + { + return [ + 'hour' => $this->getHours(), + 'minute' => $this->getMinutes(), + 'second' => $this->getSeconds(), + 'microsecond' => $this->getMicroseconds(), + ]; + } } diff --git a/tests/TestCase/ChronosTimeTest.php b/tests/TestCase/ChronosTimeTest.php index 92b6786..8959983 100644 --- a/tests/TestCase/ChronosTimeTest.php +++ b/tests/TestCase/ChronosTimeTest.php @@ -298,4 +298,16 @@ public function testToString(): void ChronosTime::resetToStringFormat(); $this->assertSame('12:13:14', (string)$t); } + + public function testToArray(): void + { + $t = new ChronosTime('12:30:45.123456'); + $array = $t->toArray(); + + $this->assertSame(12, $array['hour']); + $this->assertSame(30, $array['minute']); + $this->assertSame(45, $array['second']); + $this->assertSame(123456, $array['microsecond']); + $this->assertCount(4, $array); + } } diff --git a/tests/TestCase/Date/GettersTest.php b/tests/TestCase/Date/GettersTest.php index 409a0bc..4569085 100644 --- a/tests/TestCase/Date/GettersTest.php +++ b/tests/TestCase/Date/GettersTest.php @@ -26,4 +26,15 @@ public function testHalfOfYear(int $month, int $expectedHalfOfYear): void $d = ChronosDate::create(year: 2012, month: $month, day: 1); $this->assertSame($expectedHalfOfYear, $d->half); } + + public function testToArray(): void + { + $d = ChronosDate::create(2024, 1, 15); + $array = $d->toArray(); + + $this->assertSame(2024, $array['year']); + $this->assertSame(1, $array['month']); + $this->assertSame(15, $array['day']); + $this->assertCount(3, $array); + } } diff --git a/tests/TestCase/DateTime/GettersTest.php b/tests/TestCase/DateTime/GettersTest.php index dddbcd4..54a65aa 100644 --- a/tests/TestCase/DateTime/GettersTest.php +++ b/tests/TestCase/DateTime/GettersTest.php @@ -333,4 +333,19 @@ public function testInvalidGetter() $d = Chronos::now(); $d->doesNotExit; } + + public function testToArray(): void + { + $d = Chronos::create(2024, 1, 15, 12, 30, 45, 123456, 'America/Toronto'); + $array = $d->toArray(); + + $this->assertSame(2024, $array['year']); + $this->assertSame(1, $array['month']); + $this->assertSame(15, $array['day']); + $this->assertSame(12, $array['hour']); + $this->assertSame(30, $array['minute']); + $this->assertSame(45, $array['second']); + $this->assertSame(123456, $array['microsecond']); + $this->assertSame('America/Toronto', $array['timezone']); + } }