Commit 5d46e728 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Renforcement des vérifications dans le UploadedFileTransformer.

Showing with 43 additions and 11 deletions
+43 -11
...@@ -11,7 +11,7 @@ use Irstea\FileUploadBundle\Entity\UploadedFile; ...@@ -11,7 +11,7 @@ use Irstea\FileUploadBundle\Entity\UploadedFile;
use Irstea\FileUploadBundle\Service\FileManagerInterface; use Irstea\FileUploadBundle\Service\FileManagerInterface;
use Rhumsaa\Uuid\Uuid; use Rhumsaa\Uuid\Uuid;
use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\DataTransformerInterface;
use Traversable; use Symfony\Component\Form\Exception\TransformationFailedException;
/** /**
* Description of UploadedFileTransformer * Description of UploadedFileTransformer
...@@ -44,13 +44,22 @@ class UploadedFileTransformer implements DataTransformerInterface ...@@ -44,13 +44,22 @@ class UploadedFileTransformer implements DataTransformerInterface
if(!$this->multiple) { if(!$this->multiple) {
return $this->reverseTransformFile($value); return $this->reverseTransformFile($value);
} }
if(null === $value || '' === $value) {
return [];
}
if(!is_array($value) && !$value instanceof \Traversable) {
throw new TransformationFailedException('Expected an array or a \Traversable.');
}
$result = []; $result = [];
if(is_array($value) || $value instanceof Traversable) { foreach($value as $index => $identifier) {
foreach($value as $index => $file) { if(null !== $file = $this->reverseTransformFile($identifier)) {
$result[$index] = $this->reverseTransformFile($file); $result[$index] = $file;
} }
} }
return array_filter($result); return $result;
} }
/** /**
...@@ -61,13 +70,22 @@ class UploadedFileTransformer implements DataTransformerInterface ...@@ -61,13 +70,22 @@ class UploadedFileTransformer implements DataTransformerInterface
if(!$this->multiple) { if(!$this->multiple) {
return $this->transformFile($value); return $this->transformFile($value);
} }
if(empty($value)) {
return [];
}
if(!is_array($value) && !$value instanceof \Traversable) {
throw new TransformationFailedException('Expected an array or a \Traversable.');
}
$result = []; $result = [];
if(is_array($value) || $value instanceof Traversable) { foreach($value as $index => $file) {
foreach($value as $index => $file) { if(null !== $item = $this->transformFile($file)) {
$result[$index] = $this->transformFile($file); $result[$index] = $item;
} }
} }
return array_filter($result); return $result;
} }
/** /**
...@@ -77,7 +95,15 @@ class UploadedFileTransformer implements DataTransformerInterface ...@@ -77,7 +95,15 @@ class UploadedFileTransformer implements DataTransformerInterface
*/ */
protected function transformFile($value) protected function transformFile($value)
{ {
return $value instanceof UploadedFile ? $value : null; if(null === $value) {
return null;
}
if(!$value instanceof UploadedFile) {
throw new TransformationFailedException('Expected an UploadedFile.');
}
return $value;
} }
/** /**
...@@ -90,12 +116,18 @@ class UploadedFileTransformer implements DataTransformerInterface ...@@ -90,12 +116,18 @@ class UploadedFileTransformer implements DataTransformerInterface
if(is_string($value) && Uuid::isValid($value)) { if(is_string($value) && Uuid::isValid($value)) {
$value = $this->fileManager->get($value); $value = $this->fileManager->get($value);
} }
if(null === $value || '' === $value) {
return null;
}
if($value instanceof UploadedFile) { if($value instanceof UploadedFile) {
if($value->isOrphelin()) { if($value->isOrphelin()) {
$value->setEtat(UploadedFile::ETAT_NORMAL); $value->setEtat(UploadedFile::ETAT_NORMAL);
} }
return $value; return $value;
} }
return null;
throw new TransformationFailedException('Expected an UploadedFile or a valid file UUID.');
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment