Completed
Branch master (adbf7c)
by Ondrej
08:25
created

BitMaskConverter::convertFromEnumSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
namespace SpareParts\Enum\Set\Converter;
3
4
use SpareParts\Enum\Enum;
5
use SpareParts\Enum\Exception\EnumSetMustContainEnumsException;
6
use SpareParts\Enum\Set\ISet;
7
use SpareParts\Enum\Set\ImmutableSet;
8
9
class BitMaskConverter implements IEnumSetConverter
10
{
11
    /**
12
     * @var string
13
     */
14
    private $enumClass;
15
16
    /**
17
     * @var array
18
     */
19
    private $mapping;
20
21
    /**
22
     * @param Enum[] $enumValuesMap
23
     */
24
    public function __construct($enumValuesMap)
25
    {
26
        if (!is_array($enumValuesMap) || !count($enumValuesMap)) {
27
            throw new EnumSetMustContainEnumsException('You have to provide converter mapping.');
28
        }
29
        $this->mapping = $this->prepareInnerMapping($enumValuesMap);
30
    }
31
32
    /**
33
     * @param int $values
34
     * @return ISet
35
     */
36 View Code Duplication
    public function convertToEnumSet($values)
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...
37
    {
38
        $bitValue = (int) $values;
39
40
        $set = [];
41
        foreach ($this->mapping as $value => $bit) {
42
            if ($bitValue & $bit) {
43
                $set[] = call_user_func([$this->enumClass, 'instance'], $value);
44
            }
45
        }
46
        return new ImmutableSet($this->enumClass, $set);
47
    }
48
49
    /**
50
     * @param ISet $enumSet
51
     * @return int
52
     */
53
    public function convertFromEnumSet(ISet $enumSet)
54
    {
55
        $bitValue = 0;
56
        /** @var Enum $value */
57
        foreach ($enumSet as $value) {
58
            $bitValue = $bitValue | $this->mapping[(string) $value];
59
        }
60
        return $bitValue;
61
    }
62
63
    /**
64
     * @param $enumValuesMap
65
     * @return array
66
     */
67
    private function prepareInnerMapping($enumValuesMap)
68
    {
69
        // translate enum values to bit values mapping
70
        $innerMapping = [];
71
        $shift = 0;
72
        foreach ($enumValuesMap as $value) {
73
            $this->fetchEnumClass($value);
74
75
            $innerMapping[(string)$value] = 1 << $shift;
76
            $shift++;
77
        }
78
        return $innerMapping;
79
    }
80
81
    /**
82
     * @param $value
83
     */
84
    private function fetchEnumClass($value)
85
    {
86
        // try to guess enum class
87
        if (is_null($this->enumClass)) {
88
            $this->setEnumClass($value);
89
        }
90
        if (!is_object($value) || !($value instanceof $this->enumClass)) {
91
            throw new EnumSetMustContainEnumsException(sprintf("Expected `%s`, got `%s`", $this->enumClass, $this->printVar($value)));
92
        }
93
    }
94
95
    /**
96
     * @param mixed $enum
97
     */
98
    private function setEnumClass($enum)
99
    {
100
        if (!is_object($enum) || !is_subclass_of($enum, Enum::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \SpareParts\Enum\Enum::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
101
            throw new EnumSetMustContainEnumsException(sprintf("Class %s does not implement Enum (as it should).", $this->printVar($enum)));
102
        }
103
        $this->enumClass = get_class($enum);
104
    }
105
106
    /**
107
     * @param mixed $value
108
     * @return string
109
     */
110
    private function printVar($value)
111
    {
112
        return is_object($value) ? get_class($value) : var_export($value, true);
113
    }
114
}
115