TimestampType::convertToDatabaseValue()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 3
nc 4
nop 2
crap 3
1
<?php
2
3
/**
4
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
5
 * license agreements.  See the NOTICE file distributed with this work for
6
 * additional information regarding copyright ownership.  Crate licenses
7
 * this file to you under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.  You may
9
 * obtain a copy of the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
16
 * License for the specific language governing permissions and limitations
17
 * under the License.
18
 *
19
 * However, if you have executed another commercial license agreement
20
 * with Crate these terms will supersede the license and you may use the
21
 * software solely pursuant to the terms of the relevant commercial agreement.
22
 */
23
24
namespace Crate\DBAL\Types;
25
26
use DateTime;
27
use Doctrine\DBAL\Platforms\AbstractPlatform;
28
use Doctrine\DBAL\Types\Type;
29
30
/**
31
 * Type that maps a Crate SQL TIMESTAMP (aka Long) to a PHP DateTime object.
32
 */
33
34
class TimestampType extends Type
35
{
36
    public const NAME = 'timestamp';
37
    public const S_TO_MS = 1000;
38
39
    /**
40
     * Gets the name of this type.
41
     *
42
     * @return string
43
     */
44 46
    public function getName()
45
    {
46 46
        return self::NAME;
47
    }
48
49 42
    public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed
50
    {
51 42
        return ($value !== null && $value instanceof DateTime)
52 42
            ? $value->getTimestamp() * self::S_TO_MS : null;
53
    }
54
55 6
    public function convertToPHPValue($value, AbstractPlatform $platform): mixed
56
    {
57 6
        if ($value === null || $value instanceof DateTime) {
58 4
            return $value;
59
        }
60
61 4
        if (!is_int($value)) {
62
            return null;
63
        }
64
65 4
        $val = new DateTime();
66 4
        $val->setTimestamp((int) ($value / self::S_TO_MS));
67
68 4
        return $val;
69
    }
70
71
    /**
72
     * Gets the SQL declaration snippet for a field of this type.
73
     *
74
     * @return string
75
     * @param  array            $fieldDeclaration The field declaration.
76
     * @param  AbstractPlatform $platform         The currently used database platform.
77
     */
78 46
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
79
    {
80 46
        return $platform->getDateTimeTypeDeclarationSQL($fieldDeclaration);
81
    }
82
}
83