Completed
Push — master ( 89600f...8b2509 )
by Maxime
01:55 queued 11s
created

AbstractEnumCollectionType   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 26
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToDatabaseValue() 0 8 2
A convertToPHPValue() 0 12 2
getEnumClass() 0 1 ?
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the "elao/enum" package.
7
 *
8
 * Copyright (C) Elao
9
 *
10
 * @author Elao <[email protected]>
11
 */
12
13
namespace Elao\Enum\Bridge\Doctrine\DBAL\Types;
14
15
use Doctrine\DBAL\Platforms\AbstractPlatform;
16
use Doctrine\DBAL\Types\JsonType;
17
use Elao\Enum\Enum;
18
19
abstract class AbstractEnumCollectionType extends JsonType
20
{
21
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
22
    {
23
        if (\is_array($value)) {
24
            $value = array_map(function (Enum $enum) { return $enum->getValue(); }, $value);
25
        }
26
27
        return parent::convertToDatabaseValue(array_values($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(function ($value) {
36
                return $this->getEnumClass()::get($value);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getEnumClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
37
            }, $values);
38
        }
39
40
        return $values;
41
    }
42
43
    abstract protected function getEnumClass(): string;
44
}
45