forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializerContextBuilder.php
More file actions
91 lines (76 loc) · 3.53 KB
/
Copy pathSerializerContextBuilder.php
File metadata and controls
91 lines (76 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Serializer;
use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Util\RequestAttributesExtractor;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* {@inheritdoc}
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class SerializerContextBuilder implements SerializerContextBuilderInterface
{
public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
{
}
/**
* {@inheritdoc}
*/
public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
{
if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
throw new RuntimeException('Request attributes are not valid.');
}
$operation = $attributes['operation'] ?? $this->resourceMetadataFactory->create($attributes['resource_class'])->getOperation($attributes['operation_name']);
$context = $normalization ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
$context['operation_name'] = $operation->getName();
$context['operation'] = $operation;
$context['resource_class'] = $attributes['resource_class'];
$context['skip_null_values'] ??= true;
$context['iri_only'] ??= false;
$context['request_uri'] = $request->getRequestUri();
$context['uri'] = $request->getUri();
$context['input'] = $operation->getInput();
$context['output'] = $operation->getOutput();
if ($operation->getTypes()) {
$context['types'] = $operation->getTypes();
}
if ($operation->getUriVariables()) {
$context['uri_variables'] = [];
foreach (array_keys($operation->getUriVariables()) as $parameterName) {
$context['uri_variables'][$parameterName] = $request->attributes->get($parameterName);
}
}
if (!$normalization) {
if (!isset($context['api_allow_update'])) {
$context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
if ($context['api_allow_update'] && 'PATCH' === $method) {
$context['deep_object_to_populate'] ??= true;
}
}
if ('csv' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType())) {
$context[CsvEncoder::AS_COLLECTION_KEY] = false;
}
}
if ($operation->getCollectDenormalizationErrors() ?? false) {
$context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS] = true;
}
// to keep the cache computation smaller, we have "operation_name" and "iri" anyways
$context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'root_operation';
$context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'operation';
return $context;
}
}