diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 18c1d36..b73703f 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -45,12 +45,14 @@ class Connection implements IConnection * Connection options: (see driver-specific options too) * - lazy (bool) => if true, connection will be established only when required * - result (array) => result set options - * - formatDateTime => date-time format (if empty, DateTime objects will be returned) - * - formatJson => json format ( - * "string" for leaving value as is, - * "object" for decoding json as \stdClass, - * "array" for decoding json as an array - default - * ) + * - formatDateTime => date-time format + * empty for decoding as Dibi\DateTime (default) + * "..." formatted according to given format, see https://www.php.net/manual/en/datetime.format.php + * "native" for leaving value as is + * - formatJson => json format + * "array" for decoding json as an array (default) + * "object" for decoding json as \stdClass + * "native" for leaving value as is * - profiler (array) * - run (bool) => enable profiler? * - file => file to log diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index 36f098e..d998ba7 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -454,8 +454,12 @@ class Result implements IDataSource continue; } $value = $row[$key]; + $format = $this->formats[$type] ?? null; - if ($type === Type::TEXT) { + if ($type === null || $format === 'native') { + $row[$key] = $value; + + } elseif ($type === Type::TEXT) { $row[$key] = (string) $value; } elseif ($type === Type::INTEGER) { @@ -485,9 +489,7 @@ class Result implements IDataSource } elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) { if ($value && substr((string) $value, 0, 3) !== '000') { // '', null, false, '0000-00-00', ... $value = new DateTime($value); - $row[$key] = empty($this->formats[$type]) - ? $value - : $value->format($this->formats[$type]); + $row[$key] = $format ? $value->format($format) : $value; } else { $row[$key] = null; } @@ -503,15 +505,12 @@ class Result implements IDataSource : $value; } elseif ($type === Type::JSON) { - if ($this->formats[$type] === 'string') { + if ($format === 'string') { // back compatibility with 'native' $row[$key] = $value; } else { - $row[$key] = json_decode($value, $this->formats[$type] === 'array'); + $row[$key] = json_decode($value, $format === 'array'); } - } elseif ($type === null) { - $row[$key] = $value; - } else { throw new \RuntimeException('Unexpected type ' . $type); } diff --git a/tests/dibi/Result.normalize.phpt b/tests/dibi/Result.normalize.phpt index 5a19394..6542ab2 100644 --- a/tests/dibi/Result.normalize.phpt +++ b/tests/dibi/Result.normalize.phpt @@ -24,6 +24,17 @@ class MockResult extends Dibi\Result } +test('', function () { + $result = new MockResult; + $result->setType('col', Type::TEXT); + $result->setFormat(Type::TEXT, 'native'); + + Assert::same(['col' => null], $result->test(['col' => null])); + Assert::same(['col' => true], $result->test(['col' => true])); + Assert::same(['col' => false], $result->test(['col' => false])); +}); + + test('', function () { $result = new MockResult; $result->setType('col', Type::BOOL);