mirror of
https://github.com/dg/dibi.git
synced 2025-02-22 09:53:11 +01:00
Connection, Result: added format 'native'
This commit is contained in:
parent
a9e90d0b22
commit
c464960239
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user