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

AbstractEnumCollectionType::convertToPHPValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
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