Failed Conditions
Pull Request — master (#23)
by Adrien
03:02
created

DateType::getSQLDeclaration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\Types;
6
7
use Cake\Chronos\ChronosDate;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Types\Exception\InvalidFormat;
10
use Doctrine\DBAL\Types\Exception\InvalidType;
11
use Doctrine\DBAL\Types\Type;
12
13
final class DateType extends Type
14
{
15 1
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
16
    {
17 1
        return $platform->getDateTypeDeclarationSQL($column);
18
    }
19
20
    /**
21
     * @return ($value is null ? null : string)
22
     */
23 2
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
24
    {
25 2
        if ($value === null) {
26 1
            return $value;
27
        }
28
29 2
        if ($value instanceof ChronosDate) {
30 1
            return $value->format($platform->getDateFormatString());
31
        }
32
33 1
        throw InvalidType::new($value, self::class, ['null', 'ChronosDate']);
34
    }
35
36
    /**
37
     * @return ($value is null ? null : ChronosDate)
38
     */
39 2
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?ChronosDate
40
    {
41 2
        if ($value === null || $value instanceof ChronosDate) {
42 1
            return $value;
43
        }
44
45 2
        if (!is_string($value)) {
46 1
            throw InvalidFormat::new(
47 1
                (string) $value,
48 1
                self::class,
49 1
                $platform->getDateFormatString(),
50 1
            );
51
        }
52
53 1
        $val = new ChronosDate($value);
54
55 1
        return $val;
56
    }
57
}
58