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\Serializer; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\OperationType; |
17
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
18
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
19
|
|
|
use ApiPlatform\Core\Util\RequestAttributesExtractor; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
* |
25
|
|
|
* @author Kévin Dunglas <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class SerializerContextBuilder implements SerializerContextBuilderInterface |
28
|
|
|
{ |
29
|
|
|
private $resourceMetadataFactory; |
30
|
|
|
|
31
|
|
|
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory) |
32
|
|
|
{ |
33
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array |
40
|
|
|
{ |
41
|
|
|
if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) { |
42
|
|
|
throw new RuntimeException('Request attributes are not valid.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); |
46
|
|
|
$key = $normalization ? 'normalization_context' : 'denormalization_context'; |
47
|
|
|
|
48
|
|
|
$operationKey = null; |
49
|
|
|
$operationType = null; |
50
|
|
|
|
51
|
|
|
if (isset($attributes['collection_operation_name'])) { |
52
|
|
|
$operationKey = 'collection_operation_name'; |
53
|
|
|
$operationType = OperationType::COLLECTION; |
54
|
|
|
} elseif (isset($attributes['subresource_operation_name'])) { |
55
|
|
|
$operationKey = 'subresource_operation_name'; |
56
|
|
|
$operationType = OperationType::SUBRESOURCE; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (null !== $operationKey) { |
60
|
|
|
$attribute = $attributes[$operationKey]; |
61
|
|
|
$context = $resourceMetadata->getCollectionOperationAttribute($attribute, $key, [], true); |
62
|
|
|
$context[$operationKey] = $attribute; |
63
|
|
|
} else { |
64
|
|
|
$context = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], $key, [], true); |
65
|
|
|
$context['item_operation_name'] = $attributes['item_operation_name']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$context['operation_type'] = $operationType ? $operationType : OperationType::ITEM; |
69
|
|
|
|
70
|
|
|
if (!$normalization && !isset($context['api_allow_update'])) { |
71
|
|
|
$context['api_allow_update'] = Request::METHOD_PUT === $request->getMethod(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$context['resource_class'] = $attributes['resource_class']; |
75
|
|
|
$context['request_uri'] = $request->getRequestUri(); |
76
|
|
|
|
77
|
|
|
if (isset($attributes['subresource_context'])) { |
78
|
|
|
$context['subresource_identifiers'] = []; |
79
|
|
|
|
80
|
|
|
foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass)) { |
81
|
|
|
if (!isset($context['subresource_resources'][$resourceClass])) { |
82
|
|
|
$context['subresource_resources'][$resourceClass] = []; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$context['subresource_identifiers'][$id] = $context['subresource_resources'][$resourceClass][$id] = $request->attributes->get($id); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (isset($attributes['subresource_property'])) { |
90
|
|
|
$context['subresource_property'] = $attributes['subresource_property']; |
91
|
|
|
$context['subresource_resource_class'] = $attributes['subresource_resource_class'] ?? null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $context; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|