Completed
Push — master ( baf616...5b5714 )
by Maxime
26s queued 11s
created

requiresSQLCommentHint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum\Bridge\Doctrine\DBAL\Types;
12
13
use Doctrine\DBAL\Platforms\AbstractPlatform;
14
use Doctrine\DBAL\Types\JsonType;
15
use Elao\Enum\EnumInterface;
16
17
abstract class AbstractJsonCollectionEnumType extends JsonType
18
{
19 View Code Duplication
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        if (\is_array($value)) {
22
            $value = array_unique(array_values(array_map(static function (EnumInterface $enum) {
23
                return $enum->getValue();
24
            }, $value)));
25
        }
26
27
        return parent::convertToDatabaseValue($value, $platform);
28
    }
29
30
    public function convertToPHPValue($value, AbstractPlatform $platform)
31
    {
32
        $values = parent::convertToPHPValue($value, $platform);
33
34
        if (\is_array($values)) {
35
            $values = array_map([$this->getEnumClass(), 'get'], array_unique(array_values($values)));
36
        }
37
38
        return $values;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function requiresSQLCommentHint(AbstractPlatform $platform)
45
    {
46
        return true;
47
    }
48
49
    abstract protected function getEnumClass(): string;
50
}
51