Code Duplication    Length = 74-74 lines in 3 locations

sources/lib/Converter/PgDateChronos.php 1 location

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

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