| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace JMS\Serializer\Handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use JMS\Serializer\Exception\InvalidMetadataException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use JMS\Serializer\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use JMS\Serializer\GraphNavigatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use JMS\Serializer\SerializationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use JMS\Serializer\Visitor\DeserializationVisitorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use JMS\Serializer\Visitor\SerializationVisitorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | final class EnumHandler implements SubscribingHandlerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public static function getSubscribingMethods() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $methods = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         foreach (['json', 'xml'] as $format) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             $methods[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                 'type' => 'enum', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 'format' => $format, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 'method' => 'deserializeEnum', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $methods[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 'type' => 'enum', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'format' => $format, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 'method' => 'serializeEnum', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return $methods; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function serializeEnum( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         SerializationVisitorInterface $visitor, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         \UnitEnum $enum, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         array $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         SerializationContext $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (isset($type['params'][1]) && 'value' === $type['params'][1]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             if (!$enum instanceof \BackedEnum) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 throw new InvalidMetadataException(sprintf('The type "%s" is not a backed enum, thus you can not use "value" as serialization mode for its value.', get_class($enum))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $valueType = isset($type['params'][2]) ? ['name' => $type['params'][2]] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return $context->getNavigator()->accept($enum->value, $valueType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             return $context->getNavigator()->accept($enum->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param int|string|\SimpleXMLElement $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param array $type | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function deserializeEnum(DeserializationVisitorInterface $visitor, $data, array $type): ?\UnitEnum | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $enumType = $type['params'][0]; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $caseValue = (string) $data; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $ref = new \ReflectionEnum($enumType); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         if (isset($type['params'][1]) && 'value' === $type['params'][1] || (!isset($type['params'][1]) && is_a($enumType, \BackedEnum::class, true))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             if (!is_a($enumType, \BackedEnum::class, true)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                 throw new InvalidMetadataException(sprintf('The type "%s" is not a backed enum, thus you can not use "value" as serialization mode for its value.', $enumType)); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             if ('int' === $ref->getBackingType()->getName()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 if (!is_numeric($caseValue)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                     throw new RuntimeException(sprintf('"%s" is not a valid backing value for enum "%s"', $caseValue, $enumType)); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 $caseValue = (int) $caseValue; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             return $enumType::from($caseValue); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             if (!$ref->hasCase($caseValue)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                 throw new InvalidMetadataException(sprintf('The type "%s" does not have the case "%s"', $ref->getName(), $caseValue)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             return $ref->getCase($caseValue)->getValue(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                        
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.