/**
  * Gets the entrypoint of the API.
  *
  * return array
  */
 public function getEntrypoint()
 {
     $entrypoint = ['@context' => $this->router->generate('api_json_ld_entrypoint_context'), '@id' => $this->router->generate('api_json_ld_entrypoint'), '@type' => 'Entrypoint'];
     foreach ($this->resourceCollection as $resource) {
         $entrypoint[lcfirst($resource->getShortName())] = $this->iriConverter->getIriFromResource($resource);
     }
     return $entrypoint;
 }
 /**
  * @param AttributeMetadataInterface $attributeMetadata
  *
  * @return mixed
  */
 public function guess(AttributeMetadataInterface $attributeMetadata)
 {
     $value = null;
     $type = null;
     if (true === ($isDoctrine = isset($attributeMetadata->getTypes()[0]))) {
         $type = $attributeMetadata->getTypes()[0];
     }
     // Guess associations
     if ($isDoctrine && 'object' === $type->getType() && 'DateTime' !== $type->getClass()) {
         $class = $type->isCollection() ? $type->getCollectionType()->getClass() : $type->getClass();
         $resource = $this->resourceCollection->getResourceForEntity($class);
         $classMetadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass(), $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups());
         $id = $this->guess($classMetadata->getIdentifier());
         $value = $this->iriConverter->getIriFromResource($resource) . '/' . $id;
         if ($type->isCollection()) {
             $value = [$value];
         }
     }
     // Guess by faker
     if (null === $value) {
         try {
             $value = call_user_func([$this->generator, $attributeMetadata->getName()]);
         } catch (\InvalidArgumentException $e) {
         }
     }
     // Guess by field name
     if (null === $value) {
         $value = $this->guessFormat(Inflector::tableize($attributeMetadata->getName()));
     }
     // Guess by Doctrine type
     if (null === $value && $isDoctrine) {
         switch ($type->getType()) {
             case 'string':
                 $value = $this->generator->sentence;
                 break;
             case 'int':
                 $value = $this->generator->numberBetween;
                 break;
             case 'bool':
                 $value = $this->generator->boolean;
                 break;
             case 'object':
                 if ('DateTime' !== $type->getClass()) {
                     throw new \InvalidArgumentException(sprintf('Unknown Doctrine object type %s in field %s', $type->getClass(), $attributeMetadata->getName()));
                 }
                 $value = $this->generator->dateTime;
                 break;
         }
     }
     return $this->clean($value);
 }
 /**
  * Gets the entrypoint of the API.
  *
  * return array
  */
 public function getEntrypoint()
 {
     $entrypoint = ['@context' => $this->router->generate('api_json_ld_entrypoint_context'), '@id' => $this->router->generate('api_json_ld_entrypoint'), '@type' => 'Entrypoint'];
     foreach ($this->resourceCollection as $resource) {
         if (is_null($resource->getParent())) {
             foreach ($resource->getCollectionOperations() as $operation) {
                 if (in_array('GET', $operation->getRoute()->getMethods())) {
                     $entrypoint[lcfirst($resource->getShortName())] = $this->iriConverter->getIriFromResource($resource);
                     break;
                 }
             }
         }
     }
     return $entrypoint;
 }
 /**
  * Gets the entrypoint of the API.
  *
  * @return array
  */
 public function getEntrypoint()
 {
     $entrypoint = ['@context' => $this->router->generate('api_jsonld_context', ['shortName' => 'Entrypoint']), '@id' => $this->router->generate('api_jsonld_entrypoint'), '@type' => 'Entrypoint'];
     foreach ($this->resourceCollection as $resource) {
         if (!empty($resource->getCollectionOperations())) {
             try {
                 $entrypoint[lcfirst($resource->getShortName())] = $this->iriConverter->getIriFromResource($resource);
             } catch (InvalidArgumentException $ex) {
                 if ($this->hasGetCollectionOperation($resource)) {
                     throw $ex;
                 }
             }
         }
     }
     return $entrypoint;
 }
 /**
  * Gets the ID from an URI or a raw ID.
  *
  * @param string $value
  *
  * @return string
  */
 private function getFilterValueFromUrl($value)
 {
     if (is_array($value)) {
         $items = [];
         foreach ($value as $iri) {
             try {
                 if ($item = $this->iriConverter->getItemFromIri($iri)) {
                     $items[] = $this->propertyAccessor->getValue($item, 'id');
                 } else {
                     $items[] = $iri;
                 }
             } catch (\InvalidArgumentException $e) {
                 $items[] = $iri;
             }
         }
         return $items;
     }
     try {
         if ($item = $this->iriConverter->getItemFromIri($value)) {
             return $this->propertyAccessor->getValue($item, 'id');
         }
     } catch (\InvalidArgumentException $e) {
         // Do nothing, return the raw value
     }
     return $value;
 }
