diff --git a/src/DayOfWeek.php b/src/DayOfWeek.php index afbd2f8..ca172b7 100644 --- a/src/DayOfWeek.php +++ b/src/DayOfWeek.php @@ -213,6 +213,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + /** * Returns the capitalized English name of this day-of-week. */ diff --git a/src/Duration.php b/src/Duration.php index 9a2d03a..86d0649 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -761,6 +761,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + /** * Returns an ISO-8601 string representation of this duration. * diff --git a/src/Instant.php b/src/Instant.php index 49238bd..13f3f5a 100644 --- a/src/Instant.php +++ b/src/Instant.php @@ -373,6 +373,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + public function __toString(): string { return (string) ZonedDateTime::ofInstant($this, TimeZone::utc()); diff --git a/src/Interval.php b/src/Interval.php index 2f566f9..611a349 100644 --- a/src/Interval.php +++ b/src/Interval.php @@ -145,6 +145,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + public function __toString(): string { return $this->start . '/' . $this->end; diff --git a/src/LocalDate.php b/src/LocalDate.php index f0939c1..26b2cd1 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -754,6 +754,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + /** * Returns the ISO 8601 representation of this LocalDate. */ diff --git a/src/LocalDateRange.php b/src/LocalDateRange.php index 58ca387..ef1aac1 100644 --- a/src/LocalDateRange.php +++ b/src/LocalDateRange.php @@ -259,6 +259,14 @@ public function toNativeDatePeriod(): DatePeriod return new DatePeriod($start, $interval, $end); } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + /** * Returns an ISO 8601 string representation of this date range. */ diff --git a/src/LocalDateTime.php b/src/LocalDateTime.php index 5c2387e..024134c 100644 --- a/src/LocalDateTime.php +++ b/src/LocalDateTime.php @@ -723,6 +723,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + public function __toString(): string { return $this->date . 'T' . $this->time; diff --git a/src/LocalTime.php b/src/LocalTime.php index d1376d4..523f89d 100644 --- a/src/LocalTime.php +++ b/src/LocalTime.php @@ -647,6 +647,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + /** * Returns this time as a string, such as 10:15. * diff --git a/src/MonthDay.php b/src/MonthDay.php index 7b1ddb4..b067619 100644 --- a/src/MonthDay.php +++ b/src/MonthDay.php @@ -233,6 +233,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + public function __toString(): string { return sprintf('--%02d-%02d', $this->month, $this->day); diff --git a/src/Period.php b/src/Period.php index aee2a22..d6fac1f 100644 --- a/src/Period.php +++ b/src/Period.php @@ -384,6 +384,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + public function __toString(): string { if ($this->isZero()) { diff --git a/src/Year.php b/src/Year.php index 6caa41d..1b7d9bc 100644 --- a/src/Year.php +++ b/src/Year.php @@ -243,6 +243,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + public function __toString(): string { return (string) $this->year; diff --git a/src/YearMonth.php b/src/YearMonth.php index 032fbd4..19f7597 100644 --- a/src/YearMonth.php +++ b/src/YearMonth.php @@ -295,6 +295,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + /** * Returns the ISO 8601 representation of this YearMonth. */ diff --git a/src/YearMonthRange.php b/src/YearMonthRange.php index d7a6f6b..318bcc0 100644 --- a/src/YearMonthRange.php +++ b/src/YearMonthRange.php @@ -187,6 +187,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + /** * Returns a string representation of this year-month range. * diff --git a/src/YearWeek.php b/src/YearWeek.php index 8a410cc..0f88438 100644 --- a/src/YearWeek.php +++ b/src/YearWeek.php @@ -269,6 +269,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + public function __toString(): string { $pattern = ($this->year < 0 ? '%05d' : '%04d') . '-W%02d'; diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index 2ced2b0..1259e1b 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -717,6 +717,14 @@ public function jsonSerialize(): string return (string) $this; } + /** + * {@see __toString()}. + */ + public function toISOString(): string + { + return (string) $this; + } + public function __toString(): string { $string = $this->localDateTime . $this->timeZoneOffset; diff --git a/tests/DayOfWeekTest.php b/tests/DayOfWeekTest.php index 1d0650c..6e19b2d 100644 --- a/tests/DayOfWeekTest.php +++ b/tests/DayOfWeekTest.php @@ -286,6 +286,17 @@ public function testJsonSerialize(int $dayOfWeek, string $expectedName): void self::assertSame(json_encode($expectedName), json_encode(DayOfWeek::of($dayOfWeek))); } + /** + * @dataProvider providerToString + * + * @param int $dayOfWeek The day-of-week value, from 1 to 7. + * @param string $expectedName The expected name. + */ + public function testToISOString(int $dayOfWeek, string $expectedName): void + { + self::assertSame($expectedName, DayOfWeek::of($dayOfWeek)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/DurationTest.php b/tests/DurationTest.php index 8fe4611..fa1347b 100644 --- a/tests/DurationTest.php +++ b/tests/DurationTest.php @@ -1282,6 +1282,14 @@ public function testJsonSerialize(int $seconds, int $nanos, string $expected): v self::assertSame(json_encode($expected), json_encode(Duration::ofSeconds($seconds, $nanos))); } + /** + * @dataProvider providerToString + */ + public function testToISOString(int $seconds, int $nanos, string $expected): void + { + self::assertSame($expected, Duration::ofSeconds($seconds, $nanos)->toISOString()); + } + /** * @dataProvider providerToString */ diff --git a/tests/InstantTest.php b/tests/InstantTest.php index 5357957..4ce072b 100644 --- a/tests/InstantTest.php +++ b/tests/InstantTest.php @@ -645,6 +645,18 @@ public function testJsonSerialize(int $epochSecond, int $nano, string $expectedS self::assertSame(json_encode($expectedString), json_encode(Instant::of($epochSecond, $nano))); } + /** + * @dataProvider providerToString + * + * @param int $epochSecond The epoch second to test. + * @param int $nano The nano adjustment to the epoch second. + * @param string $expectedString The expected string output. + */ + public function testToISOString(int $epochSecond, int $nano, string $expectedString): void + { + self::assertSame($expectedString, Instant::of($epochSecond, $nano)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/IntervalTest.php b/tests/IntervalTest.php index 01a5733..2dcbc53 100644 --- a/tests/IntervalTest.php +++ b/tests/IntervalTest.php @@ -260,23 +260,44 @@ public function providerIsEqualTo(): array ]; } - public function testJsonSerialize(): void + /** @dataProvider providerToString */ + public function testJsonSerialize(int $epochSecondStart, int $epochSecondEnd, string $expectedString): void { $interval = Interval::of( - Instant::of(1000000000), - Instant::of(2000000000) + Instant::of($epochSecondStart), + Instant::of($epochSecondEnd) ); - self::assertSame(json_encode('2001-09-09T01:46:40Z/2033-05-18T03:33:20Z'), json_encode($interval)); + self::assertSame(json_encode($expectedString), json_encode($interval)); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToISOString(int $epochSecondStart, int $epochSecondEnd, string $expectedString): void { $interval = Interval::of( - Instant::of(1000000000), - Instant::of(2000000000) + Instant::of($epochSecondStart), + Instant::of($epochSecondEnd) ); - self::assertSame('2001-09-09T01:46:40Z/2033-05-18T03:33:20Z', (string) $interval); + self::assertSame($expectedString, $interval->toISOString()); + } + + /** @dataProvider providerToString */ + public function testToString(int $epochSecondStart, int $epochSecondEnd, string $expectedString): void + { + $interval = Interval::of( + Instant::of($epochSecondStart), + Instant::of($epochSecondEnd) + ); + + self::assertSame($expectedString, (string) $interval); + } + + public function providerToString(): array + { + return [ + [1000000000, 1000000000, '2001-09-09T01:46:40Z/2001-09-09T01:46:40Z'], + [1000000000, 2000000000, '2001-09-09T01:46:40Z/2033-05-18T03:33:20Z'], + ]; } } diff --git a/tests/LocalDateRangeTest.php b/tests/LocalDateRangeTest.php index 0bb6421..aa77ba1 100644 --- a/tests/LocalDateRangeTest.php +++ b/tests/LocalDateRangeTest.php @@ -189,20 +189,66 @@ public function providerCount(): array ]; } - public function testJsonSerialize(): void - { - self::assertSame(json_encode('2008-12-31/2011-01-01'), json_encode(LocalDateRange::of( - LocalDate::of(2008, 12, 31), - LocalDate::of(2011, 1, 1) - ))); + /** @dataProvider providerToString */ + public function testJsonSerialize( + int $yearStart, + int $monthStart, + int $dayStart, + int $yearEnd, + int $monthEnd, + int $dayEnd, + string $expectedString + ): void { + $dateRange = LocalDateRange::of( + LocalDate::of($yearStart, $monthStart, $dayStart), + LocalDate::of($yearEnd, $monthEnd, $dayEnd) + ); + + self::assertSame(json_encode($expectedString), json_encode($dateRange)); + } + + /** @dataProvider providerToString */ + public function testToISOString( + int $yearStart, + int $monthStart, + int $dayStart, + int $yearEnd, + int $monthEnd, + int $dayEnd, + string $expectedString + ): void { + $dateRange = LocalDateRange::of( + LocalDate::of($yearStart, $monthStart, $dayStart), + LocalDate::of($yearEnd, $monthEnd, $dayEnd) + ); + + self::assertSame($expectedString, $dateRange->toISOString()); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToString( + int $yearStart, + int $monthStart, + int $dayStart, + int $yearEnd, + int $monthEnd, + int $dayEnd, + string $expectedString + ): void { + $dateRange = LocalDateRange::of( + LocalDate::of($yearStart, $monthStart, $dayStart), + LocalDate::of($yearEnd, $monthEnd, $dayEnd) + ); + + self::assertSame($expectedString, (string) $dateRange); + } + + public function providerToString(): array { - self::assertSame('2008-12-31/2011-01-01', (string) LocalDateRange::of( - LocalDate::of(2008, 12, 31), - LocalDate::of(2011, 1, 1) - )); + return [ + [2008, 12, 31, 2008, 12, 31, '2008-12-31/2008-12-31'], + [2008, 12, 31, 2011, 1, 1, '2008-12-31/2011-01-01'], + ]; } /** diff --git a/tests/LocalDateTest.php b/tests/LocalDateTest.php index f46db60..190c9fc 100644 --- a/tests/LocalDateTest.php +++ b/tests/LocalDateTest.php @@ -1373,6 +1373,19 @@ public function testJsonSerialize(int $year, int $month, int $day, string $expec self::assertSame(json_encode($expected), json_encode(LocalDate::of($year, $month, $day))); } + /** + * @dataProvider providerToString + * + * @param int $year The year. + * @param int $month The month. + * @param int $day The day-of-month. + * @param string $expected The expected result string. + */ + public function testToISOString(int $year, int $month, int $day, string $expected): void + { + self::assertSame($expected, LocalDate::of($year, $month, $day)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/LocalDateTimeTest.php b/tests/LocalDateTimeTest.php index 3cd94d6..b176f8a 100644 --- a/tests/LocalDateTimeTest.php +++ b/tests/LocalDateTimeTest.php @@ -1220,6 +1220,23 @@ public function testJsonSerialize(int $year, int $month, int $day, int $hour, in self::assertSame(json_encode($expected), json_encode(LocalDateTime::of($year, $month, $day, $hour, $minute, $second, $nano))); } + /** + * @dataProvider providerToString + * + * @param int $year The year. + * @param int $month The month. + * @param int $day The day-of-month. + * @param int $hour The hour-of-day. + * @param int $minute The minute-of-hour. + * @param int $second The second-of-minute. + * @param int $nano The nano-of-second. + * @param string $expected The expected result string. + */ + public function testToISOString(int $year, int $month, int $day, int $hour, int $minute, int $second, int $nano, string $expected): void + { + self::assertSame($expected, LocalDateTime::of($year, $month, $day, $hour, $minute, $second, $nano)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/LocalTimeTest.php b/tests/LocalTimeTest.php index dd28983..c0ad9e7 100644 --- a/tests/LocalTimeTest.php +++ b/tests/LocalTimeTest.php @@ -1045,6 +1045,20 @@ public function testJsonSerialize(int $h, int $m, int $s, int $n, string $r): vo self::assertSame(json_encode($r), json_encode(LocalTime::of($h, $m, $s, $n))); } + /** + * @dataProvider providerToString + * + * @param int $h The hour. + * @param int $m The minute. + * @param int $s The second. + * @param int $n The nanosecond. + * @param string $r The expected result. + */ + public function testToISOString(int $h, int $m, int $s, int $n, string $r): void + { + self::assertSame($r, LocalTime::of($h, $m, $s, $n)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/MonthDayTest.php b/tests/MonthDayTest.php index d54fc14..54b4324 100644 --- a/tests/MonthDayTest.php +++ b/tests/MonthDayTest.php @@ -476,6 +476,18 @@ public function testJsonSerialize(int $month, int $day, string $string): void self::assertSame(json_encode($string), json_encode(MonthDay::of($month, $day))); } + /** + * @dataProvider providerToString + * + * @param int $month The month of the month-day to test. + * @param int $day The day of the month-day to test. + * @param string $string The expected result string. + */ + public function testToISOString(int $month, int $day, string $string): void + { + self::assertSame($string, MonthDay::of($month, $day)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/PeriodTest.php b/tests/PeriodTest.php index b43b1c1..1c6a1a8 100644 --- a/tests/PeriodTest.php +++ b/tests/PeriodTest.php @@ -409,6 +409,19 @@ public function testJsonSerialize(int $years, int $months, int $days, string $ex self::assertSame(json_encode($expected), json_encode(Period::of($years, $months, $days))); } + /** + * @dataProvider providerToString + * + * @param int $years The number of years in the period. + * @param int $months The number of months in the period. + * @param int $days The number of days in the period. + * @param string $expected The expected string output. + */ + public function testToISOString(int $years, int $months, int $days, string $expected): void + { + self::assertSame($expected, Period::of($years, $months, $days)->toISOString()); + } + /** * @dataProvider providerToString * diff --git a/tests/YearMonthRangeTest.php b/tests/YearMonthRangeTest.php index f451346..309252a 100644 --- a/tests/YearMonthRangeTest.php +++ b/tests/YearMonthRangeTest.php @@ -197,19 +197,44 @@ public function providerToLocalDateRange(): array ]; } - public function testJsonSerialize(): void + /** @dataProvider providerToString */ + public function testJsonSerialize(int $yearStart, int $monthStart, int $yearEnd, int $monthEnd, string $expectedString): void { - self::assertSame(json_encode('2008-12/2011-01'), json_encode(YearMonthRange::of( - YearMonth::of(2008, 12), - YearMonth::of(2011, 1) - ))); + $yearMonthRange = YearMonthRange::of( + YearMonth::of($yearStart, $monthStart), + YearMonth::of($yearEnd, $monthEnd) + ); + + self::assertSame(json_encode($expectedString), json_encode($yearMonthRange)); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToISOString(int $yearStart, int $monthStart, int $yearEnd, int $monthEnd, string $expectedString): void { - self::assertSame('2008-12/2011-01', (string) YearMonthRange::of( - YearMonth::of(2008, 12), - YearMonth::of(2011, 1) - )); + $yearMonthRange = YearMonthRange::of( + YearMonth::of($yearStart, $monthStart), + YearMonth::of($yearEnd, $monthEnd) + ); + + self::assertSame($expectedString, $yearMonthRange->toISOString()); + } + + /** @dataProvider providerToString */ + public function testToString(int $yearStart, int $monthStart, int $yearEnd, int $monthEnd, string $expectedString): void + { + $yearMonthRange = YearMonthRange::of( + YearMonth::of($yearStart, $monthStart), + YearMonth::of($yearEnd, $monthEnd) + ); + + self::assertSame($expectedString, (string) $yearMonthRange); + } + + public function providerToString(): array + { + return [ + [2008, 12, 2008, 12, '2008-12/2008-12'], + [2008, 12, 2011, 1, '2008-12/2011-01'], + ]; } } diff --git a/tests/YearMonthTest.php b/tests/YearMonthTest.php index d758741..1464211 100644 --- a/tests/YearMonthTest.php +++ b/tests/YearMonthTest.php @@ -459,13 +459,53 @@ public function providerToLocalDateRange(): array ]; } - public function testJsonSerialize(): void + /** + * @dataProvider providerToString + */ + public function testJsonSerialize(int $year, int $week, string $expectedString): void { - self::assertSame(json_encode('2013-09'), json_encode(YearMonth::of(2013, 9))); + self::assertSame(json_encode($expectedString), json_encode(YearMonth::of($year, $week))); } - public function testToString(): void + /** + * @dataProvider providerToString + */ + public function testToISOString(int $year, int $month, string $expectedString): void { - self::assertSame('2013-09', (string) YearMonth::of(2013, 9)); + self::assertSame($expectedString, YearMonth::of($year, $month)->toISOString()); + } + + /** + * @dataProvider providerToString + * + * @param int $year The year. + * @param int $month The month. + * @param string $expected The expected result string. + */ + public function testToString(int $year, int $month, string $expected): void + { + self::assertSame($expected, (string) YearMonth::of($year, $month)); + } + + public function providerToString(): array + { + return [ + [-999999, 12, '-999999-12'], + [-185321, 11, '-185321-11'], + [-18532, 11, '-18532-11'], + [-2023, 11, '-2023-11'], + [-2023, 11, '-2023-11'], + [-2023, 1, '-2023-01'], + [-999, 1, '-0999-01'], + [-2, 1, '-0002-01'], + [2, 1, '0002-01'], + [999, 1, '0999-01'], + [2023, 1, '2023-01'], + [2023, 11, '2023-11'], + [2023, 11, '2023-11'], + [18532, 11, '18532-11'], + [185321, 11, '185321-11'], + [999999, 12, '999999-12'], + ]; } } diff --git a/tests/YearTest.php b/tests/YearTest.php index b5dbb01..64bc94d 100644 --- a/tests/YearTest.php +++ b/tests/YearTest.php @@ -455,13 +455,30 @@ public function providerToLocalDateRange(): array ]; } - public function testJsonSerialize(): void + /** @dataProvider providerToString */ + public function testJsonSerialize(int $year, string $expectedString): void { - self::assertSame(json_encode('1987'), json_encode(Year::of(1987))); + self::assertSame(json_encode($expectedString), json_encode(Year::of($year))); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToISOString(int $year, string $expectedString): void { - self::assertSame('1987', (string) Year::of(1987)); + self::assertSame($expectedString, Year::of($year)->toISOString()); + } + + /** @dataProvider providerToString */ + public function testToString(int $year, string $expectedString): void + { + self::assertSame($expectedString, (string) Year::of($year)); + } + + public function providerToString(): array + { + return [ + [-100, '-100'], + [1987, '1987'], + [105781, '105781'], + ]; } } diff --git a/tests/YearWeekTest.php b/tests/YearWeekTest.php index 682cea2..5cc9a8c 100644 --- a/tests/YearWeekTest.php +++ b/tests/YearWeekTest.php @@ -327,6 +327,15 @@ public function testJsonSerialize(int $year, int $week, string $expected): void self::assertSame(json_encode($expected), json_encode($yearWeek)); } + /** + * @dataProvider providerToString + */ + public function testToISOString(int $year, int $week, string $expected): void + { + $yearWeek = YearWeek::of($year, $week); + self::assertSame($expected, $yearWeek->toISOString()); + } + /** * @dataProvider providerToString */ diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 40fa510..7dee25f 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -814,24 +814,38 @@ public function providerToNativeDateTime(): array ]; } - public function testJsonSerialize(): void + /** @dataProvider providerToString */ + public function testJsonSerialize(string $localDateTime, string $timeZoneIso, string $expectedString): void { - $timeZone = TimeZone::parse('America/Los_Angeles'); - $localDateTime = '2000-01-20T12:34:56.123456789'; $localDateTime = LocalDateTime::parse($localDateTime); - $zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone); + $zonedDateTime = ZonedDateTime::of($localDateTime, TimeZone::parse($timeZoneIso)); - self::assertSame(json_encode('2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]'), json_encode($zonedDateTime)); + self::assertSame(json_encode($expectedString), json_encode($zonedDateTime)); } - public function testToString(): void + /** @dataProvider providerToString */ + public function testToISOString(string $localDateTime, string $timeZoneIso, string $expectedString): void { - $timeZone = TimeZone::parse('America/Los_Angeles'); - $localDateTime = '2000-01-20T12:34:56.123456789'; $localDateTime = LocalDateTime::parse($localDateTime); - $zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone); + $zonedDateTime = ZonedDateTime::of($localDateTime, TimeZone::parse($timeZoneIso)); + + self::assertSame($expectedString, $zonedDateTime->toISOString()); + } - self::assertSame('2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]', (string) $zonedDateTime); + /** @dataProvider providerToString */ + public function testToString(string $localDateTime, string $timeZoneIso, string $expectedString): void + { + $localDateTime = LocalDateTime::parse($localDateTime); + $zonedDateTime = ZonedDateTime::of($localDateTime, TimeZone::parse($timeZoneIso)); + + self::assertSame($expectedString, (string) $zonedDateTime); + } + + public function providerToString(): array + { + return [ + ['2000-01-20T12:34:56.123456789', 'America/Los_Angeles', '2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]'], + ]; } /**