From a9e90d0b22e353e68658d8e2ab407c68447f282d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 8 Oct 2020 17:12:21 +0200 Subject: [PATCH] Connection, Results: refactorings, added Result::setFormats() --- src/Dibi/Connection.php | 16 +++++++++++----- src/Dibi/Result.php | 12 +++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 145c7856..18c1d36f 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -28,6 +28,9 @@ class Connection implements IConnection /** @var array Current connection configuration */ private $config; + /** @var string[] resultset formats */ + private $formats; + /** @var Driver|null */ private $driver; @@ -65,9 +68,14 @@ class Connection implements IConnection Helpers::alias($config, 'result|formatDateTime', 'resultDateTime'); $config['driver'] = $config['driver'] ?? 'mysqli'; $config['name'] = $name; - $config['result']['formatJson'] = $config['result']['formatJson'] ?? 'array'; $this->config = $config; + $this->formats = [ + Type::DATE => $this->config['result']['formatDate'], + Type::DATETIME => $this->config['result']['formatDateTime'], + Type::JSON => $this->config['result']['formatJson'] ?? 'array', + ]; + // profiler if (isset($config['profiler']['file']) && (!isset($config['profiler']['run']) || $config['profiler']['run'])) { $filter = $config['profiler']['filter'] ?? Event::QUERY; @@ -391,10 +399,8 @@ class Connection implements IConnection */ 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::JSON, $this->config['result']['formatJson']); + return (new Result($resultDriver)) + ->setFormats($this->formats); } diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index 67bf46ca..36f098e7 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -549,7 +549,7 @@ class Result implements IDataSource /** - * Sets date format. + * Sets type format. */ final public function setFormat(string $type, ?string $format): self { @@ -558,6 +558,16 @@ class Result implements IDataSource } + /** + * Sets type formats. + */ + final public function setFormats(array $formats): self + { + $this->formats = $formats; + return $this; + } + + /** * Returns data format. */