Example #6
0
 /**
  * Gets the ID from an URI or a raw ID.
  *
  * @param string $value
  *
  * @return string
  */
 private function getFilterValueFromUrl($value)
 {
     try {
         if ($item = $this->iriConverter->getItemFromIri($value)) {
             return $this->propertyAccessor->getValue($item, 'id');
         }
     } catch (\InvalidArgumentException $e) {
         // Do nothing, return the raw value
     }
     return $value;
 }
 /**
  * Replaces any temporary images with actual instances of the configured UploadedFile collection.
  *
  * Automatically extracts the proper setters and getters from the metadata and instantiates the correct
  * UploadedFile child class.
  *
  * @param GetResponseForControllerResultEvent $event The event
  */
 public function replaceTemporaryFile(GetResponseForControllerResultEvent $event)
 {
     $data = $event->getControllerResult();
     if (!is_object($data)) {
         return;
     }
     $classReflection = new \ReflectionClass($data);
     foreach ($classReflection->getProperties() as $property) {
         $propertyAnnotationCollection = $this->reader->getPropertyAnnotation($property, 'PartKeepr\\UploadedFileBundle\\Annotation\\UploadedFileCollection');
         $propertyAnnotation = $this->reader->getPropertyAnnotation($property, 'PartKeepr\\UploadedFileBundle\\Annotation\\UploadedFile');
         $manyToOneAnnotation = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\OneToMany');
         $oneToOneAnnotation = $this->reader->getPropertyAnnotation($property, 'Doctrine\\ORM\\Mapping\\OneToOne');
         if ($propertyAnnotationCollection !== null || $propertyAnnotation !== null) {
             if ($manyToOneAnnotation !== null) {
                 $collection = $this->propertyAccessor->getValue($data, $property->getName());
                 foreach ($collection as $key => $item) {
                     if ($item instanceof TempUploadedFile || $item instanceof TempImage) {
                         $targetEntity = $manyToOneAnnotation->targetEntity;
                         $newFile = $this->setReplacementFile($targetEntity, $item, $data);
                         $collection[$key] = $newFile;
                     }
                 }
                 $this->propertyAccessor->setValue($data, $property->getName(), $collection);
             }
             if ($oneToOneAnnotation !== null) {
                 $item = $this->propertyAccessor->getValue($data, $property->getName());
                 if ($item instanceof TempUploadedFile || $item instanceof TempImage) {
                     $targetEntity = $oneToOneAnnotation->targetEntity;
                     $newFile = $this->setReplacementFile($targetEntity, $item, $data);
                     $this->propertyAccessor->setValue($data, $property->getName(), $newFile);
                 } else {
                     $item = $this->propertyAccessor->getValue($data, $property->getName());
                     if ($item !== null && $item->getReplacement() !== null) {
                         /**
                          * @var UploadedFile
                          */
                         $tempImage = $this->iriConverter->getItemFromIri($item->getReplacement());
                         $this->replaceFile($item, $tempImage);
                     }
                 }
             }
         }
     }
     $event->setControllerResult($data);
 }
 /**
  * Denormalizes a relation.
  *
  * @param ResourceInterface          $currentResource
  * @param string                     $attributeName
  * @param AttributeMetadataInterface $attributeMetadata
  * @param string                     $class
  * @param mixed                      $value
  * @param array                      $context
  *
  * @return object|null
  *
  * @throws InvalidArgumentException
  */
 private function denormalizeRelation(ResourceInterface $currentResource, $attributeName, AttributeMetadataInterface $attributeMetadata, $class, $value, array $context)
 {
     if ('DateTime' === $class) {
         return $this->serializer->denormalize($value, $class ?: null, self::FORMAT, $context);
     }
     // Always allow IRI to be compliant with the Hydra spec
     if (is_string($value)) {
         try {
             return $this->iriConverter->getItemFromIri($value);
         } catch (InvalidArgumentException $e) {
             throw new InvalidArgumentException(sprintf('IRI  not supported (found "%s" in "%s" of "%s")', $value, $attributeName, $currentResource->getEntityClass()), $e->getCode(), $e);
         }
     }
     if (!($resource = $this->resourceCollection->getResourceForEntity($class))) {
         throw new InvalidArgumentException(sprintf('Type not supported (found "%s" in attribute "%s" of "%s")', $class, $attributeName, $currentResource->getEntityClass()));
     }
     if (!$attributeMetadata->isDenormalizationLink()) {
         return $this->serializer->denormalize($value, $class, self::FORMAT, $this->createRelationContext($resource, $context));
     }
     throw new InvalidArgumentException(sprintf('Nested objects for attribute "%s" of "%s" are not enabled. Use serialization groups to change that behavior.', $attributeName, $currentResource->getEntityClass()));
 }