Passed
Pull Request — master (#23)
by Adrien
17:46 queued 04:32
created

DateType::getSQLDeclaration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
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
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
16 2
    {
17
        return $platform->getDateTypeDeclarationSQL($column);
18 2
    }
19 1
20
    /**
21
     * @return ($value is null ? null : string)
22 2
     */
23 1
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
24
    {
25
        if ($value === null) {
26 1
            return $value;
27
        }
28
29
        if ($value instanceof ChronosDate) {
30
            return $value->format($platform->getDateFormatString());
31
        }
32 2
33
        throw InvalidType::new($value, self::class, ['null', 'ChronosDate']);
34 2
    }
35 1
36
    /**
37
     * @return ($value is null ? null : ChronosDate)
38 2
     */
39 1
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?ChronosDate
40 1
    {
41 1
        if ($value === null || $value instanceof ChronosDate) {
42 1
            return $value;
43 1
        }
44
45
        if (!is_string($value)) {
46 1
            throw InvalidFormat::new(
47
                (string) $value,
48 1
                self::class,
49
                $platform->getDateFormatString(),
50
            );
51
        }
52
53
        $val = new ChronosDate($value);
54
55
        return $val;
56
    }
57
}
58