Skip to content

Commit 81a66da

Browse files
committed
Connection: added option [result][formatTimeInterval] that sets time-interval column decoding
1 parent 624f321 commit 81a66da

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Dibi/Connection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class Connection implements IConnection
5050
* empty for decoding as Dibi\DateTime (default)
5151
* "..." formatted according to given format, see https://www.php.net/manual/en/datetime.format.php
5252
* "native" for leaving value as is
53+
* - formatTimeInterval => time-interval format
54+
* empty for decoding as DateInterval (default)
55+
* "..." formatted according to given format, see https://www.php.net/manual/en/dateinterval.format.php
56+
* "native" for leaving value as is
5357
* - formatJson => json format
5458
* "array" for decoding json as an array (default)
5559
* "object" for decoding json as \stdClass
@@ -77,6 +81,7 @@ public function __construct(array $config, string $name = null)
7781
Type::DATE => $this->config['result']['formatDate'],
7882
Type::DATETIME => $this->config['result']['formatDateTime'],
7983
Type::JSON => $this->config['result']['formatJson'] ?? 'array',
84+
Type::TIME_INTERVAL => $this->config['result']['formatTimeInterval'] ?? null,
8085
];
8186

8287
// profiler

src/Dibi/Result.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,9 @@ private function normalize(array &$row): void
498498

499499
} elseif ($type === Type::TIME_INTERVAL) {
500500
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m);
501-
$row[$key] = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
502-
$row[$key]->invert = (int) (bool) $m[1];
501+
$value = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
502+
$value->invert = (int) (bool) $m[1];
503+
$row[$key] = $format ? $value->format($format) : $value;
503504

504505
} elseif ($type === Type::BINARY) {
505506
$row[$key] = is_string($value) ? $this->getResultDriver()->unescapeBinary($value) : $value;

0 commit comments

Comments
 (0)