From f4d8264c9781f508170436c1f32eaa3ca397dff6 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 8 Oct 2020 18:21:42 +0200 Subject: [PATCH] Connection: added option [result][formatTimeInterval] that sets time-interval column decoding --- src/Dibi/Connection.php | 5 +++++ src/Dibi/Result.php | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index de23ee6ae..0400f37b1 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -50,6 +50,10 @@ class Connection implements IConnection * 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 + * - formatTimeInterval => time-interval format + * empty for decoding as DateInterval (default) + * "..." formatted according to given format, see https://www.php.net/manual/en/dateinterval.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 @@ -77,6 +81,7 @@ public function __construct(array $config, string $name = null) Type::DATE => $this->config['result']['formatDate'], Type::DATETIME => $this->config['result']['formatDateTime'], Type::JSON => $this->config['result']['formatJson'] ?? 'array', + Type::TIME_INTERVAL => $this->config['result']['formatTimeInterval'] ?? null, ]; // profiler diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index da00b78fb..b066057dd 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -498,8 +498,9 @@ private function normalize(array &$row): void } elseif ($type === Type::TIME_INTERVAL) { preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m); - $row[$key] = new \DateInterval("PT$m[2]H$m[3]M$m[4]S"); - $row[$key]->invert = (int) (bool) $m[1]; + $value = new \DateInterval("PT$m[2]H$m[3]M$m[4]S"); + $value->invert = (int) (bool) $m[1]; + $row[$key] = $format ? $value->format($format) : $value; } elseif ($type === Type::BINARY) { $row[$key] = is_string($value) ? $this->getResultDriver()->unescapeBinary($value) : $value;