|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\DataProvider\Item; |
|
4
|
|
|
|
|
5
|
|
|
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface; |
|
6
|
|
|
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; |
|
7
|
|
|
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; |
|
8
|
|
|
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; |
|
9
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
10
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Component\Collection\Collection; |
|
11
|
|
|
|
|
12
|
|
|
final class CollectionDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var ManagerRegistry */ |
|
15
|
|
|
private $managerRegistry; |
|
16
|
|
|
/** @var ContextAwareCollectionDataProviderInterface */ |
|
17
|
|
|
private $collectionDataProvider; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* LayoutDataProvider constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param ManagerRegistry $managerRegistry |
|
23
|
|
|
* @param ContextAwareCollectionDataProviderInterface $collectionDataProvider |
|
24
|
|
|
*/ |
|
25
|
3 |
|
public function __construct(ManagerRegistry $managerRegistry, ContextAwareCollectionDataProviderInterface $collectionDataProvider) |
|
26
|
|
|
{ |
|
27
|
3 |
|
$this->managerRegistry = $managerRegistry; |
|
28
|
3 |
|
$this->collectionDataProvider = $collectionDataProvider; |
|
29
|
3 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $resourceClass |
|
33
|
|
|
* @param string|null $operationName |
|
34
|
|
|
* @param array $context |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool |
|
38
|
|
|
{ |
|
39
|
4 |
|
return $resourceClass === Collection::class; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $resourceClass |
|
44
|
|
|
* @param int|string $id |
|
45
|
|
|
* @param string|null $operationName |
|
46
|
|
|
* @param array $context |
|
47
|
|
|
* @return Collection|null |
|
48
|
|
|
* @throws ResourceClassNotSupportedException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) |
|
51
|
|
|
{ |
|
52
|
|
|
$manager = $this->managerRegistry->getManagerForClass($resourceClass); |
|
53
|
|
|
if (null === $manager) { |
|
54
|
|
|
throw new ResourceClassNotSupportedException(sprintf('No manager for the class `%s`', $resourceClass)); |
|
55
|
|
|
} |
|
56
|
|
|
$repository = $manager->getRepository($resourceClass); |
|
57
|
|
|
/** @var Collection|null $collection */ |
|
58
|
|
|
$collection = $repository->find($id); |
|
59
|
|
|
if ($collection) { |
|
60
|
|
|
// In future we should find the resource's data provider in case it isn't default |
|
61
|
|
|
$collection->setCollection($this->collectionDataProvider->getCollection($collection->getResource(), $operationName, $context)); |
|
62
|
|
|
} |
|
63
|
|
|
return $collection; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|