Skip to content

Commit

Permalink
Connection: added option [result][formatTimeInterval] that sets time-…
Browse files Browse the repository at this point in the history
…interval column decoding
  • Loading branch information
dg committed Nov 2, 2020
1 parent a9b3683 commit f734bf8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Dibi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f734bf8

Please sign in to comment.