mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 21:58:10 +02:00
Result: normalizes invalid date to NULL
This commit is contained in:
@@ -518,6 +518,8 @@ class Result implements IDataSource
|
||||
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]);
|
||||
} else {
|
||||
$row[$key] = NULL;
|
||||
}
|
||||
|
||||
} elseif ($type === Type::BINARY) {
|
||||
|
@@ -155,3 +155,40 @@ test(function () {
|
||||
Assert::same(['col' => 1], $result->test(['col' => 1]));
|
||||
Assert::same(['col' => 1], $result->test(['col' => 1.0]));
|
||||
});
|
||||
|
||||
|
||||
test(function () {
|
||||
$result = new MockResult;
|
||||
$result->setType('col', Type::DATETIME);
|
||||
|
||||
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('2015-10-13')], $result->test(['col' => '2015-10-13']));
|
||||
Assert::equal(['col' => new Dibi\DateTime('2015-10-13 14:30')], $result->test(['col' => '2015-10-13 14:30']));
|
||||
});
|
||||
|
||||
|
||||
test(function () {
|
||||
$result = new MockResult;
|
||||
$result->setType('col', Type::DATETIME);
|
||||
$result->setFormat(Type::DATETIME, 'Y-m-d H:i:s');
|
||||
|
||||
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::same(['col' => date('Y-m-d 00:00:00')], $result->test(['col' => '00:00:00']));
|
||||
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']));
|
||||
});
|
||||
|
Reference in New Issue
Block a user