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\Identifier\Normalizer\ChainIdentifierDenormalizer; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
20
|
|
|
|
21
|
|
|
trait OperationDataProviderTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var CollectionDataProviderInterface |
25
|
|
|
*/ |
26
|
|
|
private $collectionDataProvider; |
27
|
|
|
/** |
28
|
|
|
* @var ItemDataProviderInterface |
29
|
|
|
*/ |
30
|
|
|
private $itemDataProvider; |
31
|
|
|
/** |
32
|
|
|
* @var SubresourceDataProviderInterface |
33
|
|
|
*/ |
34
|
|
|
private $subresourceDataProvider; |
35
|
|
|
/** |
36
|
|
|
* @var ChainIdentifierDenormalizer |
37
|
|
|
*/ |
38
|
|
|
private $identifierDenormalizer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieves data for a collection operation. |
42
|
|
|
* |
43
|
|
|
* @return array|\Traversable|null |
44
|
|
|
*/ |
45
|
|
|
private function getCollectionData(Request $request, array $attributes, array $context) |
46
|
|
|
{ |
47
|
|
|
if ($request->isMethod('POST')) { |
48
|
|
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $context); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Gets data for an item operation. |
56
|
|
|
* |
57
|
|
|
* @throws NotFoundHttpException |
58
|
|
|
* |
59
|
|
|
* @return object|null |
60
|
|
|
*/ |
61
|
|
|
private function getItemData(Request $request, array $attributes, array $context) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$id = $request->attributes->get('id'); |
64
|
|
|
$context = []; |
65
|
|
|
|
66
|
|
|
try { |
67
|
|
|
if ($this->identifierDenormalizer) { |
68
|
|
|
$id = $this->identifierDenormalizer->denormalize((string) $id, $attributes['resource_class']); |
69
|
|
|
$context = [ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER => true]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$data = $this->itemDataProvider->getItem($attributes['resource_class'], $id, $attributes['item_operation_name'], $context); |
73
|
|
|
} catch (InvalidIdentifierException $e) { |
74
|
|
|
$data = null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (null === $data) { |
78
|
|
|
throw new NotFoundHttpException('Not Found'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $data; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets data for a nested operation. |
86
|
|
|
* |
87
|
|
|
* @throws NotFoundHttpException |
88
|
|
|
* @throws RuntimeException |
89
|
|
|
* |
90
|
|
|
* @return object|null |
91
|
|
|
*/ |
92
|
|
|
private function getSubresourceData(Request $request, array $attributes, array $context) |
93
|
|
|
{ |
94
|
|
|
if (null === $this->subresourceDataProvider) { |
95
|
|
|
throw new RuntimeException('No subresource data provider.'); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$attributes['subresource_context'] += $context; |
99
|
|
|
$identifiers = []; |
100
|
|
|
if ($this->identifierDenormalizer) { |
101
|
|
|
$attributes['subresource_context'][ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] = true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass, $hasIdentifier)) { |
105
|
|
|
if (false === $hasIdentifier) { |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$identifiers[$id] = $request->attributes->get($id); |
110
|
|
|
|
111
|
|
|
if ($this->identifierDenormalizer) { |
112
|
|
|
try { |
113
|
|
|
$identifiers[$id] = $this->identifierDenormalizer->denormalize((string) $identifiers[$id], $resourceClass); |
114
|
|
|
} catch (InvalidIdentifierException $e) { |
115
|
|
|
throw new NotFoundHttpException('Not Found'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$data = $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'], $attributes['subresource_operation_name']); |
121
|
|
|
|
122
|
|
|
if (null === $data) { |
123
|
|
|
throw new NotFoundHttpException('Not Found.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $data; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.