From 8c16f3b849e43e351a17679cbfc43f01196ebf26 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 24 Sep 2023 08:38:22 +0530 Subject: [PATCH 1/2] Fix casting to string for ChronosDate and ChronosTime. Make all classes explicitly implemement Stringable as recommeneded by the PHP docs. --- src/Chronos.php | 3 +- src/ChronosDate.php | 12 ++++++-- src/ChronosTime.php | 48 ++++++++++++++++++++++++++++- src/FormattingTrait.php | 2 +- tests/TestCase/ChronosTimeTest.php | 12 ++++++++ tests/TestCase/Date/StringsTest.php | 2 +- 6 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/Chronos.php b/src/Chronos.php index 3cc8bad8..2c531b1c 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 2c0730d2..b4d9cfe0 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 01870962..dae8b6d7 100644 --- a/src/ChronosTime.php +++ b/src/ChronosTime.php @@ -18,12 +18,13 @@ use DateTimeInterface; use DateTimeZone; use InvalidArgumentException; +use Stringable; /** * @psalm-immutable * @psalm-consistent-constructor */ -class ChronosTime +class ChronosTime implements Stringable { /** * @var int @@ -50,6 +51,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 +338,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 4f72a716..70bd8d68 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 2b1a4438..a5a0b1cc 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 7afebace..f91312c0 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() From 5e6207e78022d3eebdc61f8ef146f95447486f37 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 24 Sep 2023 08:48:03 +0530 Subject: [PATCH 2/2] Make psalm happy. --- psalm-baseline.xml | 10 ---------- src/ChronosTime.php | 1 - 2 files changed, 11 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index b6ed17df..6cce66c9 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -148,14 +148,4 @@ ];]]> - - - format - mod - mod - mod - mod - mod - - diff --git a/src/ChronosTime.php b/src/ChronosTime.php index dae8b6d7..df84d952 100644 --- a/src/ChronosTime.php +++ b/src/ChronosTime.php @@ -21,7 +21,6 @@ use Stringable; /** - * @psalm-immutable * @psalm-consistent-constructor */ class ChronosTime implements Stringable