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 7310295 commit 597381c
Show file tree
Hide file tree
Showing 30 changed files with 439 additions and 47 deletions.
8 changes: 8 additions & 0 deletions src/DayOfWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
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
11 changes: 11 additions & 0 deletions tests/DayOfWeekTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
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'],
];
}
}
Loading

0 comments on commit 597381c

Please sign in to comment.