| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace VSV\GVQ_API\Company\Serializers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ramsey\Uuid\Uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use VSV\GVQ_API\Common\ValueObjects\NotEmptyString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use VSV\GVQ_API\Company\Models\Company; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use VSV\GVQ_API\Company\Models\TranslatedAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use VSV\GVQ_API\Company\Models\TranslatedAliases; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use VSV\GVQ_API\User\Models\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use VSV\GVQ_API\User\Serializers\UserDenormalizer; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class CompanyDenormalizer implements DenormalizerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @var TranslatedAliasDenormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     private $translatedAliasDenormalizer; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @var UserDenormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     private $userDenormalizer; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @param TranslatedAliasDenormalizer $translatedAliasDenormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param UserDenormalizer $userDenormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         TranslatedAliasDenormalizer $translatedAliasDenormalizer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         UserDenormalizer $userDenormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->translatedAliasDenormalizer = $translatedAliasDenormalizer; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |         $this->userDenormalizer = $userDenormalizer; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     public function denormalize($data, $class, $format = null, array $context = array()): Company | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $translatedAliases = array_map( | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             function (array $translatedAlias) use ($format) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                 return $this->translatedAliasDenormalizer->denormalize( | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                     $translatedAlias, | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                     TranslatedAlias::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                     $format | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             }, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $data['aliases'] | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $user = $this->userDenormalizer->denormalize( | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $data['user'], | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             User::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $format, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $context | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         // TODO: Better to use decorator and inject uuid generator. | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         if (!isset($data['id'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             $data['id'] = Uuid::uuid4(); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         return new Company( | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             Uuid::fromString($data['id']), | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             new NotEmptyString($data['name']), | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             new TranslatedAliases(...$translatedAliases), | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             $user | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function supportsDenormalization($data, $type, $format = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |         return ($type === Company::class) && ($format === 'json'); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |