Skip to content

Commit

Permalink
Merge pull request #428 from cakephp/3.x-stringable
Browse files Browse the repository at this point in the history
Fix casting to string for ChronosDate and ChronosTime.
  • Loading branch information
othercorey authored Sep 24, 2023
2 parents 11b1f05 + 5e6207e commit 7ffdb16
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 17 deletions.
10 changes: 0 additions & 10 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,4 @@
];]]></code>
</ImpureStaticVariable>
</file>
<file src="src/ChronosTime.php">
<ImpureMethodCall>
<code>format</code>
<code>mod</code>
<code>mod</code>
<code>mod</code>
<code>mod</code>
<code>mod</code>
</ImpureMethodCall>
</file>
</files>
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
49 changes: 47 additions & 2 deletions src/ChronosTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand Down Expand Up @@ -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.
*
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);
}

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

0 comments on commit 7ffdb16

Please sign in to comment.