diff --git a/psalm-baseline.xml b/psalm-baseline.xml index b6ed17d..6cce66c 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -148,14 +148,4 @@ ];]]> - - - format - mod - mod - mod - mod - mod - - diff --git a/src/Chronos.php b/src/Chronos.php index 3cc8bad..2c531b1 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -21,6 +21,7 @@ use DateTimeZone; use InvalidArgumentException; use RuntimeException; +use Stringable; /** * An Immutable extension on the native DateTime object. @@ -58,7 +59,7 @@ * @psalm-immutable * @psalm-consistent-constructor */ -class Chronos extends DateTimeImmutable +class Chronos extends DateTimeImmutable implements Stringable { use FormattingTrait; diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 2c0730d..b4d9cfe 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -20,6 +20,7 @@ use DateTimeInterface; use DateTimeZone; use InvalidArgumentException; +use Stringable; /** * An immutable date object. @@ -43,16 +44,23 @@ * @psalm-immutable * @psalm-consistent-constructor */ -class ChronosDate +class ChronosDate implements Stringable { use FormattingTrait; + /** + * Default format to use for __toString method when type juggling occurs. + * + * @var string + */ + public const DEFAULT_TO_STRING_FORMAT = 'Y-m-d'; + /** * Format to use for __toString method when type juggling occurs. * * @var string */ - protected static string $toStringFormat = 'Y-m-d'; + protected static string $toStringFormat = self::DEFAULT_TO_STRING_FORMAT; /** * Names of days of the week. diff --git a/src/ChronosTime.php b/src/ChronosTime.php index 0187096..df84d95 100644 --- a/src/ChronosTime.php +++ b/src/ChronosTime.php @@ -18,12 +18,12 @@ use DateTimeInterface; use DateTimeZone; use InvalidArgumentException; +use Stringable; /** - * @psalm-immutable * @psalm-consistent-constructor */ -class ChronosTime +class ChronosTime implements Stringable { /** * @var int @@ -50,6 +50,20 @@ class ChronosTime */ protected const TICKS_PER_DAY = self::TICKS_PER_HOUR * 24; + /** + * Default format to use for __toString method. + * + * @var string + */ + public const DEFAULT_TO_STRING_FORMAT = 'H:i:s'; + + /** + * Format to use for __toString method. + * + * @var string + */ + protected static string $toStringFormat = self::DEFAULT_TO_STRING_FORMAT; + /** * @var int */ @@ -323,6 +337,37 @@ public function format(string $format): string return $this->toDateTimeImmutable()->format($format); } + /** + * Reset the format used to the default when converting to a string + * + * @return void + */ + public static function resetToStringFormat(): void + { + static::setToStringFormat(static::DEFAULT_TO_STRING_FORMAT); + } + + /** + * Set the default format used when converting to a string + * + * @param string $format The format to use in future __toString() calls. + * @return void + */ + public static function setToStringFormat(string $format): void + { + static::$toStringFormat = $format; + } + + /** + * Format the instance as a string using the set format + * + * @return string + */ + public function __toString(): string + { + return $this->format(static::$toStringFormat); + } + /** * Returns whether time is equal to target time. * diff --git a/src/FormattingTrait.php b/src/FormattingTrait.php index 4f72a71..70bd8d6 100644 --- a/src/FormattingTrait.php +++ b/src/FormattingTrait.php @@ -32,7 +32,7 @@ trait FormattingTrait */ public static function resetToStringFormat(): void { - static::setToStringFormat(Chronos::DEFAULT_TO_STRING_FORMAT); + static::setToStringFormat(static::DEFAULT_TO_STRING_FORMAT); } /** diff --git a/tests/TestCase/ChronosTimeTest.php b/tests/TestCase/ChronosTimeTest.php index 2b1a443..a5a0b1c 100644 --- a/tests/TestCase/ChronosTimeTest.php +++ b/tests/TestCase/ChronosTimeTest.php @@ -268,4 +268,16 @@ public function testToDateTimeImmutable(): void $native = ChronosTime::parse('23:59:59.999999')->toNative(); $this->assertSame('23:59:59.999999', $native->format('H:i:s.u')); } + + public function testToString(): void + { + $t = new ChronosTime('12:13:14.123456'); + $this->assertSame('12:13:14', (string)$t); + + ChronosTime::setToStringFormat('H:i:s.u'); + $this->assertSame('12:13:14.123456', (string)$t); + + ChronosTime::resetToStringFormat(); + $this->assertSame('12:13:14', (string)$t); + } } diff --git a/tests/TestCase/Date/StringsTest.php b/tests/TestCase/Date/StringsTest.php index 7afebac..f91312c 100644 --- a/tests/TestCase/Date/StringsTest.php +++ b/tests/TestCase/Date/StringsTest.php @@ -68,7 +68,7 @@ public function testResetToStringFormat() $d = ChronosDate::parse(Chronos::now()); ChronosDate::setToStringFormat('123'); ChronosDate::resetToStringFormat(); - $this->assertSame($d->toDateTimeString(), '' . $d); + $this->assertSame($d->toDateString(), '' . $d); } public function testToDateString()