Skip to content

Commit bc0332a

Browse files
committed
Connection, Results: refactorings, passes $formats to Result::__construct()
1 parent b7ae806 commit bc0332a

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

src/Dibi/Connection.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class Connection implements IConnection
2828
/** @var array Current connection configuration */
2929
private $config;
3030

31+
/** @var string[] resultset formats */
32+
private $formats;
33+
3134
/** @var Driver|null */
3235
private $driver;
3336

@@ -65,9 +68,14 @@ public function __construct(array $config, string $name = null)
6568
Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
6669
$config['driver'] = $config['driver'] ?? 'mysqli';
6770
$config['name'] = $name;
68-
$config['result']['formatJson'] = $config['result']['formatJson'] ?? 'array';
6971
$this->config = $config;
7072

73+
$this->formats = [
74+
Type::DATE => $this->config['result']['formatDate'],
75+
Type::DATETIME => $this->config['result']['formatDateTime'],
76+
Type::JSON => $this->config['result']['formatJson'] ?? 'array',
77+
];
78+
7179
// profiler
7280
if (isset($config['profiler']['file']) && (!isset($config['profiler']['run']) || $config['profiler']['run'])) {
7381
$filter = $config['profiler']['filter'] ?? Event::QUERY;
@@ -391,10 +399,7 @@ public function rollback(string $savepoint = null): void
391399
*/
392400
public function createResultSet(ResultDriver $resultDriver): Result
393401
{
394-
$res = new Result($resultDriver);
395-
return $res->setFormat(Type::DATE, $this->config['result']['formatDate'])
396-
->setFormat(Type::DATETIME, $this->config['result']['formatDateTime'])
397-
->setFormat(Type::JSON, $this->config['result']['formatJson']);
402+
return new Result($resultDriver, $this->formats);
398403
}
399404

400405

src/Dibi/Result.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ class Result implements IDataSource
3838
private $rowFactory;
3939

4040
/** @var array format */
41-
private $formats = [];
41+
private $formats;
4242

4343

44-
public function __construct(ResultDriver $driver)
44+
public function __construct(ResultDriver $driver, array $formats = [])
4545
{
4646
$this->driver = $driver;
47+
$this->formats = $formats;
4748
$this->detectTypes();
4849
}
4950

0 commit comments

Comments
 (0)