| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Pim\Bundle\CatalogBundle\Entity\Family; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Pim\Bundle\CatalogBundle\Entity\Repository\AssociationTypeRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class FamilyNormalizer implements NormalizerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var AttributeNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $attributeNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param AttributeNormalizer $attributeNormalizer The entity manager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |       AttributeNormalizer $attributeNormalizer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |       AssociationTypeRepository $associationTypeRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->attributeNormalizer = $attributeNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->associationTypeRepository = $associationTypeRepository; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param  object                                                $family | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param  null                                                  $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param  array                                                 $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return array|\Symfony\Component\Serializer\Normalizer\scalar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @throws Exception\NormalizeException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     public function normalize($family, $format = null, array $context = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         /**@var Family $family * */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $normalizedFamily = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |           'code'             => $family->getCode(), | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |           'labels'           => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |           'attribute_groups' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         foreach ($family->getTranslations() as $trans) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             $normalizedFamily['labels'][$trans->getLocale()] = $trans->getLabel( | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $associations = $this->associationTypeRepository->findAll(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $normalizedFamily['associations'] = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         foreach($associations as $association) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $normalizedFamily['associations'][$association->getCode()] = array(); | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 52 |  | View Code Duplication |             foreach ($association->getTranslations() as $assocTrans) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                 $normalizedFamily['associations'][$association->getCode()]['labels'][$assocTrans->getLocale( | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 )] = $assocTrans->getLabel(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         foreach ($family->getAttributes() as $attr) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $normalizedFamily['attribute_groups'][$attr->getGroup()->getCode( | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             )]['code'] = $attr->getGroup()->getCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             foreach ($attr->getGroup()->getTranslations() as $groupTrans) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 $normalizedFamily['attribute_groups'][$attr->getGroup() | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                   ->getCode()]['labels'][$groupTrans->getLocale( | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 )] = $groupTrans->getLabel(); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             $normalizedFamily['attribute_groups'][$attr->getGroup()->getCode( | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             )]['attributes'][$attr->getCode( | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             )] = $this->attributeNormalizer->normalize($attr, null, $context); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         return $normalizedFamily; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Checks whether the given class is supported for normalization by this normalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param mixed  $data   Data to normalize. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string $format The format being (de-)serialized from or into. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function supportsNormalization($data, $format = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return $data instanceof Family; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: