Skip to content

Commit 28559e9

Browse files
committed
RowNormalizer: added configuring methods [Closes #257]
1 parent e2fa816 commit 28559e9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Diff for: src/Database/RowNormalizer.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,38 @@ final class RowNormalizer
1919
{
2020
use Nette\SmartObject;
2121

22+
private $skipped = [];
23+
24+
25+
public function skipNumeric(): static
26+
{
27+
$this->skipped[IStructure::FIELD_FIXED] = true;
28+
return $this;
29+
}
30+
31+
32+
public function skipDateTime(): static
33+
{
34+
$this->skipped[IStructure::FIELD_DATETIME] = true;
35+
$this->skipped[IStructure::FIELD_DATE] = true;
36+
$this->skipped[IStructure::FIELD_TIME] = true;
37+
return $this;
38+
}
39+
40+
41+
public function skipInterval(): static
42+
{
43+
$this->skipped[IStructure::FIELD_TIME_INTERVAL] = true;
44+
return $this;
45+
}
46+
47+
2248
public function __invoke(array $row, ResultSet $resultSet): array
2349
{
2450
foreach ($resultSet->getColumnTypes() as $key => $type) {
25-
$row[$key] = $this->normalizeField($row[$key], $type);
51+
if (!isset($this->skipped[$type])) {
52+
$row[$key] = $this->normalizeField($row[$key], $type);
53+
}
2654
}
2755

2856
return $row;

Diff for: tests/Database/ResultSet.customNormalizer.phpt

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ test('disabled normalization', function () use ($connection) {
2929
});
3030

3131

32+
test('configured RowNormalizer', function () use ($connection) {
33+
global $driverName;
34+
35+
$connection->setRowNormalizer((new Nette\Database\RowNormalizer)->skipDateTime());
36+
$res = $connection->query('SELECT * FROM author');
37+
Assert::same([
38+
'id' => 11,
39+
'name' => 'Jakub Vrana',
40+
'web' => 'http://www.vrana.cz/',
41+
'born' => $driverName === 'sqlite' ? (PHP_VERSION_ID >= 80100 ? 1_642_892_400 : '1642892400') : '2022-01-23',
42+
], (array) $res->fetch());
43+
});
44+
45+
3246
test('custom normalization', function () use ($connection) {
3347
global $driverName;
3448

0 commit comments

Comments
 (0)