1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of PommProject's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2017 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Foundation\Converter; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Session\Session; |
13
|
|
|
use PommProject\Foundation\Exception\ConverterException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PgTimestamp |
17
|
|
|
* |
18
|
|
|
* Date and timestamp converter |
19
|
|
|
* |
20
|
|
|
* @package Foundation |
21
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
22
|
|
|
* @author Grégoire HUBERT <[email protected]> |
23
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
24
|
|
|
* @see ConverterInterface |
25
|
|
|
*/ |
26
|
|
|
class PgTimestamp implements ConverterInterface |
27
|
|
|
{ |
28
|
|
|
const TS_FORMAT = 'Y-m-d H:i:s.uP'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* fromPg |
32
|
|
|
* |
33
|
|
|
* @see ConverterInterface |
34
|
|
|
*/ |
35
|
|
|
public function fromPg($data, $type, Session $session) |
36
|
|
|
{ |
37
|
|
|
$data = trim($data); |
38
|
|
|
|
39
|
|
|
return $data !== '' ? new \DateTime($data) : null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* toPg |
44
|
|
|
* |
45
|
|
|
* @see ConverterInterface |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function toPg($data, $type, Session $session) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return |
50
|
|
|
$data !== null |
51
|
|
|
? sprintf("%s '%s'", $type, $this->checkData($data)->format(static::TS_FORMAT)) |
52
|
|
|
: sprintf("NULL::%s", $type) |
53
|
|
|
; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* toPgStandardFormat |
58
|
|
|
* |
59
|
|
|
* @see ConverterInterface |
60
|
|
|
*/ |
61
|
|
|
public function toPgStandardFormat($data, $type, Session $session) |
62
|
|
|
{ |
63
|
|
|
return |
64
|
|
|
$data !== null |
65
|
|
|
? $this->checkData($data)->format(static::TS_FORMAT) |
66
|
|
|
: null |
67
|
|
|
; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* checkData |
72
|
|
|
* |
73
|
|
|
* Ensure a DateTime instance. |
74
|
|
|
* |
75
|
|
|
* @param mixed $data |
76
|
|
|
* @throws ConverterException |
77
|
|
|
* @return \DateTime |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
protected function checkData($data) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if (!$data instanceof \DateTimeInterface) { |
82
|
|
|
try { |
83
|
|
|
$data = new \DateTime($data); |
84
|
|
|
} catch (\Exception $e) { |
85
|
|
|
throw new ConverterException( |
86
|
|
|
sprintf( |
87
|
|
|
"Cannot convert data from invalid datetime representation '%s'.", |
88
|
|
|
$data |
89
|
|
|
), |
90
|
|
|
null, |
91
|
|
|
$e |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $data; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.