Completed
Pull Request — master (#81)
by
unknown
04:08
created

PgDateChronos::toPgStandardFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
/*
3
 * This file is part of PommProject's Foundation 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 Cake\Chronos\Date;
13
use PommProject\Foundation\Session\Session;
14
use PommProject\Foundation\Exception\ConverterException;
15
16
/**
17
 * PgDateChronos
18
 *
19
 * Date converter
20
 *
21
 * @package   Foundation
22
 * @copyright 2017 Grégoire HUBERT
23
 * @author    Miha Vrhovnik
24
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
25
 * @see       ConverterInterface
26
 */
27 View Code Duplication
class PgDateChronos implements ConverterInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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