Completed
Push — master ( a08147...ea1ae0 )
by Ondrej
10s
created

MapConverter::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 7
nc 4
nop 1
crap 4
1
<?php
2
namespace SpareParts\Enum\Converter;
3
4
use SpareParts\Enum\Enum;
5
6
/**
7
 * Converts Enum values to custom string values using map
8
 *
9
 * Usage:
10
 * $converter = new MapConverter(
11
 *      [
12
 *          'totally_opened' => WindowState::OPEN(),
13
 *          'totally_closed' => WindowState::CLOSED(),
14
 *      ]
15
 * );
16
 *
17
 * $converter->toEnum('totally_closed') // returns WindowState::CLOSED() instance
18
 *
19
 * $converter->fromEnum(WindowState::CLOSED()) // returns 'totally_closed' string
20
 */
21
class MapConverter implements IConverter
22
{
23
    /**
24
     * @var array mapValue => enumInstance
25
     */
26
    protected $scalarMap = [];
27
28
    /**
29
     * @var array enumString => mapValue
30
     */
31
    protected $enumMap = [];
32
33
    /**
34
     * @param array $map
35
     * @throws ConverterSetupException
36
     */
37 6
    public function __construct(array $map)
38
    {
39 6
        $this->enumMap = $map;
40 6
        foreach ($map as $value => $enum) {
41 6
            if (!($enum instanceof Enum)) {
42 1
                $ident = is_object($enum) ? get_class($enum) : print_r($enum, true);
43 1
                throw new ConverterSetupException(sprintf('Class %s is not a valid Enum. (doesnt extend Enum class)', $ident));
44
            }
45 5
            $this->scalarMap[(string) $enum] = $value;
46 5
        }
47 5
    }
48
49
    /**
50
     * @param mixed $value
51
     * @return Enum
52
     * @throws UnableToConvertException
53
     */
54 3
    public function toEnum($value)
55
    {
56 3
        if (!is_scalar($value)) {
57 1
            throw new UnableToConvertException(sprintf('Unable to convert: Value %s is not a scalar value.', print_r($value, true)));
58
        }
59
60 2 View Code Duplication
        if (!isset($this->enumMap[$value])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
61 1
            throw new UnableToConvertException(sprintf('Unable to convert: Value %s not found in possible options: [%s]', $value, implode(', ', array_keys($this->enumMap))));
62
        }
63
64 1
        return $this->enumMap[$value];
65
    }
66
67
    /**
68
     * @param Enum $enum
69
     * @return string
70
     * @throws UnableToConvertException
71
     */
72 2
    public function fromEnum(Enum $enum)
73
    {
74 2 View Code Duplication
        if (!isset($this->scalarMap[(string) $enum])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75 1
            throw new UnableToConvertException(
76 1
                sprintf(
77 1
                    'Unable to convert: Enum %s::%s() not found in possible options: [%s]',
78 1
                    get_class($enum),
79 1
                    (string) $enum,
80 1
                    implode(', ', array_keys($this->enumMap))
81 1
                )
82 1
            );
83
        }
84
85 1
        return $this->scalarMap[(string) $enum];
86
    }
87
}