Skip to content

Commit

Permalink
Add toISO() methods (same as __toString()).
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Sep 30, 2023
1 parent 835865f commit d47b51a
Show file tree
Hide file tree
Showing 30 changed files with 401 additions and 43 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 toISO(): 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 toISO(): 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 toISO(): 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 toISO(): 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 @@ -757,6 +757,14 @@ public function jsonSerialize(): string
return (string) $this;
}

/**
* {@see __toString()}.
*/
public function toISO(): 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 toISO(): 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 toISO(): 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 @@ -648,6 +648,14 @@ public function jsonSerialize(): string
return (string) $this;
}

/**
* {@see __toString()}.
*/
public function toISO(): 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 @@ -231,6 +231,14 @@ public function jsonSerialize(): string
return (string) $this;
}

/**
* {@see __toString()}.
*/
public function toISO(): string
{
return (string) $this;
}

public function __toString(): string
{
// This code is optimized for high performance
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 toISO(): 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 toISO(): 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 @@ -298,6 +298,14 @@ public function jsonSerialize(): string
return (string) $this;
}

/**
* {@see __toString()}.
*/
public function toISO(): 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 toISO(): 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 @@ -272,6 +272,14 @@ public function jsonSerialize(): string
return (string) $this;
}

/**
* {@see __toString()}.
*/
public function toISO(): string
{
return (string) $this;
}

public function __toString(): string
{
// This code is optimized for high performance
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 toISO(): 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 testToISO(int $dayOfWeek, string $expectedName): void
{
self::assertSame($expectedName, DayOfWeek::of($dayOfWeek)->toISO());
}

/**
* @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 testToISO(int $seconds, int $nanos, string $expected): void
{
self::assertSame($expected, Duration::ofSeconds($seconds, $nanos)->toISO());
}

/**
* @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 testToISO(int $epochSecond, int $nano, string $expectedString): void
{
self::assertSame($expectedString, Instant::of($epochSecond, $nano)->toISO());
}

/**
* @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 testToISO(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->toISO());
}

/** @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 d47b51a

Please sign in to comment.