Автор: Kévin Dunglas (dunglas@gmail.com)
Наследование: extends Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer, use trait ContextTrait
Пример #1
0
 /**
  * Gets HAL components of the resource: states, links and embedded.
  *
  * @param object      $object
  * @param string|null $format
  * @param array       $context
  *
  * @return array
  */
 private function getComponents($object, string $format = null, array $context)
 {
     if (isset($this->componentsCache[$context['cache_key']])) {
         return $this->componentsCache[$context['cache_key']];
     }
     $attributes = parent::getAttributes($object, $format, $context);
     $options = $this->getFactoryOptions($context);
     $components = ['states' => [], 'links' => [], 'embedded' => []];
     foreach ($attributes as $attribute) {
         $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
         $type = $propertyMetadata->getType();
         $isOne = $isMany = false;
         if (null !== $type) {
             if ($type->isCollection()) {
                 $valueType = $type->getCollectionValueType();
                 $isMany = null !== $valueType && ($className = $valueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
             } else {
                 $className = $type->getClassName();
                 $isOne = $className && $this->resourceClassResolver->isResourceClass($className);
             }
         }
         if (!$isOne && !$isMany) {
             $components['states'][] = $attribute;
             continue;
         }
         $relation = ['name' => $attribute, 'cardinality' => $isOne ? 'one' : 'many'];
         if ($propertyMetadata->isReadableLink()) {
             $components['embedded'][] = $relation;
         }
         $components['links'][] = $relation;
     }
     return $this->componentsCache[$context['cache_key']] = $components;
 }
Пример #2
0
 /**
  * {@inheritdoc}
  *
  * @throws InvalidArgumentException
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     // Avoid issues with proxies if we populated the object
     if (isset($data['id']) && !isset($context['object_to_populate'])) {
         if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
             throw new InvalidArgumentException('Update is not allowed for this operation.');
         }
         $context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
     }
     return parent::denormalize($data, $class, $format, $context);
 }