/**
  * 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 #2
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()));
 }