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 3, 2023
1 parent 0c94b40 commit 87a773a
Show file tree
Hide file tree
Showing 28 changed files with 482 additions and 84 deletions.
16 changes: 12 additions & 4 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,15 +754,23 @@ public function toNanosPart(): int
}

/**
* Serializes as a string using {@see Duration::__toString()}.
* Serializes as a string using {@see Duration::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* Returns an ISO-8601 string representation of this duration.
* {@see Duration::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this duration.
*
* The format of the returned string will be PTnHnMn.nS, where n is
* the relevant hours, minutes, seconds or nanoseconds part of the duration.
Expand All @@ -772,7 +780,7 @@ public function jsonSerialize(): string
*
* Note that multiples of 24 hours are not output as days to avoid confusion with Period.
*/
public function __toString(): string
public function toISOString(): string
{
$seconds = $this->seconds;
$nanos = $this->nanos;
Expand Down
15 changes: 13 additions & 2 deletions src/Instant.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,25 @@ public function toDecimal(): string
}

/**
* Serializes as a string using {@see Instant::__toString()}.
* Serializes as a string using {@see Instant::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

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

/**
* Returns the ISO 8601 representation of this instant.
*/
public function toISOString(): string
{
return (string) ZonedDateTime::ofInstant($this, TimeZone::utc());
}
Expand Down
15 changes: 13 additions & 2 deletions src/Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,25 @@ public function isEqualTo(Interval $that): bool
}

/**
* Serializes as a string using {@see Interval::__toString()}.
* Serializes as a string using {@see Interval::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

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

/**
* Returns the ISO 8601 representation of this interval.
*/
public function toISOString(): string
{
return $this->start . '/' . $this->end;
}
Expand Down
14 changes: 11 additions & 3 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,17 +747,25 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable
}

/**
* Serializes as a string using {@see LocalDate::__toString()}.
* Serializes as a string using {@see LocalDate::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this LocalDate.
* {@see LocalDate::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this date.
*/
public function toISOString(): string
{
$pattern = ($this->year < 0 ? '%05d' : '%04d') . '-%02d-%02d';

Expand Down
14 changes: 11 additions & 3 deletions src/LocalDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ public function count(): int
}

/**
* Serializes as a string using {@see LocalDateRange::__toString()}.
* Serializes as a string using {@see LocalDateRange::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
Expand All @@ -260,9 +260,17 @@ public function toNativeDatePeriod(): DatePeriod
}

/**
* Returns an ISO 8601 string representation of this date range.
* {@see LocalDateRange::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this date range.
*/
public function toISOString(): string
{
return $this->start . '/' . $this->end;
}
Expand Down
15 changes: 13 additions & 2 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,14 +716,25 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable
}

/**
* Serializes as a string using {@see LocalDateTime::__toString()}.
* Serializes as a string using {@see LocalDateTime::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* {@see LocalDateTime::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this date time.
*/
public function toISOString(): string
{
return $this->date . 'T' . $this->time;
}
Expand Down
18 changes: 13 additions & 5 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,17 +640,25 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable
}

/**
* Serializes as a string using {@see LocalTime::__toString()}.
* Serializes as a string using {@see LocalTime::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* Returns this time as a string, such as 10:15.
* {@see LocalTime::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this time.
*
* The output will be one of the following ISO-8601 formats:
* The output will be one of the following formats:
*
* * `HH:mm`
* * `HH:mm:ss`
Expand All @@ -660,7 +668,7 @@ public function jsonSerialize(): string
* the time where the omitted parts are implied to be zero.
* The nanoseconds value, if present, can be 0 to 9 digits.
*/
public function __toString(): string
public function toISOString(): string
{
if ($this->nano === 0) {
if ($this->second === 0) {
Expand Down
15 changes: 13 additions & 2 deletions src/MonthDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,25 @@ public function atYear(int $year): LocalDate
}

/**
* Serializes as a string using {@see MonthDay::__toString()}.
* Serializes as a string using {@see MonthDay::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* {@see MonthDay::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this month-day.
*/
public function toISOString(): string
{
return sprintf('--%02d-%02d', $this->month, $this->day);
}
Expand Down
15 changes: 13 additions & 2 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,25 @@ public function toNativeDateInterval(): DateInterval
}

/**
* Serializes as a string using {@see Period::__toString()}.
* Serializes as a string using {@see Period::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* {@see Period::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this period.
*/
public function toISOString(): string
{
if ($this->isZero()) {
return 'P0D';
Expand Down
15 changes: 13 additions & 2 deletions src/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,25 @@ public function toLocalDateRange(): LocalDateRange
}

/**
* Serializes as a string using {@see Year::__toString()}.
* Serializes as a string using {@see Year::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* {@see Year::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this year.
*/
public function toISOString(): string
{
return (string) $this->year;
}
Expand Down
14 changes: 11 additions & 3 deletions src/YearMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,17 +288,25 @@ public function toLocalDateRange(): LocalDateRange
}

/**
* Serializes as a string using {@see YearMonth::__toString()}.
* Serializes as a string using {@see YearMonth::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this YearMonth.
* {@see YearMonth::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this year-month.
*/
public function toISOString(): string
{
$pattern = ($this->year < 0 ? '%05d' : '%04d') . '-%02d';

Expand Down
14 changes: 11 additions & 3 deletions src/YearMonthRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,19 @@ public function toLocalDateRange(): LocalDateRange
}

/**
* Serializes as a string using {@see YearMonthRange::__toString()}.
* Serializes as a string using {@see YearMonthRange::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* {@see YearMonthRange::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
Expand All @@ -193,7 +201,7 @@ public function jsonSerialize(): string
* ISO 8601 does not seem to provide a standard notation for year-month ranges, but we're using the same format as
* date ranges.
*/
public function __toString(): string
public function toISOString(): string
{
return $this->start . '/' . $this->end;
}
Expand Down
15 changes: 13 additions & 2 deletions src/YearWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,25 @@ public function toLocalDateRange(): LocalDateRange
}

/**
* Serializes as a string using {@see YearWeek::__toString()}.
* Serializes as a string using {@see YearWeek::toISOString()}.
*/
public function jsonSerialize(): string
{
return (string) $this;
return $this->toISOString();
}

/**
* {@see YearWeek::toISOString()}.
*/
public function __toString(): string
{
return $this->toISOString();
}

/**
* Returns the ISO 8601 representation of this year-week.
*/
public function toISOString(): string
{
$pattern = ($this->year < 0 ? '%05d' : '%04d') . '-W%02d';

Expand Down
Loading

0 comments on commit 87a773a

Please sign in to comment.