Completed
Push — master ( c18e77...cd2879 )
by Maxime
13s queued 11s
created

convertToPHPValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
    abstract protected function getEnumClass(): string;
42
}
43