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 { [enumValue: string]: Enum } |
||||
35 | * @throws ConverterSetupException |
||||
36 | */ |
||||
37 | 7 | public function __construct(array $map) |
|||
38 | { |
||||
39 | 7 | $this->enumMap = $map; |
|||
40 | 7 | foreach ($map as $value => $enum) { |
|||
41 | 7 | 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)); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
44 | } |
||||
45 | 6 | $this->scalarMap[(string) $enum] = $value; |
|||
46 | } |
||||
47 | 6 | } |
|||
48 | |||||
49 | /** |
||||
50 | * @param mixed $value |
||||
51 | * @return Enum |
||||
52 | * @throws UnableToConvertException |
||||
53 | */ |
||||
54 | 3 | public function toEnum($value): Enum |
|||
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))); |
|||
0 ignored issues
–
show
It seems like
print_r($value, true) can also be of type true ; however, parameter $values of sprintf() does only seem to accept double|integer|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
58 | } |
||||
59 | |||||
60 | 2 | if (!isset($this->enumMap[$value])) { |
|||
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 mixed |
||||
70 | * @throws UnableToConvertException |
||||
71 | */ |
||||
72 | 3 | public function fromEnum(Enum $enum) |
|||
73 | { |
||||
74 | 3 | if (!isset($this->scalarMap[(string) $enum])) { |
|||
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 | ) |
||||
82 | ); |
||||
83 | } |
||||
84 | |||||
85 | 2 | return $this->scalarMap[(string) $enum]; |
|||
86 | } |
||||
87 | } |