public function normalizeEntity($entity, $doNotExpandRelationsAndCollections = false) { // Make sure that the used is authorized to see this resource $canSee = $this->getConfigurationEntity()['can_see']; if (false === $canSee($this->securityContext, $entity)) { throw new AccessDeniedException(); } $entityRepresentationClass = $this->getConfigurationEntity()['representation_class']; $entityRepresentation = new $entityRepresentationClass(); $entityRepresentation->addLink($this->atomLinkFactory->create('self', $this->getUrlGenerator()->generateEntityUrl($entity))); $getEntityValue = function ($value) use($entity) { if ($value instanceof \Closure) { return $value($entity); } $propertyPath = new PropertyPath($value); return $propertyPath->getValue($entity); }; // Properties $normalizeAttributes = $this->getConfigurationEntity()['normalize_attributes']; foreach ($normalizeAttributes as $key => $value) { $entityRepresentation->setAttribute($key, $getEntityValue($value)); } // Elements $normalizeElements = $this->getConfigurationEntity()['normalize_elements']; foreach ($normalizeElements as $key => $value) { $entityRepresentation->setElement($key, $getEntityValue($value)); } // Entity collections foreach ($this->getConfigurationEntityCollections() as $entityCollectionRel => $configurationEntityCollection) { if (isset($configurationEntityCollection['can_see']) && !$configurationEntityCollection['can_see']($this->securityContext, $entity)) { continue; } if ($doNotExpandRelationsAndCollections || !in_array($entityCollectionRel, $this->getConfigurationEntity()['expanded_collections'])) { $entityRepresentation->addLink($this->atomLinkFactory->create($entityCollectionRel, $this->getUrlGenerator()->generateEntityCollectionUrl($entity, $entityCollectionRel))); } else { $entityRelationSearchFormDescription = $this->createEntityCollectionSearchFormDescription($entity, $entityCollectionRel); $entityRelationPager = $this->getEntityCollectionPager($entity, $entityRelationSearchFormDescription->getData(), $entityCollectionRel); $entityRelationRepresentation = $this->normalizeEntityCollection($entity, $entityCollectionRel, $entityRelationPager, $entityRelationSearchFormDescription); $entityRelationRepresentation->rel = $this->atomLinkFactory->getRel($entityCollectionRel); $entityRepresentation->addCollection($entityRelationRepresentation); } } // Entity relations foreach ($this->getConfigurationEntityRelations() as $entityRelationRel => $entityRelationConfiguration) { $entityRelation = $this->getEntityRelation($entity, $entityRelationRel); if (null === $entityRelation) { continue; } $entityRelationClassName = ClassUtils::getRealClass(get_class($entityRelation)); $entityRelationResource = $this->container->get($entityRelationConfiguration['resources'][$entityRelationClassName]); $canSeeEntityRelation = $entityRelationResource->getConfigurationEntity()['can_see']; if (!$canSeeEntityRelation($this->securityContext, $entityRelation) || $doNotExpandRelationsAndCollections || !in_array($entityRelationRel, $this->getConfigurationEntity()['expanded_relations'])) { $entityRepresentation->addLink($this->atomLinkFactory->create($entityRelationRel, $entityRelationResource->getUrlGenerator()->generateEntityUrl($entityRelation))); } else { $entityRelationRepresentation = $entityRelationResource->normalizeEntity($entityRelation, true); $entityRelationRepresentation->rel = $this->atomLinkFactory->getRel($entityRelationRel); $entityRepresentation->addRelation($entityRelationRepresentation); } } return $entityRepresentation; }