Code Duplication    Length = 42-45 lines in 2 locations

src/Types/JsonApiDateTimeType.php 1 location

@@ 28-72 (lines=45) @@
25
/**
26
 * @package Limoncello\Flute
27
 */
28
class JsonApiDateTimeType extends DateTimeType
29
{
30
    /** Type name */
31
    const NAME = 'limoncelloDateTime';
32
33
    /** DateTime format */
34
    const JSON_API_FORMAT = DateBaseType::JSON_API_FORMAT;
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function convertToPHPValue($value, AbstractPlatform $platform)
40
    {
41
        $dateTimeOrNot = parent::convertToPHPValue($value, $platform);
42
43
        return $dateTimeOrNot instanceof DateTimeInterface ? new JsonApiDateTime($dateTimeOrNot) : $dateTimeOrNot;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     *
49
     * @SuppressWarnings(PHPMD.StaticAccess)
50
     * @SuppressWarnings(PHPMD.ElseExpression)
51
     */
52
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
53
    {
54
        if ($value instanceof DateTimeInterface || $value === null) {
55
            $dateTime = $value;
56
        } elseif ($value instanceof JsonApiDateTime) {
57
            $dateTime = $value->getValue();
58
        } elseif (is_string($value) === true) {
59
            if (($dateTime = DateTime::createFromFormat(self::JSON_API_FORMAT, $value)) === false) {
60
                throw ConversionException::conversionFailed($value, $this->getName());
61
            }
62
        } else {
63
            throw ConversionException::conversionFailedInvalidType(
64
                $value,
65
                DateTimeInterface::class,
66
                [DateTimeInterface::class, JsonApiDateTime::class, 'string']
67
            );
68
        }
69
70
        return parent::convertToDatabaseValue($dateTime, $platform);
71
    }
72
}
73

src/Types/JsonApiDateType.php 1 location

@@ 28-69 (lines=42) @@
25
/**
26
 * @package Limoncello\Flute
27
 */
28
class JsonApiDateType extends DateType
29
{
30
    /** Type name */
31
    const NAME = 'limoncelloDate';
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function convertToPHPValue($value, AbstractPlatform $platform)
37
    {
38
        $dateOrNot = parent::convertToPHPValue($value, $platform);
39
40
        return $dateOrNot instanceof DateTimeInterface ? new JsonApiDateTime($dateOrNot) : $dateOrNot;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     *
46
     * @SuppressWarnings(PHPMD.StaticAccess)
47
     * @SuppressWarnings(PHPMD.ElseExpression)
48
     */
49
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
50
    {
51
        if ($value instanceof DateTimeInterface || $value === null) {
52
            $date = $value;
53
        } elseif ($value instanceof JsonApiDateTime) {
54
            $date = $value->getValue();
55
        } elseif (is_string($value) === true) {
56
            if (($date = DateTime::createFromFormat(JsonApiDateTimeType::JSON_API_FORMAT, $value)) === false) {
57
                throw ConversionException::conversionFailed($value, $this->getName());
58
            }
59
        } else {
60
            throw ConversionException::conversionFailedInvalidType(
61
                $value,
62
                DateTimeInterface::class,
63
                [DateTimeInterface::class, JsonApiDateTime::class, 'string']
64
            );
65
        }
66
67
        return parent::convertToDatabaseValue($date, $platform);
68
    }
69
}
70