| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace W2w\Lib\Apie\Plugins\PrimaryKey\Normalizers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Serializer\SerializerAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Serializer\SerializerAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use W2w\Lib\Apie\Core\ApiResourceMetadataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use W2w\Lib\Apie\Core\ClassResourceConverter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use W2w\Lib\Apie\Core\IdentifierExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use W2w\Lib\Apie\Core\Resources\ApiResourcesInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use W2w\Lib\Apie\Plugins\Core\Normalizers\ApieObjectNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use W2w\Lib\Apie\Plugins\Core\Normalizers\ContextualNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use W2w\Lib\Apie\Plugins\PrimaryKey\ValueObjects\PrimaryKeyReference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * If a resource has an other api resource as a child, this class will map it as a url and not try to hydrate it as an url. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class ApiePrimaryKeyNormalizer implements ContextAwareNormalizerInterface, SerializerAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     use SerializerAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var ApiResourcesInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $apiResources; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var IdentifierExtractor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $identifierExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var ApiResourceMetadataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $metadataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var ClassResourceConverter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private $baseUrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ApiResourcesInterface $apiResources, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         IdentifierExtractor $identifierExtractor, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ApiResourceMetadataFactory $metadataFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ClassResourceConverter $converter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         string $baseUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->apiResources = $apiResources; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->identifierExtractor = $identifierExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->metadataFactory = $metadataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->converter = $converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->baseUrl = rtrim($baseUrl, '/') . '/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function supportsNormalization($data, $format = null, array $context = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (ContextualNormalizer::isNormalizerEnabled(ApieObjectNormalizer::class) || empty($context['object_hierarchy']) || !empty($context['disable_pk_normalize'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         foreach ($this->apiResources->getApiResources() as $apiResource) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             // if someone is really stupid to add this as an API resource.... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if ($apiResource === PrimaryKeyReference::class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $resourceContext = $this->metadataFactory->getMetadata($apiResource)->getContext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $identifier = $this->identifierExtractor->getIdentifierKeyOfClass($apiResource, $resourceContext); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             if (null !== $identifier && is_a($data, $apiResource)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     public function normalize($object, $format = null, array $context = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         $metadata = $this->metadataFactory->getMetadata($object); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $resourceContext = $this->metadataFactory->getMetadata($object)->getContext(); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $identifierValue = $this->identifierExtractor->getIdentifierValue($object, $resourceContext); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         return $this->serializer->normalize( | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             new PrimaryKeyReference( | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 $metadata, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 $this->baseUrl . $this->converter->normalize($metadata->getClassName()), | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                 $identifierValue | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             $format, | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  |