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

TimeType::convertToPHPValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 9.9666
cc 4
nc 3
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\Types;
6
7
use Cake\Chronos\ChronosTime;
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 TimeType extends Type
14
{
15
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
16 2
    {
17
        return $platform->getTimeTypeDeclarationSQL($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 ChronosTime) {
30
            return $value->format($platform->getTimeFormatString());
31
        }
32 2
33
        throw InvalidType::new($value, self::class, ['null', 'ChronosTime']);
34 2
    }
35 1
36
    /**
37
     * @return ($value is null ? null : ChronosTime)
38 2
     */
39 1
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?ChronosTime
40 1
    {
41 1
        if ($value === null || $value instanceof ChronosTime) {
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->getTimeFormatString(),
50
            );
51
        }
52
53
        $val = new ChronosTime($value);
54
55
        return $val;
56
    }
57
}
58