| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dontdrinkandroot\RestBundle\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\Common\Collections\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\Common\Util\ClassUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Metadata\MetadataFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Normalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @var MetadataFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $metadataFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 10 |  |     function __construct(MetadataFactoryInterface $metadataFactory) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 10 |  |         $this->metadataFactory = $metadataFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param mixed    $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string[] $includes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param int      $depth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param string   $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 10 |  |     public function normalize($data, $includes = [], int $depth = 0, string $path = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 10 |  |         if (is_array($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 4 |  |             $normalizedData = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 4 |  |             foreach ($data as $datum) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 4 |  |                 $normalizedData[] = $this->normalize($datum, $includes, $depth + 1, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 4 |  |             return $normalizedData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 10 |  |         if (is_object($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 10 |  |             $normalizedData = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             /** @var ClassMetadata $classMetadata */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 10 |  |             $classMetadata = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($data)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             /** @var PropertyMetadata $propertyMetadatum */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 10 |  |             foreach ($classMetadata->propertyMetadata as $propertyMetadatum) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 10 |  |                 if ($propertyMetadatum->isExcluded()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 6 |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 10 |  |                 if ($propertyMetadatum->isAssociation()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     /* Inlude if includable AND it is on include path */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     if ($propertyMetadatum->isIncludable() && $this->isIncluded( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                             $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                             $propertyMetadatum->getIncludablePaths(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                             $includes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                         $value = $propertyMetadatum->getValue($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                         if ($propertyMetadatum->isCollection()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                             /** @var Collection $value */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                             $value = $value->getValues(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                         $normalizedData[$propertyMetadatum->name] = $this->normalize( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                             $value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                             $includes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                             $depth + 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                             $this->appendPath($path, $propertyMetadatum->name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     /* Inlude if includable is missing OR it is on include path */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 10 |  |                     if (!$propertyMetadatum->isIncludable() || $this->isIncluded( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                             $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 10 |  |                             $propertyMetadatum->getIncludablePaths(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                             $includes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 10 |  |                         $value = $propertyMetadatum->getValue($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 10 |  |                         $normalizedData[$propertyMetadatum->name] = $this->normalizeField($value, $propertyMetadatum); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 10 |  |             return $normalizedData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     private function isIncluded($currentPath, array $paths, ?array $includes): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if (null === $includes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         foreach ($paths as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             if (in_array($this->appendPath($currentPath, $path), $includes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     private function appendPath($path, $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         if (null === $path || '' === $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             return $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $path . '.' . $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 10 |  |     private function normalizeField($value, PropertyMetadata $propertyMetadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 10 |  |         switch ($propertyMetadata->getType()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 10 |  |             case 'datetime': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 6 |  |                 if (null === $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 /** @var $value \DateTime */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 6 |  |                 return $value->format('Y-m-d H:i:s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 10 |  |             case 'date': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 6 |  |                 if (null === $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                     return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 /** @var $value \DateTime */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 6 |  |                 return $value->format('Y-m-d'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 10 |  |             case 'time': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 6 |  |                 if (null === $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                     return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 /** @var $value \DateTime */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 6 |  |                 return $value->format('H:i:s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 10 |  |                 return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |  | 
            
                        
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.