Code Duplication    Length = 74-74 lines in 2 locations

sources/lib/Converter/PgTimestamp.php 1 location

@@ 26-99 (lines=74) @@
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
    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
     * @access protected
76
     * @param  mixed $data
77
     * @throws ConverterException
78
     * @return \DateTime
79
     */
80
    protected function checkData($data)
81
    {
82
        if (!$data instanceof \DateTime) {
83
            try {
84
                $data = new \DateTime($data);
85
            } catch (\Exception $e) {
86
                throw new ConverterException(
87
                    sprintf(
88
                        "Cannot convert data from invalid datetime representation '%s'.",
89
                        $data
90
                    ),
91
                    null,
92
                    $e
93
                );
94
            }
95
        }
96
97
        return $data;
98
    }
99
}
100

sources/lib/Converter/PgTimestampChronos.php 1 location

@@ 28-101 (lines=74) @@
25
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
26
 * @see       ConverterInterface
27
 */
28
class PgTimestampChronos implements ConverterInterface
29
{
30
    const TS_FORMAT = 'Y-m-d H:i:s.uP';
31
32
    /**
33
     * fromPg
34
     *
35
     * @see ConverterInterface
36
     */
37
    public function fromPg($data, $type, Session $session)
38
    {
39
        $data = trim($data);
40
41
        return $data !== '' ? new Chronos($data) : null;
42
    }
43
44
    /**
45
     * toPg
46
     *
47
     * @see ConverterInterface
48
     */
49
    public function toPg($data, $type, Session $session)
50
    {
51
        return
52
            $data !== null
53
            ? sprintf("%s '%s'", $type, $this->checkData($data)->format(static::TS_FORMAT))
54
            : sprintf("NULL::%s", $type)
55
            ;
56
    }
57
58
    /**
59
     * toPgStandardFormat
60
     *
61
     * @see ConverterInterface
62
     */
63
    public function toPgStandardFormat($data, $type, Session $session)
64
    {
65
        return
66
            $data !== null
67
            ? $this->checkData($data)->format(static::TS_FORMAT)
68
            : null
69
            ;
70
    }
71
72
    /**
73
     * checkData
74
     *
75
     * Ensure a ChronosInterface instance.
76
     *
77
     * @access protected
78
     * @param  mixed $data
79
     * @throws ConverterException
80
     * @return ChronosInterface
81
     */
82
    protected function checkData($data)
83
    {
84
        if (!$data instanceof ChronosInterface) {
85
            try {
86
                $data = new Chronos($data);
87
            } catch (\Exception $e) {
88
                throw new ConverterException(
89
                    sprintf(
90
                        "Cannot convert data from invalid datetime representation '%s'.",
91
                        $data
92
                    ),
93
                    null,
94
                    $e
95
                );
96
            }
97
        }
98
99
        return $data;
100
    }
101
}
102