1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the API Platform project. |
||
5 | * |
||
6 | * (c) Kévin Dunglas <[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 ApiPlatform\Core\DataProvider; |
||
15 | |||
16 | use ApiPlatform\Core\Exception\InvalidIdentifierException; |
||
17 | use ApiPlatform\Core\Exception\RuntimeException; |
||
18 | use ApiPlatform\Core\Identifier\IdentifierConverterInterface; |
||
19 | |||
20 | /** |
||
21 | * @internal |
||
22 | */ |
||
23 | trait OperationDataProviderTrait |
||
24 | { |
||
25 | /** |
||
26 | * @var CollectionDataProviderInterface |
||
27 | */ |
||
28 | private $collectionDataProvider; |
||
29 | |||
30 | /** |
||
31 | * @var ItemDataProviderInterface |
||
32 | */ |
||
33 | private $itemDataProvider; |
||
34 | |||
35 | /** |
||
36 | * @var SubresourceDataProviderInterface|null |
||
37 | */ |
||
38 | private $subresourceDataProvider; |
||
39 | |||
40 | /** |
||
41 | * @var IdentifierConverterInterface|null |
||
42 | */ |
||
43 | private $identifierConverter; |
||
44 | |||
45 | /** |
||
46 | * Retrieves data for a collection operation. |
||
47 | * |
||
48 | * @return iterable|null |
||
49 | */ |
||
50 | private function getCollectionData(array $attributes, array $context) |
||
51 | { |
||
52 | return $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $context); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Gets data for an item operation. |
||
57 | * |
||
58 | * @return object|null |
||
59 | */ |
||
60 | private function getItemData($identifiers, array $attributes, array $context) |
||
61 | { |
||
62 | return $this->itemDataProvider->getItem($attributes['resource_class'], $identifiers, $attributes['item_operation_name'], $context); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Gets data for a nested operation. |
||
67 | * |
||
68 | * @throws RuntimeException |
||
69 | * |
||
70 | * @return array|object|null |
||
71 | */ |
||
72 | private function getSubresourceData($identifiers, array $attributes, array $context) |
||
73 | { |
||
74 | if (null === $this->subresourceDataProvider) { |
||
75 | throw new RuntimeException('Subresources not supported'); |
||
76 | } |
||
77 | |||
78 | return $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'] + $context, $attributes['subresource_operation_name']); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param array $parameters - usually comes from $request->attributes->all() |
||
83 | * |
||
84 | * @throws InvalidIdentifierException |
||
85 | */ |
||
86 | private function extractIdentifiers(array $parameters, array $attributes) |
||
87 | { |
||
88 | if (isset($attributes['item_operation_name'])) { |
||
89 | if (!isset($parameters['id'])) { |
||
90 | throw new InvalidIdentifierException('Parameter "id" not found'); |
||
91 | } |
||
92 | |||
93 | $id = $parameters['id']; |
||
94 | |||
95 | if (null !== $this->identifierConverter) { |
||
96 | return $this->identifierConverter->convert((string) $id, $attributes['resource_class']); |
||
97 | } |
||
98 | |||
99 | return $id; |
||
100 | } |
||
101 | |||
102 | if (!isset($attributes['subresource_context'])) { |
||
103 | throw new RuntimeException('Either "item_operation_name" or "collection_operation_name" must be defined, unless the "_api_receive" request attribute is set to false.'); |
||
104 | } |
||
105 | |||
106 | $identifiers = []; |
||
107 | |||
108 | foreach ($attributes['subresource_context']['identifiers'] as $key => [$id, $resourceClass, $hasIdentifier]) { |
||
109 | if (false === $hasIdentifier) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | $identifiers[$id] = $parameters[$id]; |
||
114 | |||
115 | if (null !== $this->identifierConverter) { |
||
116 | $identifiers[$id] = $this->identifierConverter->convert((string) $identifiers[$id], $resourceClass); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | return $identifiers; |
||
121 | } |
||
122 | } |
||
123 |