/** * Creates normalization context. * * @param ResourceInterface $resource * @param array $context * * @return array */ private function createContext(ResourceInterface $resource, $context) { if (!isset($context['jsonld_has_context'])) { $context += ['resource' => $resource, 'jsonld_has_context' => true, 'jsonld_sub_level' => true, 'jsonld_normalization_groups' => $resource->getNormalizationGroups(), 'jsonld_denormalization_groups' => $resource->getDenormalizationGroups(), 'jsonld_validation_groups' => $resource->getValidationGroups()]; } return $context; }
/** * Parses a class. * * @param ResourceInterface $resource * @param string $entityClass * @param string $io * * @return array */ private function parseClass(ResourceInterface $resource, $entityClass, $io) { $classMetadata = $this->classMetadataFactory->getMetadataFor($entityClass, $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups()); $data = array(); foreach ($classMetadata->getAttributes() as $attributeMetadata) { if (!$attributeMetadata->isIdentifier() && $attributeMetadata->isReadable() && self::OUT_PREFIX === $io || $attributeMetadata->isWritable() && self::IN_PREFIX === $io) { $data[$attributeMetadata->getName()] = $this->parseAttribute($resource, $attributeMetadata, $io); } } return $data; }
/** * Builds the JSON-LD context for the given resource. * * @param ResourceInterface|null $resource * * @return array */ public function getContext(ResourceInterface $resource = null) { $context = $this->getBaseContext(); if ($resource) { $prefixedShortName = sprintf('#%s', $resource->getShortName()); $attributes = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass(), $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups())->getAttributes(); foreach ($attributes as $attributeName => $attribute) { $convertedName = $this->nameConverter ? $this->nameConverter->normalize($attributeName) : $attributeName; if (!($id = $attribute->getIri())) { $id = sprintf('%s/%s', $prefixedShortName, $convertedName); } if ($attribute->isNormalizationLink()) { $context[$convertedName] = ['@id' => $id, '@type' => '@id']; } else { $context[$convertedName] = $id; } } } return $context; }
/** * Gets metadata for the given resource with the current context. * * Fallback to the resource own groups if no context is provided. * * @param ResourceInterface $resource * @param array $context * * @return ClassMetadataInterface */ private function getMetadata(ResourceInterface $resource, array $context) { return $this->apiClassMetadataFactory->getMetadataFor($resource->getEntityClass(), isset($context['json_ld_normalization_groups']) ? $context['json_ld_normalization_groups'] : $resource->getNormalizationGroups(), isset($context['json_ld_denormalization_groups']) ? $context['json_ld_denormalization_groups'] : $resource->getDenormalizationGroups(), isset($context['json_ld_validation_groups']) ? $context['json_ld_validation_groups'] : $resource->getValidationGroups()); }
/** * @param ResourceInterface $resource * * @return AttributeMetadataInterface */ private function getIdentifierFromResource(ResourceInterface $resource) { $classMetadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass(), $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups()); return $classMetadata->getIdentifier(); }
/** * Bootstrap relation context. * * @param ResourceInterface $resource * @param string $class * * @return array */ public function bootstrapRelation(ResourceInterface $resource, $class) { return ['resource' => $this->resourceCollection->getResourceForEntity($class), 'json_ld_has_context' => true, 'json_ld_normalization_groups' => $resource->getNormalizationGroups(), 'json_ld_denormalization_groups' => $resource->getDenormalizationGroups(), 'json_ld_validation_groups' => $resource->getValidationGroups()]; }