-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathRowNormalizer.php
65 lines (49 loc) · 1.81 KB
/
RowNormalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Database;
use Nette;
/**
* Default implementation for row normalization.
*/
class RowNormalizer implements IRowNormalizer
{
use Nette\SmartObject;
/** @var ISupplementalDriver */
private $supplementalDriver;
/**
* @inheritdoc
*/
public function normalizeRow($row, ResultSet $resultSet)
{
foreach ($resultSet->getColumnTypes() as $key => $type) {
$value = $row[$key];
if ($value === NULL || $value === FALSE || $type === IStructure::FIELD_TEXT) {
} elseif ($type === IStructure::FIELD_INTEGER) {
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
} elseif ($type === IStructure::FIELD_FLOAT) {
if (($pos = strpos($value, '.')) !== FALSE) {
$value = rtrim(rtrim($pos === 0 ? "0$value" : $value, '0'), '.');
}
$float = (float) $value;
$row[$key] = (string) $float === $value ? $float : $value;
} elseif ($type === IStructure::FIELD_BOOL) {
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
} elseif ($type === IStructure::FIELD_DATETIME || $type === IStructure::FIELD_DATE || $type === IStructure::FIELD_TIME) {
$row[$key] = new Nette\Utils\DateTime($value);
} elseif ($type === IStructure::FIELD_TIME_INTERVAL) {
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m);
$row[$key] = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
$row[$key]->invert = (int) (bool) $m[1];
} elseif ($type === IStructure::FIELD_UNIX_TIMESTAMP) {
$row[$key] = Nette\Utils\DateTime::from($value);
}
}
if ($this->supplementalDriver === NULL) {
$this->supplementalDriver = $resultSet->getConnection()->getSupplementalDriver();
}
return $this->supplementalDriver->normalizeRow($row);
}
}