|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Form\DataTransformer; |
|
3
|
|
|
|
|
4
|
|
|
use App\Entity\Collection; |
|
5
|
|
|
use App\Entity\ItemCollection; |
|
6
|
|
|
use App\Repository\CollectionRepository; |
|
7
|
|
|
use App\Repository\Exception\CollectionNotFoundException; |
|
8
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
9
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
|
10
|
|
|
use Ramsey\Uuid\Exception\InvalidUuidStringException; |
|
11
|
|
|
|
|
12
|
|
|
class UuidToCollectionTransformer implements DataTransformerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private $collectionRepository; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(CollectionRepository $collectionRepository) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->collectionRepository = $collectionRepository; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Transforms an object (collections) to a UuidInterface |
|
23
|
|
|
* |
|
24
|
|
|
* @param Collection[]|null $collections |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
public function reverseTransform($collections) |
|
28
|
|
|
{ |
|
29
|
|
|
if (null == $collections) { |
|
30
|
|
|
return []; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$result = []; |
|
34
|
|
|
|
|
35
|
|
|
foreach ($collections as $collection) { |
|
36
|
|
|
$result[] = $collection->getId(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $result; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Transforms a UuidInterface to an object (Collection). |
|
44
|
|
|
* |
|
45
|
|
|
* @param ItemCollection[]|null $itemCollections |
|
46
|
|
|
* @return Collection[]|null |
|
47
|
|
|
* @throws TransformationFailedException if object (Collection) is not found. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function transform($itemCollections) |
|
50
|
|
|
{ |
|
51
|
|
|
if (!$itemCollections) { |
|
52
|
|
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$result = []; |
|
56
|
|
|
foreach ($itemCollections as $itemCollection) { |
|
57
|
|
|
$collectionId = $itemCollection->getCollection()->getId(); |
|
58
|
|
|
try { |
|
59
|
|
|
$collection = $this->collectionRepository->find($collectionId); |
|
60
|
|
|
} catch (CollectionNotFoundException $e) { |
|
61
|
|
|
throw new TransformationFailedException(sprintf('Collection "%s" not found !', $collectionId->toString())); |
|
62
|
|
|
} catch (InvalidUuidStringException $e) { |
|
63
|
|
|
throw new TransformationFailedException(sprintf('Invalid UUID "%s" !', $collectionId->toString())); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (null === $collection) { |
|
67
|
|
|
throw new TransformationFailedException(sprintf('An collection with id "%s" does not exist!', $collectionId->toString())); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$result[] = $collection; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $result; |
|
74
|
|
|
} |
|
75
|
|
|
} |