Skip to content

Commit

Permalink
Connection: added option [result][formatJson] that sets json column d…
Browse files Browse the repository at this point in the history
…ecoding (text|object|array) (#335)
  • Loading branch information
paveljanda authored and dg committed Sep 9, 2019
1 parent 0535d57 commit a2afac8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Connection implements IConnection
* - 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
* )
* - profiler (array)
* - run (bool) => enable profiler?
* - file => file to log
Expand All @@ -59,6 +64,7 @@ public function __construct(array $config, string $name = null)
Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
$config['driver'] = $config['driver'] ?? 'mysqli';
$config['name'] = $name;
$config['result']['formatJson'] = $config['result']['formatJson'] ?? 'array';
$this->config = $config;

// profiler
Expand Down Expand Up @@ -395,7 +401,8 @@ public function createResultSet(ResultDriver $resultDriver): Result
{
$res = new Result($resultDriver);
return $res->setFormat(Type::DATE, $this->config['result']['formatDate'])
->setFormat(Type::DATETIME, $this->config['result']['formatDateTime']);
->setFormat(Type::DATETIME, $this->config['result']['formatDateTime'])
->setFormat(Type::JSON, $this->config['result']['formatJson']);
}


Expand Down
6 changes: 5 additions & 1 deletion src/Dibi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,11 @@ private function normalize(array &$row): void
$row[$key] = is_string($value) ? $this->getResultDriver()->unescapeBinary($value) : $value;

} elseif ($type === Type::JSON) {
$row[$key] = json_decode($value, true);
if ($this->formats[$type] === 'string') {
$row[$key] = $value;
} else {
$row[$key] = json_decode($value, $this->formats[$type] === 'array');
}
}
}
}
Expand Down

0 comments on commit a2afac8

Please sign in to comment.