|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\DataTransformer; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; |
|
17
|
|
|
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface; |
|
18
|
|
|
use ApiPlatform\Core\DataTransformer\DataTransformerInterface; |
|
19
|
|
|
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; |
|
20
|
|
|
use ApiPlatform\Core\Util\AttributesExtractor; |
|
21
|
|
|
use ApiPlatform\Core\Util\RequestParser; |
|
22
|
|
|
use Silverback\ApiComponentsBundle\ApiPlatform\CollectionHelper; |
|
23
|
|
|
use Silverback\ApiComponentsBundle\Entity\Component\Collection; |
|
24
|
|
|
use Silverback\ApiComponentsBundle\Exception\OutOfBoundsException; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author Daniel West <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class CollectionOutputDataTransformer implements DataTransformerInterface |
|
31
|
|
|
{ |
|
32
|
|
|
private CollectionHelper $iriConverter; |
|
33
|
|
|
private CollectionDataProviderInterface $collectionDataProvider; |
|
34
|
|
|
private RequestStack $requestStack; |
|
35
|
|
|
private SerializerContextBuilderInterface $serializerContextBuilder; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(CollectionHelper $iriConverter, CollectionDataProviderInterface $collectionDataProvider, RequestStack $requestStack, SerializerContextBuilderInterface $serializerContextBuilder) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->iriConverter = $iriConverter; |
|
40
|
|
|
$this->collectionDataProvider = $collectionDataProvider; |
|
41
|
|
|
$this->requestStack = $requestStack; |
|
42
|
|
|
$this->serializerContextBuilder = $serializerContextBuilder; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function supportsTransformation($data, string $to, array $context = []): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return $data instanceof Collection && Collection::class === $to; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Collection $object |
|
52
|
|
|
*/ |
|
53
|
|
|
public function transform($object, string $to, array $context = []) |
|
54
|
|
|
{ |
|
55
|
|
|
$parameters = $this->iriConverter->getRouterParametersFromIri($object->getResourceIri()); |
|
56
|
|
|
$attributes = AttributesExtractor::extractAttributes($parameters); |
|
57
|
|
|
$request = $this->requestStack->getMasterRequest(); |
|
58
|
|
|
if (!$request) { |
|
59
|
|
|
$filters = null; |
|
60
|
|
|
} else { |
|
61
|
|
|
$queryString = RequestParser::getQueryString($request); |
|
62
|
|
|
$filters = $queryString ? RequestParser::parseRequestParams($queryString) : null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($this->collectionDataProvider instanceof ContextAwareCollectionDataProviderInterface) { |
|
66
|
|
|
$collectionContext = null === $filters ? [] : ['filters' => $filters]; |
|
67
|
|
|
|
|
68
|
|
|
// Comment copied from ApiPlatform\Core\EventListener\ReadListener |
|
69
|
|
|
// Builtin data providers are able to use the serialization context to automatically add join clauses |
|
70
|
|
|
$collectionContext += $normalizationContext = $this->serializerContextBuilder->createFromRequest( |
|
71
|
|
|
$request, |
|
|
|
|
|
|
72
|
|
|
true, |
|
73
|
|
|
$attributes |
|
74
|
|
|
); |
|
75
|
|
|
$request->attributes->set('_api_normalization_context', $normalizationContext); |
|
76
|
|
|
|
|
77
|
|
|
$collectionData = $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $collectionContext); |
|
|
|
|
|
|
78
|
|
|
} else { |
|
79
|
|
|
$collectionData = $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name']); |
|
80
|
|
|
} |
|
81
|
|
|
if (!$collectionData instanceof \Traversable) { |
|
82
|
|
|
throw new OutOfBoundsException('$collectionData should be Traversable'); |
|
83
|
|
|
} |
|
84
|
|
|
$object->setCollection(iterator_count($collectionData) ? $collectionData : null); |
|
85
|
|
|
|
|
86
|
|
|
return $object; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|