Completed
Pull Request — master (#21)
by Peter
01:27
created

BaseType::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2016, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Interval\Persistence\Doctrine\DBAL\Types;
12
13
use Doctrine\DBAL\Platforms\AbstractPlatform;
14
use Doctrine\DBAL\Types\ConversionException;
15
use Doctrine\DBAL\Types\TextType;
16
use GpsLab\Component\Interval\Exception\InvalidIntervalFormatException;
17
use GpsLab\Component\Interval\IntervalInterface;
18
19
abstract class BaseType extends TextType
20
{
21
    /**
22
     * @param IntervalInterface|null $value
23
     * @param AbstractPlatform $platform
24
     *
25
     * @return null|string
26
     */
27 18
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
28
    {
29 18
        $class = $this->getIntervalClass();
30
31 18
        return $value instanceof $class ? (string) $value : null;
32
    }
33
34
    /**
35
     * @throws ConversionException
36
     *
37
     * @param mixed $value
38
     * @param AbstractPlatform $platform
39
     *
40
     * @return null|IntervalInterface
41
     */
42 18
    public function convertToPHPValue($value, AbstractPlatform $platform)
43
    {
44 18
        if ($value === null) {
45 9
            return null;
46
        }
47
48
        try {
49 9
            return call_user_func([$this->getIntervalClass(), 'fromString'], $value);
50
        } catch (InvalidIntervalFormatException $e) {
51
            throw ConversionException::conversionFailed($value, $this->getName());
52
        }
53
    }
54
55
    /**
56
     * @return string
57
     */
58 9
    public function getName()
59
    {
60 9
        $names = explode('\\', $this->getIntervalClass());
61
62 9
        return array_pop($names);
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function requiresSQLCommentHint(AbstractPlatform $platform)
69
    {
70
        return true;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    abstract protected function getIntervalClass();
77
}
78