Skip to content

Commit 63ecd64

Browse files
committed
Connection, Result: added format 'native'
1 parent 8dfaf3e commit 63ecd64

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/Dibi/Connection.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ class Connection implements IConnection
4545
* Connection options: (see driver-specific options too)
4646
* - lazy (bool) => if true, connection will be established only when required
4747
* - result (array) => result set options
48-
* - formatDateTime => date-time format (if empty, DateTime objects will be returned)
49-
* - formatJson => json format (
50-
* "string" for leaving value as is,
51-
* "object" for decoding json as \stdClass,
52-
* "array" for decoding json as an array - default
53-
* )
48+
* - formatDateTime => date-time format
49+
* empty for decoding as Dibi\DateTime (default)
50+
* "..." formatted according to given format, see https://www.php.net/manual/en/datetime.format.php
51+
* "native" for leaving value as is
52+
* - formatJson => json format
53+
* "array" for decoding json as an array (default)
54+
* "object" for decoding json as \stdClass
55+
* "native" for leaving value as is
5456
* - profiler (array)
5557
* - run (bool) => enable profiler?
5658
* - file => file to log

src/Dibi/Result.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,12 @@ private function normalize(array &$row): void
454454
continue;
455455
}
456456
$value = $row[$key];
457+
$format = $this->formats[$type] ?? null;
457458

458-
if ($type === Type::TEXT) {
459+
if ($type === null || $format === 'native') {
460+
$row[$key] = $value;
461+
462+
} elseif ($type === Type::TEXT) {
459463
$row[$key] = (string) $value;
460464

461465
} elseif ($type === Type::INTEGER) {
@@ -485,7 +489,7 @@ private function normalize(array &$row): void
485489
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
486490
if ($value && substr((string) $value, 0, 3) !== '000') { // '', null, false, '0000-00-00', ...
487491
$value = new DateTime($value);
488-
$row[$key] = empty($this->formats[$type]) ? $value : $value->format($this->formats[$type]);
492+
$row[$key] = empty($format) ? $value : $value->format($format);
489493
} else {
490494
$row[$key] = null;
491495
}
@@ -499,15 +503,12 @@ private function normalize(array &$row): void
499503
$row[$key] = is_string($value) ? $this->getResultDriver()->unescapeBinary($value) : $value;
500504

501505
} elseif ($type === Type::JSON) {
502-
if ($this->formats[$type] === 'string') {
506+
if ($format === 'string') { // back compatibility with 'native'
503507
$row[$key] = $value;
504508
} else {
505-
$row[$key] = json_decode($value, $this->formats[$type] === 'array');
509+
$row[$key] = json_decode($value, $format === 'array');
506510
}
507511

508-
} elseif ($type === null) {
509-
$row[$key] = $value;
510-
511512
} else {
512513
throw new \RuntimeException('Unexpected type ' . $type);
513514
}

tests/dibi/Result.normalize.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ class MockResult extends Dibi\Result
2424
}
2525

2626

27+
test('', function () {
28+
$result = new MockResult;
29+
$result->setType('col', Type::TEXT);
30+
$result->setFormat(Type::TEXT, 'native');
31+
32+
Assert::same(['col' => null], $result->test(['col' => null]));
33+
Assert::same(['col' => true], $result->test(['col' => true]));
34+
Assert::same(['col' => false], $result->test(['col' => false]));
35+
});
36+
37+
2738
test('', function () {
2839
$result = new MockResult;
2940
$result->setType('col', Type::BOOL);

0 commit comments

Comments
 (0)