Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix casting to string for ChronosDate and ChronosTime. #428

Merged
merged 2 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use DateTimeZone;
use InvalidArgumentException;
use RuntimeException;
use Stringable;

/**
* An Immutable extension on the native DateTime object.
Expand Down Expand Up @@ -58,7 +59,7 @@
* @psalm-immutable
* @psalm-consistent-constructor
*/
class Chronos extends DateTimeImmutable
class Chronos extends DateTimeImmutable implements Stringable
{
use FormattingTrait;

Expand Down
12 changes: 10 additions & 2 deletions src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
use Stringable;

/**
* An immutable date object.
Expand All @@ -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.
Expand Down
48 changes: 47 additions & 1 deletion src/ChronosTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,6 +51,20 @@
*/
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
*/
Expand Down Expand Up @@ -323,6 +338,37 @@
return $this->toDateTimeImmutable()->format($format);
}

/**
* Reset the format used to the default when converting to a string
*
* @return void
*/
public static function resetToStringFormat(): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably never needed the reset helpers since you can pass the constant to set directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just made it consistent with the other 2 classes.

{
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);

Check failure on line 369 in src/ChronosTime.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

ImpureStaticProperty

src/ChronosTime.php:369:30: ImpureStaticProperty: Cannot use a static property in a mutation-free context (see https://psalm.dev/221)
}

/**
* Returns whether time is equal to target time.
*
Expand Down
2 changes: 1 addition & 1 deletion src/FormattingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this fix and addition of the constant to ChronosDate, calling ChronosDate::__toString() after ChronosDate::resetToStringFormat() returned value in Y-m-d H:i:s format instead of H:i:s.

}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/ChronosTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Date/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading