Completed
Pull Request — master (#2795)
by Marco
04:44
created

DateTimeTzType::convertToPHPValue()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 7
cts 7
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 3
nop 2
crap 5
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Types;
21
22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23
24
/**
25
 * DateTime type saving additional timezone information.
26
 *
27
 * Caution: Databases are not necessarily experts at storing timezone related
28
 * data of dates. First, of all the supported vendors only PostgreSQL and Oracle
29
 * support storing Timezone data. But those two don't save the actual timezone
30
 * attached to a DateTime instance (for example "Europe/Berlin" or "America/Montreal")
31
 * but the current offset of them related to UTC. That means depending on daylight saving times
32
 * or not you may get different offsets.
33
 *
34
 * This datatype makes only sense to use, if your application works with an offset, not
35
 * with an actual timezone that uses transitions. Otherwise your DateTime instance
36
 * attached with a timezone such as Europe/Berlin gets saved into the database with
37
 * the offset and re-created from persistence with only the offset, not the original timezone
38
 * attached.
39
 *
40
 * @link   www.doctrine-project.org
41
 * @since  1.0
42
 * @author Benjamin Eberlei <[email protected]>
43
 * @author Guilherme Blanco <[email protected]>
44
 * @author Jonathan Wage <[email protected]>
45
 * @author Roman Borschel <[email protected]>
46
 */
47 View Code Duplication
class DateTimeTzType extends Type
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...
48
{
49
    /**
50
     * {@inheritdoc}
51
     */
52 45
    public function getName()
53
    {
54 45
        return Type::DATETIMETZ;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 15
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
61
    {
62 15
        return $platform->getDateTimeTzTypeDeclarationSQL($fieldDeclaration);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 17
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
69
    {
70 17
        if (null === $value) {
71
            return $value;
72
        }
73
74 17
        if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
75 4
            return $value->format($platform->getDateTimeTzFormatString());
76
        }
77
78 13
        throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 6
    public function convertToPHPValue($value, AbstractPlatform $platform)
85
    {
86 6
        if ($value === null || $value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
87 3
            return $value;
88
        }
89
90 3
        $val = \DateTime::createFromFormat($platform->getDateTimeTzFormatString(), $value);
91 3
        if ( ! $val) {
92 1
            throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeTzFormatString());
93
        }
94
95 2
        return $val;
96
    }
97
}
98