Skip to content

Commit

Permalink
Add toISOString() methods (same as __toString()).
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Oct 2, 2023
1 parent 1640dac commit 8109088
Show file tree
Hide file tree
Showing 28 changed files with 420 additions and 47 deletions.
8 changes: 8 additions & 0 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Instant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 8 additions & 0 deletions src/Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/LocalDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/MonthDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
8 changes: 8 additions & 0 deletions src/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/YearMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/YearMonthRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/YearWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 8 additions & 0 deletions src/ZonedDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tests/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/InstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
37 changes: 29 additions & 8 deletions tests/IntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}
68 changes: 57 additions & 11 deletions tests/LocalDateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/LocalDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Loading

0 comments on commit 8109088

Please sign in to comment.