mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 13:47:33 +02:00
Add support for DateTimeInterface
This commit is contained in:
committed by
David Grudl
parent
0071b80938
commit
176b1a8895
@@ -278,7 +278,7 @@ class DibiFirebirdDriver extends DibiObject implements IDibiDriver, IDibiResultD
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||||
|
@@ -234,7 +234,7 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver, IDibiResult
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||||
|
@@ -219,7 +219,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||||
|
@@ -313,7 +313,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver, IDibiResultDriv
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||||
|
@@ -301,7 +301,7 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||||
|
@@ -242,7 +242,7 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver, IDibiResultDrive
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? "#m/d/Y H:i:s#" : "#m/d/Y#");
|
return $value->format($type === dibi::DATETIME ? "#m/d/Y H:i:s#" : "#m/d/Y#");
|
||||||
|
@@ -236,7 +236,7 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
|
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
|
||||||
|
@@ -279,7 +279,7 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||||
|
@@ -294,7 +294,7 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver, IDibiResultDr
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
|
||||||
|
@@ -235,7 +235,7 @@ class DibiSqlite3Driver extends DibiObject implements IDibiDriver, IDibiResultDr
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
|
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
|
||||||
|
@@ -240,7 +240,7 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver, IDibiResultDri
|
|||||||
|
|
||||||
case dibi::DATE:
|
case dibi::DATE:
|
||||||
case dibi::DATETIME:
|
case dibi::DATETIME:
|
||||||
if (!$value instanceof DateTime) {
|
if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
}
|
}
|
||||||
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
|
return $value->format($type === dibi::DATETIME ? $this->fmtDateTime : $this->fmtDate);
|
||||||
|
@@ -326,7 +326,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
|
|
||||||
// with modifier procession
|
// with modifier procession
|
||||||
if ($modifier) {
|
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;
|
$this->hasError = TRUE;
|
||||||
return '**Unexpected type ' . gettype($value) . '**';
|
return '**Unexpected type ' . gettype($value) . '**';
|
||||||
}
|
}
|
||||||
@@ -444,7 +444,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
} elseif ($value === NULL) {
|
} elseif ($value === NULL) {
|
||||||
return 'NULL';
|
return 'NULL';
|
||||||
|
|
||||||
} elseif ($value instanceof DateTime) {
|
} elseif ($value instanceof DateTime || $value instanceof DateTimeInterface) {
|
||||||
return $this->driver->escape($value, dibi::DATETIME);
|
return $this->driver->escape($value, dibi::DATETIME);
|
||||||
|
|
||||||
} elseif ($value instanceof DibiLiteral) {
|
} elseif ($value instanceof DibiLiteral) {
|
||||||
|
22
tests/dibi/DibiTranslator.DateTimeInterface.phpt
Normal file
22
tests/dibi/DibiTranslator.DateTimeInterface.phpt
Normal 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));
|
Reference in New Issue
Block a user