diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index d98f7906..2fb2849c 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -515,7 +515,7 @@ class Result implements IDataSource } elseif ($type === Type::BOOL) { $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F'; - } elseif ($type === Type::DATE || $type === Type::DATETIME) { + } elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) { if ((int) $value !== 0 || substr((string) $value, 0, 3) === '00:') { // '', NULL, FALSE, '0000-00-00', ... $value = new DateTime($value); $row[$key] = empty($this->formats[$type]) ? $value : $value->format($this->formats[$type]); diff --git a/src/Dibi/Type.php b/src/Dibi/Type.php index 801e5ee4..f6c40e8e 100644 --- a/src/Dibi/Type.php +++ b/src/Dibi/Type.php @@ -20,7 +20,7 @@ class Type INTEGER = 'i', FLOAT = 'f', DATE = 'd', - DATETIME = 't', + DATETIME = 'dt', TIME = 't', TIME_INTERVAL = 'ti'; diff --git a/tests/dibi/Result.normalize.phpt b/tests/dibi/Result.normalize.phpt index 26898709..68b87ada 100644 --- a/tests/dibi/Result.normalize.phpt +++ b/tests/dibi/Result.normalize.phpt @@ -192,3 +192,36 @@ test(function () { Assert::equal(['col' => '2015-10-13 00:00:00'], $result->test(['col' => '2015-10-13'])); Assert::equal(['col' => '2015-10-13 14:30:00'], $result->test(['col' => '2015-10-13 14:30'])); }); + + +test(function () { + $result = new MockResult; + $result->setType('col', Type::DATE); + + Assert::same(['col' => NULL], $result->test(['col' => NULL])); + Assert::exception(function () use ($result) { + $result->test(['col' => TRUE]); + }, 'Exception'); + Assert::same(['col' => NULL], $result->test(['col' => FALSE])); + + Assert::same(['col' => NULL], $result->test(['col' => ''])); + Assert::same(['col' => NULL], $result->test(['col' => '0000-00-00'])); + Assert::equal(['col' => new Dibi\DateTime('2015-10-13')], $result->test(['col' => '2015-10-13'])); +}); + + +test(function () { + $result = new MockResult; + $result->setType('col', Type::TIME); + + Assert::same(['col' => NULL], $result->test(['col' => NULL])); + Assert::exception(function () use ($result) { + $result->test(['col' => TRUE]); + }, 'Exception'); + Assert::same(['col' => NULL], $result->test(['col' => FALSE])); + + Assert::same(['col' => NULL], $result->test(['col' => ''])); + Assert::same(['col' => NULL], $result->test(['col' => '0000-00-00'])); + Assert::equal(['col' => new Dibi\DateTime('00:00:00')], $result->test(['col' => '00:00:00'])); + Assert::equal(['col' => new Dibi\DateTime('14:30')], $result->test(['col' => '14:30'])); +});