public function getAttributeMetadataFor(ClassMetadataInterface $classMetadata, $attributeName, array $normalizationGroups = null, array $denormalizationGroups = null) { if ($classMetadata->hasAttributeMetadata($attributeName)) { return $classMetadata->getAttributeMetadata($attributeName); } $attributeMetadata = new AttributeMetadata(); $reflectionProperty = $this->getReflectionProperty($classMetadata->getReflectionClass(), $attributeName); if (!$reflectionProperty) { return $attributeMetadata; } $types = $this->propertyInfo->getTypes($reflectionProperty); if (!isset($types[0])) { return $attributeMetadata; } $type = $types[0]; $attributeMetadata = $attributeMetadata->withType($type); $class = $type->getClass(); $link = $this->resourceCollection->getResourceForEntity($class) || $type->isCollection() && $type->getCollectionType() && ($class = $type->getCollectionType()->getClass()) && $this->resourceCollection->getResourceForEntity($class); $attributeMetadata = $attributeMetadata->withLink($link)->withLinkClass($class); if (!$link) { return $attributeMetadata; } if (null === $normalizationGroups) { $attributeMetadata = $attributeMetadata->withNormalizationLink(true); } if (null === $denormalizationGroups) { $attributeMetadata = $attributeMetadata->withDenormalizationLink(true); } return $attributeMetadata; }
/** * Gets or creates the {@see AttributeMetadata} of the given name. * * @param ClassMetadata $classMetadata * @param string $attributeName * @param string[]|null $normalizationGroups * * @return AttributeMetadata */ private function getOrCreateAttribute(ClassMetadata $classMetadata, $attributeName, array $normalizationGroups = null, array $denormalizationGroups = null) { if (isset($classMetadata->getAttributes()[$attributeName])) { return $classMetadata->getAttributes()[$attributeName]; } $attributeMetadata = new AttributeMetadata($attributeName); $classMetadata->addAttribute($attributeMetadata); $reflectionProperty = $this->getReflectionProperty($classMetadata->getReflectionClass(), $attributeName); if (!$reflectionProperty) { return $attributeMetadata; } $types = $this->propertyInfo->getTypes($reflectionProperty); if (null !== $types) { $attributeMetadata->setTypes($types); } if (!isset($types[0])) { return $attributeMetadata; } $class = $types[0]->getClass(); if (!$this->resourceCollection->getResourceForEntity($class) && !($types[0]->isCollection() && $types[0]->getCollectionType() && ($class = $types[0]->getCollectionType()->getClass()) && $this->resourceCollection->getResourceForEntity($class))) { return $attributeMetadata; } if (null === $normalizationGroups) { $attributeMetadata->setNormalizationLink(true); } if (null === $denormalizationGroups) { $attributeMetadata->setDenormalizationLink(true); } if ($attributeMetadata->isNormalizationLink() && $attributeMetadata->isDenormalizationLink()) { return $attributeMetadata; } if (!$this->serializerClassMetadataFactory || !($relationSerializerMetadata = $this->serializerClassMetadataFactory->getMetadataFor($class))) { $attributeMetadata->setNormalizationLink(true); $attributeMetadata->setDenormalizationLink(true); return $attributeMetadata; } foreach ($relationSerializerMetadata->getAttributesMetadata() as $serializerAttributeMetadata) { $serializerAttributeGroups = $serializerAttributeMetadata->getGroups(); if (null !== $normalizationGroups && 1 <= count(array_intersect($normalizationGroups, $serializerAttributeGroups))) { $normalizationLink = false; } if (null !== $denormalizationGroups && 1 <= count(array_intersect($denormalizationGroups, $serializerAttributeGroups))) { $denormalizationLink = false; } if (isset($normalizationLink) && isset($denormalizationLink)) { return $attributeMetadata; } } if (!isset($normalizationLink)) { $attributeMetadata->setNormalizationLink(true); } if (!isset($denormalizationLink)) { $attributeMetadata->setDenormalizationLink(true); } return $attributeMetadata; }