/**
  * Populates normalization and denormalization links.
  *
  * @param ClassMetadataInterface     $classMetadata
  * @param string                     $attributeName
  * @param AttributeMetadataInterface $attributeMetadata
  * @param array|null                 $normalizationGroups
  * @param array|null                 $denormalizationGroups
  *
  * @return ClassMetadataInterface
  */
 private function populateNormalizationLinks(ClassMetadataInterface $classMetadata, $attributeName, AttributeMetadataInterface $attributeMetadata, array $normalizationGroups = null, array $denormalizationGroups = null)
 {
     if (!$classMetadata->hasAttributeMetadata($attributeName) || !$attributeMetadata->isLink() || $attributeMetadata->isNormalizationLink() && $attributeMetadata->isDenormalizationLink()) {
         return $classMetadata;
     }
     $relationSerializerMetadata = $this->serializerClassMetadataFactory->getMetadataFor($attributeMetadata->getLinkClass());
     if (!$relationSerializerMetadata) {
         $attributeMetadata = $attributeMetadata->withNormalizationLink(true)->withDenormalizationLink(true);
         return $classMetadata->withAttributeMetadata($attributeName, $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)) {
             $classMetadata = $classMetadata->withAttributeMetadata($attributeName, $attributeMetadata);
         }
     }
     if (!isset($normalizationLink)) {
         $attributeMetadata = $attributeMetadata->withNormalizationLink(true);
     }
     if (!isset($denormalizationLink)) {
         $attributeMetadata = $attributeMetadata->withDenormalizationLink(true);
     }
     return $classMetadata->withAttributeMetadata($attributeName, $attributeMetadata);
 }
 /**
  * {@inheritdoc}
  */
 public function getProperties($class, array $context = array())
 {
     if (!isset($context['serializer_groups']) || !is_array($context['serializer_groups'])) {
         return;
     }
     if (!$this->classMetadataFactory->getMetadataFor($class)) {
         return;
     }
     $properties = array();
     $serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($class);
     foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
         if (count(array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups())) > 0) {
             $properties[] = $serializerAttributeMetadata->getName();
         }
     }
     return $properties;
 }
 /**
  * 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;
 }
 public function testGetAllowedAttributesAsObjects()
 {
     $classMetadata = new ClassMetadata('c');
     $a1 = new AttributeMetadata('a1');
     $classMetadata->addAttributeMetadata($a1);
     $a2 = new AttributeMetadata('a2');
     $a2->addGroup('test');
     $classMetadata->addAttributeMetadata($a2);
     $a3 = new AttributeMetadata('a3');
     $a3->addGroup('other');
     $classMetadata->addAttributeMetadata($a3);
     $a4 = new AttributeMetadata('a4');
     $a4->addGroup('test');
     $a4->addGroup('other');
     $classMetadata->addAttributeMetadata($a4);
     $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
     $result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('test')), false);
     $this->assertEquals(array($a2, $a4), $result);
     $result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), false);
     $this->assertEquals(array($a3, $a4), $result);
 }
Beispiel #5
0
 /**
  * Gets attributes to normalize using groups.
  *
  * @param string|object $classOrObject
  * @param array         $context
  * @param bool          $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
  *
  * @return string[]|AttributeMetadataInterface[]|bool
  */
 protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
 {
     if (!$this->classMetadataFactory || !isset($context['groups']) || !is_array($context['groups'])) {
         return false;
     }
     $allowedAttributes = array();
     foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
         if (count(array_intersect($attributeMetadata->getGroups(), $context['groups']))) {
             $allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
         }
     }
     return array_unique($allowedAttributes);
 }
Beispiel #6
0
 /**
  * Gets attributes to normalize using groups.
  *
  * @param string|object $classOrObject
  * @param array         $context
  * @param bool          $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
  *
  * @return string[]|AttributeMetadataInterface[]|bool
  */
 protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
 {
     if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
         return false;
     }
     $allowedAttributes = array();
     foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
         $name = $attributeMetadata->getName();
         if (count(array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS])) && $this->isAllowedAttribute($classOrObject, $name, null, $context)) {
             $allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
         }
     }
     return $allowedAttributes;
 }
 /**
  * {@inheritdoc}
  */
 public function hasMetadataFor($value)
 {
     return $this->decorated->hasMetadataFor($value);
 }