1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-04 21:28:02 +02:00

Add support for DateTimeInterface

This commit is contained in:
Patrik Votocek
2014-03-24 15:37:25 +01:00
committed by David Grudl
parent 0071b80938
commit 176b1a8895
13 changed files with 35 additions and 13 deletions

View File

@@ -278,7 +278,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");

View File

@@ -234,7 +234,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");

View File

@@ -219,7 +219,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");

View File

@@ -313,7 +313,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");

View File

@@ -301,7 +301,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");

View File

@@ -242,7 +242,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "#m/d/Y H:i:s#" : "#m/d/Y#");

View File

@@ -236,7 +236,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);

View File

@@ -279,7 +279,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");

View File

@@ -294,7 +294,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");

View File

@@ -235,7 +235,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);

View File

@@ -240,7 +240,7 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri
case dibi::DATE:
case dibi::DATETIME:
if (!$value instanceof DateTime) {
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
$value = new DibiDateTime($value);
}
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);

View File

@@ -326,7 +326,7 @@ final class DibiTranslator extends DibiObject
// with modifier procession
if ($modifier) {
if ($value !== NULL && !is_scalar($value) && !($value instanceof DateTime)) { // array is already processed
if ($value !== NULL && !is_scalar($value) && !$value instanceof DateTime && !$value instanceof DateTimeInterface) { // array is already processed
$this->hasError = TRUE;
return '**Unexpected type ' . gettype($value) . '**';
}
@@ -444,7 +444,7 @@ final class DibiTranslator extends DibiObject
} elseif ($value === NULL) {
return 'NULL';
} elseif ($value instanceof DateTime) {
} elseif ($value instanceof DateTime || $value instanceof DateTimeInterface) {
return $this->driver->escape($value, dibi::DATETIME);
} elseif ($value instanceof DibiLiteral) {

View File

@@ -0,0 +1,22 @@
<?php
/**
* Test: DateTimeInterface of DibiTranslator
*
* @author Patrik Votoček
* @phpversion 5.5
*/
require __DIR__ . '/bootstrap.php';
$connection = new DibiConnection(array(
'driver' => 'sqlite3',
'database' => ':memory:',
));
$translator = new DibiTranslator($connection);
$datetime = new DateTime('1978-01-23 00:00:00');
Assert::equal($datetime->format('U'), $translator->formatValue(new DateTime($datetime->format('c')), NULL));
Assert::equal($datetime->format('U'), $translator->formatValue(new DateTimeImmutable($datetime->format('c')), NULL));