Skip to content

Commit

Permalink
Merge branch 'support-DateTimeImmutable' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
k-holy committed Apr 5, 2023
2 parents 84b138a + 9bbc731 commit e107651
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Adapter/Mysql/MysqlParameterBuilder.php
Expand Up @@ -170,7 +170,7 @@ public function toDate(mixed $value): string
}

// DateTime
if ($value instanceof \DateTime) {
if ($value instanceof \DateTimeInterface) {
return sprintf("STR_TO_DATE('%s', '%s')",
$value->format(sprintf('Y%sm%sd',
self::$dateDelimiter,
Expand Down Expand Up @@ -258,7 +258,7 @@ public function toTimestamp(mixed $value): string
$value->setTimezone(new \DateTimeZone(date_default_timezone_get()));
}

if ($value instanceof \DateTime) {
if ($value instanceof \DateTimeInterface) {
return sprintf("STR_TO_DATE('%s', '%s')",
$value->format(sprintf('Y%sm%sd%sH%si%ss',
self::$dateDelimiter,
Expand Down Expand Up @@ -360,7 +360,7 @@ public function toTime(mixed $value): string
}

// DateTime
if ($value instanceof \DateTime) {
if ($value instanceof \DateTimeInterface) {
return sprintf("'%s'", $value->format(sprintf('H%si%ss',
self::$timeDelimiter,
self::$timeDelimiter
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/Sqlite/SqliteParameterBuilder.php
Expand Up @@ -148,7 +148,7 @@ public function toDate(mixed $value): string

// DateTime
// SQLiteの日付関数は固定書式
if ($value instanceof \DateTime) {
if ($value instanceof \DateTimeInterface) {
return sprintf("date('%s')", $value->format('Y-m-d'));
}

Expand Down Expand Up @@ -218,7 +218,7 @@ public function toTimestamp(mixed $value): string

// DateTime
// SQLiteの日付関数は固定書式
if ($value instanceof \DateTime) {
if ($value instanceof \DateTimeInterface) {
return sprintf("datetime('%s')", $value->format('Y-m-d H:i:s'));
}

Expand Down
Expand Up @@ -363,6 +363,14 @@ public function testToDateForDateTime()
$this->assertEquals("STR_TO_DATE('2013-01-02', '%Y-%m-%d')", $builder->ToDate(new \DateTime('2013-01-02')));
}

public function testToDateForDateTimeImmutable()
{
$builder = new MysqlParameterBuilder(
new PdoDriver($this->getPdo(), new MysqlMetaDataProcessor())
);
$this->assertEquals("STR_TO_DATE('2013-01-02', '%Y-%m-%d')", $builder->ToDate(new \DateTimeImmutable('2013-01-02')));
}

public function testToDateForUnixTimestamp()
{
$builder = new MysqlParameterBuilder(
Expand Down Expand Up @@ -490,6 +498,14 @@ public function testToTimestampForDateTime()
$this->assertEquals("STR_TO_DATE('2013-01-02 03:04:05', '%Y-%m-%d %H:%i:%s')", $builder->toTimestamp(new \DateTime('2013-01-02 03:04:05')));
}

public function testToTimestampForDateTimeImmutable()
{
$builder = new MysqlParameterBuilder(
new PdoDriver($this->getPdo(), new MysqlMetaDataProcessor())
);
$this->assertEquals("STR_TO_DATE('2013-01-02 03:04:05', '%Y-%m-%d %H:%i:%s')", $builder->toTimestamp(new \DateTimeImmutable('2013-01-02 03:04:05')));
}

public function testToTimestampForUnixTimestamp()
{
$builder = new MysqlParameterBuilder(
Expand Down Expand Up @@ -596,6 +612,14 @@ public function testToTimeForDateTime()
$this->assertEquals("'03:04:05'", $builder->toTime(new \DateTime('2013-01-02 03:04:05')));
}

public function testToTimeForDateTimeImmutable()
{
$builder = new MysqlParameterBuilder(
new PdoDriver($this->getPdo(), new MysqlMetaDataProcessor())
);
$this->assertEquals("'03:04:05'", $builder->toTime(new \DateTimeImmutable('2013-01-02 03:04:05')));
}

public function testToTimeForUnixTimestamp()
{
$builder = new MysqlParameterBuilder(
Expand Down
Expand Up @@ -285,6 +285,14 @@ public function testToDateForDateTime()
$this->assertEquals("date('2013-01-02')", $builder->ToDate(new \DateTime('2013-01-02')));
}

public function testToDateForDateTimeImmutable()
{
$builder = new SqliteParameterBuilder(
new PdoDriver($this->getPdo(), new SqliteMetaDataProcessor())
);
$this->assertEquals("date('2013-01-02')", $builder->ToDate(new \DateTimeImmutable('2013-01-02')));
}

public function testToDateForUnixTimestamp()
{
$builder = new SqliteParameterBuilder(
Expand Down Expand Up @@ -397,6 +405,14 @@ public function testToTimestampForDateTime()
$this->assertEquals("datetime('2013-01-02 03:04:05')", $builder->toTimestamp(new \DateTime('2013-01-02 03:04:05')));
}

public function testToTimestampForDateTimeImmutable()
{
$builder = new SqliteParameterBuilder(
new PdoDriver($this->getPdo(), new SqliteMetaDataProcessor())
);
$this->assertEquals("datetime('2013-01-02 03:04:05')", $builder->toTimestamp(new \DateTimeImmutable('2013-01-02 03:04:05')));
}

public function testToTimestampForUnixTimestamp()
{
$builder = new SqliteParameterBuilder(
Expand Down

0 comments on commit e107651

Please sign in to comment.