Completed
Pull Request — 2.0 (#75)
by Julien
02:03
created

PgInterval::toPg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
/*
3
 * This file is part of the Pomm package.
4
 *
5
 * (c) 2014 - 2015 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 - 2015 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(sprintf("Data '%s' is not an ISO8601 interval representation.", $data), null, $e);
40
        }
41
    }
42
43
    /**
44
     * @see ConverterInterface
45
     */
46 View Code Duplication
    public function toPg($data, $type, Session $session)
47
    {
48
        return $data !== null
49
            ? sprintf("%s '%s'", $type, $this->checkData($data)->format('%Y years %M months %D days %H:%i:%S'))
50
            : sprintf("NULL::%s", $type)
51
            ;
52
    }
53
54
55
    /**
56
     * @see ConverterInterface
57
     */
58 View Code Duplication
    public function toPgStandardFormat($data, $type, Session $session)
59
    {
60
        return $data !== null
61
            ? sprintf('"%s"', $this->checkData($data)->format('%Y years %M months %D days %H:%i:%S'))
62
            : null
63
            ;
64
    }
65
66
    /**
67
     * checkData
68
     *
69
     * Check if Data is a DateInterval. If not, it tries to instantiate a
70
     * DateInterval with the given data.
71
     *
72
     * @access protected
73
     * @param  mixed $data
74
     * @throws ConverterException
75
     * @return \DateInterval $data
76
     */
77 View Code Duplication
    protected function checkData($data)
78
    {
79
        if (!$data instanceof \DateInterval) {
80
            try {
81
                $data = new \DateInterval($data);
82
            } catch (\Exception $e) {
83
                throw new ConverterException("First argument is not a \DateInterval instance.", null, $e);
84
            }
85
        }
86
87
        return $data;
88
    }
89
}
90