• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/src/GraphQl/Serializer/SerializerContextBuilder.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\GraphQl\Serializer;
15

16
use ApiPlatform\Metadata\GraphQl\Mutation;
17
use ApiPlatform\Metadata\GraphQl\Operation;
18
use ApiPlatform\Metadata\GraphQl\Subscription;
19
use GraphQL\Type\Definition\ResolveInfo;
20
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
21
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
22
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
23

24
/**
25
 * Builds the context used by the Symfony Serializer.
26
 *
27
 * @author Alan Poulain <contact@alanpoulain.eu>
28
 */
29
final class SerializerContextBuilder implements SerializerContextBuilderInterface
30
{
31
    public function __construct(private readonly ?NameConverterInterface $nameConverter)
32
    {
UNCOV
33
    }
1,392✔
34

35
    public function create(?string $resourceClass, Operation $operation, array $resolverContext, bool $normalization): array
36
    {
UNCOV
37
        $context = ['resource_class' => $resourceClass, 'operation_name' => $operation->getName(), 'graphql_operation_name' => $operation->getName()];
365✔
38

UNCOV
39
        if (isset($resolverContext['fields'])) {
365✔
40
            $context['no_resolver_data'] = true;
3✔
41
        }
42

UNCOV
43
        $context['operation'] = $operation;
365✔
UNCOV
44
        if ($operation->getInput()) {
365✔
45
            $context['input'] = $operation->getInput();
15✔
46
        }
UNCOV
47
        if ($operation->getOutput()) {
365✔
48
            $context['output'] = $operation->getOutput();
12✔
49
        }
UNCOV
50
        $context = $normalization ? array_merge($operation->getNormalizationContext() ?? [], $context) : array_merge($operation->getDenormalizationContext() ?? [], $context);
365✔
51

UNCOV
52
        if ($normalization) {
365✔
UNCOV
53
            $context['attributes'] = $this->fieldsToAttributes($resourceClass, $operation, $resolverContext, $context);
365✔
54
        }
55

UNCOV
56
        return $context;
365✔
57
    }
58

59
    /**
60
     * Retrieves fields, recursively replaces the "_id" key (the raw id) by "id" (the name of the property expected by the Serializer) and flattens edge and node structures (pagination).
61
     */
62
    private function fieldsToAttributes(?string $resourceClass, Operation $operation, array $resolverContext, array $context): array
63
    {
UNCOV
64
        if (isset($resolverContext['fields'])) {
365✔
65
            $fields = $resolverContext['fields'];
3✔
66
        } else {
67
            /** @var ResolveInfo $info */
UNCOV
68
            $info = $resolverContext['info'];
362✔
UNCOV
69
            $fields = $info->getFieldSelection(\PHP_INT_MAX);
362✔
70
        }
71

UNCOV
72
        $attributes = $this->replaceIdKeys($fields['edges']['node'] ?? $fields['collection'] ?? $fields, $resourceClass, $context);
365✔
73

UNCOV
74
        if ($operation instanceof Subscription || $operation instanceof Mutation) {
365✔
75
            $wrapFieldName = lcfirst($operation->getShortName());
106✔
76

77
            return $attributes[$wrapFieldName] ?? [];
106✔
78
        }
79

UNCOV
80
        return $attributes;
278✔
81
    }
82

83
    private function replaceIdKeys(array $fields, ?string $resourceClass, array $context): array
84
    {
UNCOV
85
        $denormalizedFields = [];
365✔
86

UNCOV
87
        foreach ($fields as $key => $value) {
365✔
UNCOV
88
            if ('_id' === $key) {
365✔
89
                $denormalizedFields['id'] = $fields['_id'];
13✔
90

91
                continue;
13✔
92
            }
93

UNCOV
94
            $denormalizedFields[$this->denormalizePropertyName((string) $key, $resourceClass, $context)] = \is_array($value) ? $this->replaceIdKeys($value, $resourceClass, $context) : $value;
362✔
95
        }
96

UNCOV
97
        return $denormalizedFields;
365✔
98
    }
99

100
    private function denormalizePropertyName(string $property, ?string $resourceClass, array $context): string
101
    {
UNCOV
102
        if (null === $this->nameConverter) {
362✔
103
            return $property;
×
104
        }
UNCOV
105
        if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
362✔
UNCOV
106
            return $this->nameConverter->denormalize($property, $resourceClass, null, $context);
362✔
107
        }
108

109
        return $this->nameConverter->denormalize($property);
×
110
    }
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc