/**
  * Gets the manager if applicable.
  *
  * @param ResourceInterface $resourceType
  * @param mixed             $data
  *
  * @return ObjectManager|null
  */
 private function getManager(ResourceInterface $resourceType, $data)
 {
     $objectManager = $this->managerRegistry->getManagerForClass($resourceType->getEntityClass());
     if (null === $objectManager || !is_object($data)) {
         return;
     }
     return $objectManager;
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function applyToCollection(ResourceInterface $resource, QueryBuilder $queryBuilder)
 {
     $classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resource->getEntityClass());
     $identifiers = $classMetaData->getIdentifier();
     if (null !== $this->order && 1 === count($identifiers)) {
         $identifier = $identifiers[0];
         $queryBuilder->addOrderBy('o.' . $identifier, $this->order);
     }
 }
 /**
  * Left joins relations to eager load.
  *
  * @param ResourceInterface $resource
  * @param QueryBuilder      $queryBuilder
  */
 private function joinRelations(ResourceInterface $resource, QueryBuilder $queryBuilder)
 {
     $classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resource->getEntityClass());
     foreach ($classMetaData->getAssociationNames() as $i => $association) {
         $mapping = $classMetaData->associationMappings[$association];
         if (ClassMetadataInfo::FETCH_EAGER === $mapping['fetch']) {
             $queryBuilder->leftJoin('o.' . $association, 'a' . $i);
             $queryBuilder->addSelect('a' . $i);
         }
     }
 }
