|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the "elao/enum" package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Elao |
|
7
|
|
|
* |
|
8
|
|
|
* @author Elao <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Elao\Enum\Bridge\Symfony\Form\DataTransformer; |
|
12
|
|
|
|
|
13
|
|
|
use Elao\Enum\EnumInterface; |
|
14
|
|
|
use Elao\Enum\Exception\InvalidValueException; |
|
15
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
16
|
|
|
use Symfony\Component\Form\Exception\InvalidArgumentException; |
|
17
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Enumerated values to enum instances transformer. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ValueToEnumTransformer implements DataTransformerInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var string|EnumInterface */ |
|
25
|
|
|
private $enumClass; |
|
26
|
|
|
|
|
27
|
|
View Code Duplication |
public function __construct(string $enumClass) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
if (!is_a($enumClass, EnumInterface::class, true)) { |
|
30
|
|
|
throw new InvalidArgumentException(sprintf( |
|
31
|
|
|
'"%s" is not an instance of "%s"', |
|
32
|
|
|
$enumClass, |
|
33
|
|
|
EnumInterface::class |
|
34
|
|
|
)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->enumClass = $enumClass; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Transforms EnumInterface object to a raw enumerated value. |
|
42
|
|
|
* |
|
43
|
|
|
* @param EnumInterface $value EnumInterface instance |
|
44
|
|
|
* |
|
45
|
|
|
* @throws TransformationFailedException When the transformation fails |
|
46
|
|
|
* |
|
47
|
|
|
* @return int|string Value of EnumInterface |
|
48
|
|
|
*/ |
|
49
|
|
View Code Duplication |
public function transform($value) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
if ($value === null) { |
|
52
|
|
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!$value instanceof $this->enumClass) { |
|
56
|
|
|
throw new TransformationFailedException(sprintf( |
|
57
|
|
|
'Expected instance of "%s". Got "%s".', |
|
58
|
|
|
$this->enumClass, |
|
59
|
|
|
\is_object($value) ? \get_class($value) : \gettype($value) |
|
60
|
|
|
)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $value->getValue(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Transforms a raw enumerated value to an enumeration instance. |
|
68
|
|
|
* |
|
69
|
|
|
* @param int|string $value Value accepted by EnumInterface |
|
70
|
|
|
* |
|
71
|
|
|
* @throws TransformationFailedException When the transformation fails |
|
72
|
|
|
* |
|
73
|
|
|
* @return EnumInterface|null A single EnumInterface instance or null |
|
74
|
|
|
*/ |
|
75
|
|
|
public function reverseTransform($value) |
|
76
|
|
|
{ |
|
77
|
|
|
if ($value === null) { |
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
try { |
|
82
|
|
|
return $this->enumClass::get($value); |
|
83
|
|
|
} catch (InvalidValueException $exception) { |
|
84
|
|
|
throw new TransformationFailedException($exception->getMessage(), $exception->getCode(), $exception); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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.