1 | <?php |
||
2 | |||
3 | namespace Bdf\Serializer\Normalizer; |
||
4 | |||
5 | use Bdf\Serializer\Context\DenormalizationContext; |
||
6 | use Bdf\Serializer\Context\NormalizationContext; |
||
7 | use Bdf\Serializer\Exception\UnexpectedValueException; |
||
8 | use Bdf\Serializer\Type\Type; |
||
9 | use Closure; |
||
10 | use SuperClosure\Analyzer\TokenAnalyzer; |
||
11 | use SuperClosure\Exception\SuperClosureException; |
||
12 | use SuperClosure\Serializer; |
||
13 | use SuperClosure\SerializerInterface; |
||
14 | |||
15 | /** |
||
16 | * ClosureNormalizer |
||
17 | * |
||
18 | * @author Seb |
||
19 | * |
||
20 | * @implements NormalizerInterface<\Closure> |
||
21 | */ |
||
22 | class ClosureNormalizer implements NormalizerInterface, AutoRegisterInterface |
||
23 | { |
||
24 | /** |
||
25 | * The closure serializer |
||
26 | * |
||
27 | * @var SerializerInterface |
||
28 | */ |
||
29 | private $serializer; |
||
30 | |||
31 | /** |
||
32 | * ClosureNormalizer constructor. |
||
33 | * |
||
34 | * @param SerializerInterface $serializer |
||
35 | */ |
||
36 | 8 | public function __construct(SerializerInterface $serializer = null) |
|
37 | { |
||
38 | 8 | $this->serializer = $serializer ?: new Serializer(new TokenAnalyzer()); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | * |
||
44 | * @throws UnexpectedValueException If the closure could not be serialize |
||
45 | * @psalm-suppress InvalidCatch |
||
46 | */ |
||
47 | 2 | public function normalize($data, NormalizationContext $context) |
|
48 | { |
||
49 | try { |
||
50 | 2 | return $this->serializer->serialize($data); |
|
51 | } catch (SuperClosureException $e) { |
||
52 | throw new UnexpectedValueException('Could not normalize closure object', 0, $e); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | * |
||
59 | * @throws UnexpectedValueException If the closure could not be unserialize |
||
60 | * @psalm-suppress InvalidCatch |
||
61 | */ |
||
62 | 2 | public function denormalize($data, Type $type, DenormalizationContext $context) |
|
63 | { |
||
64 | try { |
||
65 | 2 | return $this->serializer->unserialize($data); |
|
0 ignored issues
–
show
|
|||
66 | } catch (SuperClosureException $e) { |
||
67 | throw new UnexpectedValueException('Could not denormalize closure object', 0, $e); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 2 | public function supports(string $className): bool |
|
75 | { |
||
76 | 2 | return $className === Closure::class; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 6 | public function registerTo(NormalizerLoaderInterface $loader): void |
|
83 | { |
||
84 | 6 | $loader->associate(Closure::class, $this); |
|
85 | } |
||
86 | } |
||
87 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: