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
|
14 |
|
public function __construct($enumValuesMap) |
25
|
|
|
{ |
26
|
14 |
|
if (!is_array($enumValuesMap) || !count($enumValuesMap)) { |
27
|
2 |
|
throw new EnumSetMustContainEnumsException('You have to provide converter mapping.'); |
28
|
|
|
} |
29
|
12 |
|
$this->mapping = $this->prepareInnerMapping($enumValuesMap); |
30
|
9 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $values |
34
|
|
|
* @return ISet |
35
|
|
|
*/ |
36
|
5 |
View Code Duplication |
public function convertToEnumSet($values) |
|
|
|
|
37
|
|
|
{ |
38
|
5 |
|
$bitValue = (int) $values; |
39
|
|
|
|
40
|
5 |
|
$set = []; |
41
|
5 |
|
foreach ($this->mapping as $value => $bit) { |
42
|
5 |
|
if ($bitValue & $bit) { |
43
|
4 |
|
$set[] = call_user_func([$this->enumClass, 'instance'], $value); |
44
|
4 |
|
} |
45
|
5 |
|
} |
46
|
5 |
|
return new ImmutableSet($this->enumClass, $set); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param ISet $enumSet |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
4 |
|
public function convertFromEnumSet(ISet $enumSet) |
54
|
|
|
{ |
55
|
4 |
|
$bitValue = 0; |
56
|
|
|
/** @var Enum $value */ |
57
|
4 |
|
foreach ($enumSet as $value) { |
58
|
3 |
|
$bitValue = $bitValue | $this->mapping[(string) $value]; |
59
|
4 |
|
} |
60
|
4 |
|
return $bitValue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $enumValuesMap |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
12 |
|
private function prepareInnerMapping($enumValuesMap) |
68
|
|
|
{ |
69
|
|
|
// translate enum values to bit values mapping |
70
|
12 |
|
$innerMapping = []; |
71
|
12 |
|
$shift = 0; |
72
|
12 |
|
foreach ($enumValuesMap as $value) { |
73
|
12 |
|
$this->fetchEnumClass($value); |
74
|
|
|
|
75
|
11 |
|
$innerMapping[(string)$value] = 1 << $shift; |
76
|
11 |
|
$shift++; |
77
|
11 |
|
} |
78
|
9 |
|
return $innerMapping; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $value |
83
|
|
|
*/ |
84
|
12 |
|
private function fetchEnumClass($value) |
85
|
|
|
{ |
86
|
|
|
// try to guess enum class |
87
|
12 |
|
if (is_null($this->enumClass)) { |
88
|
12 |
|
$this->setEnumClass($value); |
89
|
11 |
|
} |
90
|
11 |
|
if (!is_object($value) || !($value instanceof $this->enumClass)) { |
91
|
2 |
|
throw new EnumSetMustContainEnumsException(sprintf("Expected `%s`, got `%s`", $this->enumClass, $this->printVar($value))); |
92
|
|
|
} |
93
|
11 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param mixed $enum |
97
|
|
|
*/ |
98
|
12 |
|
private function setEnumClass($enum) |
99
|
|
|
{ |
100
|
12 |
|
if (!is_object($enum) || !is_subclass_of($enum, Enum::class)) { |
|
|
|
|
101
|
1 |
|
throw new EnumSetMustContainEnumsException(sprintf("Class %s does not implement Enum (as it should).", $this->printVar($enum))); |
102
|
|
|
} |
103
|
11 |
|
$this->enumClass = get_class($enum); |
104
|
11 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $value |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
3 |
|
private function printVar($value) |
111
|
|
|
{ |
112
|
3 |
|
return is_object($value) ? get_class($value) : var_export($value, true); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.