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\ResourceClassNotFoundException; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Manipulates the property factory options. |
23
|
|
|
* |
24
|
|
|
* @internal |
25
|
|
|
* |
26
|
|
|
* @author @author Alan Poulain <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait PropertyFactoryOptionsTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Gets the options for a property factory. |
32
|
|
|
*/ |
33
|
|
|
private function getPropertyFactoryOptions(string $resourceClass, bool $splitDeserialization = false): array |
34
|
|
|
{ |
35
|
|
|
$serializationContext = $this->getSerializationContext($resourceClass, true); |
36
|
|
|
$deserializationContext = $this->getSerializationContext($resourceClass, false); |
37
|
|
|
$serializationGroups = (array) ($serializationContext[AbstractNormalizer::GROUPS] ?? []); |
38
|
|
|
$deserializationGroups = (array) ($deserializationContext[AbstractNormalizer::GROUPS] ?? []); |
39
|
|
|
|
40
|
|
|
if (!$splitDeserialization) { |
41
|
|
|
$serializationGroups = array_unique(array_merge($serializationGroups, $deserializationGroups)); |
42
|
|
|
$deserializationGroups = []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$options = []; |
46
|
|
|
if ($serializationGroups) { |
47
|
|
|
$options['serializer_groups'] = $serializationGroups; |
48
|
|
|
} |
49
|
|
|
if ($deserializationGroups) { |
|
|
|
|
50
|
|
|
$options['deserializer_groups'] = $deserializationGroups; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $options; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the serialization context using the serializer context builder if available or the resource attributes if not. |
58
|
|
|
*/ |
59
|
|
|
private function getSerializationContext(string $resourceClass, bool $normalization, ?string $operationType = null, ?string $operationName = null): array |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
if ($this->serializerContextBuilder) { |
63
|
|
|
switch ($operationType) { |
64
|
|
|
case OperationType::ITEM: |
65
|
|
|
$operationKey = 'item_operation_name'; |
66
|
|
|
break; |
67
|
|
|
case OperationType::COLLECTION: |
68
|
|
|
$operationKey = 'collection_operation_name'; |
69
|
|
|
break; |
70
|
|
|
case OperationType::SUBRESOURCE: |
71
|
|
|
$operationKey = 'subresource_operation_name'; |
72
|
|
|
break; |
73
|
|
|
default: |
74
|
|
|
$operationKey = 'resource_operation_name'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$request = Request::createFromGlobals(); |
78
|
|
|
|
79
|
|
|
return $this->serializerContextBuilder->createFromRequest($request, $normalization, [ |
80
|
|
|
'resource_class' => $resourceClass, |
81
|
|
|
$operationKey => $operationName ?? 'resource', |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
86
|
|
|
$attribute = $normalization ? 'normalization_context' : 'denormalization_context'; |
87
|
|
|
|
88
|
|
|
if (null === $operationType || null === $operationName) { |
89
|
|
|
return $resourceMetadata->getAttribute($attribute, []); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, [], true); |
93
|
|
|
} catch (ResourceClassNotFoundException $exception) { |
94
|
|
|
return []; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|