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\GraphQl\Resolver\Stage; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer; |
17
|
|
|
use ApiPlatform\Core\GraphQl\Serializer\SerializerContextBuilderInterface; |
18
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
20
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Deserialize stage of GraphQL resolvers. |
24
|
|
|
* |
25
|
|
|
* @experimental |
26
|
|
|
* |
27
|
|
|
* @author Alan Poulain <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class DeserializeStage implements DeserializeStageInterface |
30
|
|
|
{ |
31
|
|
|
private $resourceMetadataFactory; |
32
|
|
|
private $denormalizer; |
33
|
|
|
private $serializerContextBuilder; |
34
|
|
|
|
35
|
|
|
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, DenormalizerInterface $denormalizer, SerializerContextBuilderInterface $serializerContextBuilder) |
36
|
|
|
{ |
37
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
38
|
|
|
$this->denormalizer = $denormalizer; |
39
|
|
|
$this->serializerContextBuilder = $serializerContextBuilder; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function __invoke($objectToPopulate, string $resourceClass, string $operationName, array $context) |
46
|
|
|
{ |
47
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
48
|
|
|
if (!$resourceMetadata->getGraphqlAttribute($operationName, 'deserialize', true, true)) { |
49
|
|
|
return $objectToPopulate; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$denormalizationContext = $this->serializerContextBuilder->create($resourceClass, $operationName, $context, false); |
53
|
|
|
if (null !== $objectToPopulate) { |
54
|
|
|
$denormalizationContext[AbstractNormalizer::OBJECT_TO_POPULATE] = $objectToPopulate; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->denormalizer->denormalize($context['args']['input'], $resourceClass, ItemNormalizer::FORMAT, $denormalizationContext); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|