Skip to content

Commit 229fec5

Browse files
committed
RowNormalizer: added configuring methods [Closes #257]
1 parent 3e7168b commit 229fec5

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Database/RowNormalizer.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ final class RowNormalizer
2828
];
2929

3030

31+
private $skipped = [];
32+
33+
3134
/**
3235
* Heuristic column type detection.
3336
* @return Type::*
@@ -49,10 +52,36 @@ public static function detectType(string $type): string
4952
}
5053

5154

55+
public function skipNumeric(): static
56+
{
57+
$this->skipped[Type::Decimal] = true;
58+
return $this;
59+
}
60+
61+
62+
public function skipDateTime(): static
63+
{
64+
$this->skipped[Type::DateTime] = true;
65+
$this->skipped[Type::Date] = true;
66+
$this->skipped[Type::Time] = true;
67+
$this->skipped[Type::UnixTimestamp] = true;
68+
return $this;
69+
}
70+
71+
72+
public function skipInterval(): static
73+
{
74+
$this->skipped[Type::TimeInterval] = true;
75+
return $this;
76+
}
77+
78+
5279
public function __invoke(array $row, ResultSet $resultSet): array
5380
{
5481
foreach ($resultSet->getColumnTypes() as $key => $type) {
55-
$row[$key] = $this->normalizeField($row[$key], $type);
82+
if (!isset($this->skipped[$type])) {
83+
$row[$key] = $this->normalizeField($row[$key], $type);
84+
}
5685
}
5786

5887
return $row;

tests/Database/ResultSet.customNormalizer.phpt

Lines changed: 14 additions & 0 deletions
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+
$driverName = $GLOBALS['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
$driverName = $GLOBALS['driverName'];
3448

0 commit comments

Comments
 (0)