Skip to content

Commit 01c9515

Browse files
authored
Add toArray() method to Chronos, ChronosDate, and ChronosTime (#506)
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
1 parent 13b70d1 commit 01c9515

6 files changed

Lines changed: 86 additions & 0 deletions

File tree

src/Chronos.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,6 +2661,25 @@ public function toNative(): DateTimeImmutable
26612661
return new DateTimeImmutable($this->format('Y-m-d H:i:s.u'), $this->getTimezone());
26622662
}
26632663

2664+
/**
2665+
* Returns the date and time as an associative array.
2666+
*
2667+
* @return array{year: int, month: int, day: int, hour: int, minute: int, second: int, microsecond: int, timezone: string}
2668+
*/
2669+
public function toArray(): array
2670+
{
2671+
return [
2672+
'year' => $this->year,
2673+
'month' => $this->month,
2674+
'day' => $this->day,
2675+
'hour' => $this->hour,
2676+
'minute' => $this->minute,
2677+
'second' => $this->second,
2678+
'microsecond' => $this->microsecond,
2679+
'timezone' => $this->timezone->getName(),
2680+
];
2681+
}
2682+
26642683
/**
26652684
* Get a part of the object
26662685
*

src/ChronosDate.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,20 @@ public function toNative(DateTimeZone|string|null $timezone = null): DateTimeImm
16081608
return $this->toDateTimeImmutable($timezone);
16091609
}
16101610

1611+
/**
1612+
* Returns the date as an associative array.
1613+
*
1614+
* @return array{year: int, month: int, day: int}
1615+
*/
1616+
public function toArray(): array
1617+
{
1618+
return [
1619+
'year' => $this->year,
1620+
'month' => $this->month,
1621+
'day' => $this->day,
1622+
];
1623+
}
1624+
16111625
/**
16121626
* Get a part of the object
16131627
*

src/ChronosTime.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,19 @@ public function toNative(DateTimeZone|string|null $timezone = null): DateTimeImm
521521
{
522522
return $this->toDateTimeImmutable($timezone);
523523
}
524+
525+
/**
526+
* Returns the time as an associative array.
527+
*
528+
* @return array{hour: int, minute: int, second: int, microsecond: int}
529+
*/
530+
public function toArray(): array
531+
{
532+
return [
533+
'hour' => $this->getHours(),
534+
'minute' => $this->getMinutes(),
535+
'second' => $this->getSeconds(),
536+
'microsecond' => $this->getMicroseconds(),
537+
];
538+
}
524539
}

tests/TestCase/ChronosTimeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,16 @@ public function testToString(): void
328328
ChronosTime::resetToStringFormat();
329329
$this->assertSame('12:13:14', (string)$t);
330330
}
331+
332+
public function testToArray(): void
333+
{
334+
$t = new ChronosTime('12:30:45.123456');
335+
$array = $t->toArray();
336+
337+
$this->assertSame(12, $array['hour']);
338+
$this->assertSame(30, $array['minute']);
339+
$this->assertSame(45, $array['second']);
340+
$this->assertSame(123456, $array['microsecond']);
341+
$this->assertCount(4, $array);
342+
}
331343
}

tests/TestCase/Date/GettersTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ public function testHalfOfYear(int $month, int $expectedHalfOfYear): void
2626
$d = ChronosDate::create(year: 2012, month: $month, day: 1);
2727
$this->assertSame($expectedHalfOfYear, $d->half);
2828
}
29+
30+
public function testToArray(): void
31+
{
32+
$d = ChronosDate::create(2024, 1, 15);
33+
$array = $d->toArray();
34+
35+
$this->assertSame(2024, $array['year']);
36+
$this->assertSame(1, $array['month']);
37+
$this->assertSame(15, $array['day']);
38+
$this->assertCount(3, $array);
39+
}
2940
}

tests/TestCase/DateTime/GettersTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,19 @@ public function testInvalidGetter()
333333
$d = Chronos::now();
334334
$d->doesNotExit;
335335
}
336+
337+
public function testToArray(): void
338+
{
339+
$d = Chronos::create(2024, 1, 15, 12, 30, 45, 123456, 'America/Toronto');
340+
$array = $d->toArray();
341+
342+
$this->assertSame(2024, $array['year']);
343+
$this->assertSame(1, $array['month']);
344+
$this->assertSame(15, $array['day']);
345+
$this->assertSame(12, $array['hour']);
346+
$this->assertSame(30, $array['minute']);
347+
$this->assertSame(45, $array['second']);
348+
$this->assertSame(123456, $array['microsecond']);
349+
$this->assertSame('America/Toronto', $array['timezone']);
350+
}
336351
}

0 commit comments

Comments
 (0)