1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm 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\Exception\ConverterException; |
13
|
|
|
use PommProject\Foundation\Session\Session; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PgInterval |
17
|
|
|
* |
18
|
|
|
* Convert an ISO8601 interval from/to PHP. |
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
|
|
|
*/ |
25
|
|
|
class PgInterval implements ConverterInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @see ConverterInterface |
29
|
|
|
*/ |
30
|
|
|
public function fromPg($data, $type, Session $session) |
31
|
|
|
{ |
32
|
|
|
if (trim($data) === '') { |
33
|
|
|
return null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
return new \DateInterval(preg_replace('/\.[0-9]+S/', 'S', $data)); |
38
|
|
|
} catch (\Exception $e) { |
39
|
|
|
throw new ConverterException( |
40
|
|
|
sprintf("Data '%s' is not an ISO8601 interval representation.", $data), |
41
|
|
|
null, |
42
|
|
|
$e |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @see ConverterInterface |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function toPg($data, $type, Session $session) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
return $data !== null |
53
|
|
|
? sprintf("%s '%s'", $type, $this->checkData($data)->format('%Y years %M months %D days %H:%i:%S')) |
54
|
|
|
: sprintf("NULL::%s", $type) |
55
|
|
|
; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @see ConverterInterface |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function toPgStandardFormat($data, $type, Session $session) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
return $data !== null |
65
|
|
|
? sprintf('"%s"', $this->checkData($data)->format('%Y years %M months %D days %H:%i:%S')) |
66
|
|
|
: null |
67
|
|
|
; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* checkData |
72
|
|
|
* |
73
|
|
|
* Check if Data is a DateInterval. If not, it tries to instantiate a |
74
|
|
|
* DateInterval with the given data. |
75
|
|
|
* |
76
|
|
|
* @param mixed $data |
77
|
|
|
* @throws ConverterException |
78
|
|
|
* @return \DateInterval $data |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
protected function checkData($data) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
if (!$data instanceof \DateInterval) { |
83
|
|
|
try { |
84
|
|
|
$data = new \DateInterval($data); |
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
throw new ConverterException("First argument is not a \DateInterval instance.", null, $e); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $data; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.