Esempio n. 4
0
 /**
  * @param Request           $request
  * @param ResourceInterface $resourceEmbed
  * @param                   $data
  *
  * @return \Doctrine\Common\Collections\Collection|PersistentCollection
  */
 public function ApplyCriteria(Request $request, ResourceInterface $resourceEmbed, $data)
 {
     if ($data instanceof PersistentCollection && $data->count() > 0) {
         $embedClassMeta = $this->em->getClassMetadata($resourceEmbed->getEntityClass());
         $criteria = Criteria::create();
         foreach ($resourceEmbed->getFilters() as $filter) {
             if ($filter instanceof FilterInterface) {
                 $this->applyFilter($request, $filter, $criteria, $embedClassMeta);
                 $data = $data->matching($criteria);
             }
         }
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function add(ResourceInterface $resource)
 {
     $entityClass = $resource->getEntityClass();
     if (isset($this->entityClassIndex[$entityClass])) {
         throw new \InvalidArgumentException(sprintf('A Resource class already exists for "%s".', $entityClass));
     }
     $shortName = $resource->getShortName();
     if (isset($this->shortNameIndex[$shortName])) {
         throw new \InvalidArgumentException(sprintf('A Resource class with the short name "%s" already exists.', $shortName));
     }
     $this->append($resource);
     $this->entityClassIndex[$entityClass] = $resource;
     $this->shortNameIndex[$shortName] = $resource;
 }
 /**
  * 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) {
             if (!($id = $attribute->getIri())) {
                 $id = sprintf('%s/%s', $prefixedShortName, $attributeName);
             }
             if ($attribute->isNormalizationLink()) {
                 $context[$attributeName] = ['@id' => $id, '@type' => '@id'];
             } else {
                 $context[$attributeName] = $id;
             }
         }
     }
     return $context;
 }
 /**
  * Gets class metadata for the given resource.
  *
  * @param ResourceInterface $resource
  *
  * @return ClassMetadata
  */
 private function getClassMetadata(ResourceInterface $resource)
 {
     $entityClass = $resource->getEntityClass();
     return $this->managerRegistry->getManagerForClass($entityClass)->getClassMetadata($entityClass);
 }
 /**
  * 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());
 }
Esempio n. 9
0
 /**
  * created querybuilder
  *
  * @param ResourceInterface $resource
  * @param Request $request
  * @return mixed
  */
 public function getQueryBuilderForCollection(ResourceInterface $resource, Request $request)
 {
     $entityClass = $resource->getEntityClass();
     $manager = $this->managerRegistry->getManagerForClass($resource->getEntityClass());
     $repository = $manager->getRepository($entityClass);
     $page = (int) $request->get($this->pageParameter, 1);
     $itemsPerPage = $this->getItemPerPage($request);
     $queryBuilder = $this->getQB($request, $repository, $page, $itemsPerPage);
     foreach ($resource->getFilters() as $filter) {
         if ($filter instanceof FilterInterface) {
             $filter->apply($resource, $queryBuilder, $request);
         }
     }
     return $queryBuilder;
 }
Esempio n. 10
0
 /**
  * Denormalizes a relation.
  *
  * @param ResourceInterface $currentResource
  * @param AttributeMetadata $attributeMetadata
  * @param                   $class
  * @param                   $value
  *
  * @return object
  * @throws InvalidArgumentException
  */
 private function denormalizeRelation(ResourceInterface $currentResource, AttributeMetadata $attributeMetadata, $class, $value)
 {
     if ('Datetime' === $class) {
         $dateTimeNormalizer = new DateTimeNormalizer();
         return $dateTimeNormalizer->denormalize($value, $class ?: null, self::FORMAT);
     }
     $attributeName = $attributeMetadata->getName();
     //        // Always allow IRI to be compliant with the Hydra spec
     //        if (is_string($value)) {
     //            $item = $this->dataProvider->getItemFromIri($value);
     //            if (null === $item) {
     //                throw new InvalidArgumentException(sprintf(
     //                    'IRI  not supported (found "%s" in "%s" of "%s")',
     //                    $value,
     //                    $attributeName,
     //                    $currentResource->getEntityClass()
     //                ));
     //            }
     //            return $item;
     //        }
     if (!$this->resourceCollection->getResourceForEntity($class)) {
         $shortname = ucfirst(Inflector::singularize($attributeName));
         if (!$this->resourceCollection->getResourceForShortName($shortname)) {
             throw new InvalidArgumentException(sprintf('Type not supported (found "%s" in attribute "%s" of "%s")', $class, $attributeName, $currentResource->getEntityClass()));
         } else {
             $resource = $this->resourceCollection->getResourceForShortName($shortname);
             $context = $this->contextBuilder->bootstrapRelation($resource, $resource->getEntityClass());
         }
     } else {
         $context = $this->contextBuilder->bootstrapRelation($currentResource, $class);
     }
     if (!$attributeMetadata->isDenormalizationLink()) {
         $object = $this->denormalize(json_encode($value), $resource->getEntityClass(), self::FORMAT, $context);
         //$this->deserializeObjects[] = $object;
         return $object;
     }
     throw new InvalidArgumentException(sprintf('Nested objects for attribute "%s" of "%s" are not enabled. Use serialization groups to change that behavior.', $attributeName, $currentResource->getEntityClass()));
 }
 /**
  * Builds ApiDoc annotation from DunglasApiBundle data.
  *
  * @param bool               $collection
  * @param ResourceInterface  $resource
  * @param OperationInterface $operation
  * @param array              $resourceHydraDoc
  * @param array              $entrypointHydraDoc
  *
  * @return ApiDoc
  */
 private function getApiDoc($collection, ResourceInterface $resource, OperationInterface $operation, array $resourceHydraDoc, array $entrypointHydraDoc = [])
 {
     $method = $operation->getRoute()->getMethods()[0];
     if ($collection) {
         $operationHydraDoc = $this->getCollectionOperationHydraDoc($resource->getShortName(), $method, $entrypointHydraDoc);
     } else {
         $operationHydraDoc = $this->getOperationHydraDoc($operation->getRoute()->getMethods()[0], $resourceHydraDoc);
     }
     $route = $operation->getRoute();
     $data = ['resource' => $route->getPath(), 'description' => $operationHydraDoc['hydra:title'], 'resourceDescription' => $resourceHydraDoc['hydra:title'], 'section' => $resourceHydraDoc['hydra:title']];
     $entityClass = $resource->getEntityClass();
     if (isset($operationHydraDoc['expects']) && 'owl:Nothing' !== $operationHydraDoc['expects']) {
         $data['input'] = sprintf('%s:%s', DunglasApiParser::IN_PREFIX, $entityClass);
     }
     if (isset($operationHydraDoc['returns']) && 'owl:Nothing' !== $operationHydraDoc['returns']) {
         $data['output'] = sprintf('%s:%s', DunglasApiParser::OUT_PREFIX, $entityClass);
     }
     if (Request::METHOD_GET === $method && $collection) {
         $data['filters'] = [];
         foreach ($resource->getFilters() as $filter) {
             foreach ($filter->getDescription($resource) as $name => $definition) {
                 $data['filters'][] = ['name' => $name] + $definition;
             }
         }
     }
     $apiDoc = new ApiDoc($data);
     $apiDoc->setRoute($route);
     return $apiDoc;
 }
 /**
  * {@inheritdoc}
  */
 public function supports(ResourceInterface $resource)
 {
     return null !== $this->managerRegistry->getManagerForClass($resource->getEntityClass());
 }
 /**
  * @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();
 }
Esempio n. 14
0
 /**
  * @param Request           $request
  * @param ResourceInterface $resource
  *
  * @return object
  *
  * @throws DeserializationException
  */
 protected function getEntity(Request $request, ResourceInterface $resource)
 {
     try {
         $object = $this->get('api.json_ld.normalizer.item')->denormalize($request->getContent(), $resource->getEntityClass(), 'json-ld', $resource->getDenormalizationContext());
     } catch (\Exception $e) {
         throw new DeserializationException($e->getMessage(), $e->getCode(), $e);
     }
     return $object;
 }
 /**
  * {@inheritdoc}
  */
 public function apply(ResourceInterface $resource, QueryBuilder $queryBuilder, Request $request)
 {
     if ($this->getResourceClass() === $resource->getEntityClass()) {
         $this->applyFilter($resource, $queryBuilder, $this->extractProperties($request));
     }
 }