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

api-platform / core / 6693023961

30 Oct 2023 12:51PM UTC coverage: 67.319%. Remained the same
6693023961

push

github

web-flow
ci: php cs fixer (#5905)

--


fix cs


fix last cs

259 of 259 new or added lines in 83 files covered. (100.0%)

15610 of 23188 relevant lines covered (67.32%)

10.87 hits per line

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

63.33
/src/GraphQl/Serializer/ItemNormalizer.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\Api\IdentifiersExtractorInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Core\Api\IdentifiersExtractorInterface as LegacyIdentifiersExtractorInterface;
19
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
20
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\Serializer\ItemNormalizer as BaseItemNormalizer;
23
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
24
use ApiPlatform\Util\ClassInfoTrait;
25
use Psr\Log\LoggerInterface;
26
use Psr\Log\NullLogger;
27
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
28
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
29
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
30
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
31

32
/**
33
 * GraphQL normalizer.
34
 *
35
 * @author Kévin Dunglas <dunglas@gmail.com>
36
 */
37
final class ItemNormalizer extends BaseItemNormalizer
38
{
39
    use ClassInfoTrait;
40

41
    public const FORMAT = 'graphql';
42
    public const ITEM_RESOURCE_CLASS_KEY = '#itemResourceClass';
43
    public const ITEM_IDENTIFIERS_KEY = '#itemIdentifiers';
44

45
    /**
46
     * @var IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface
47
     */
48
    private $identifiersExtractor;
49

50
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $identifiersExtractor, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, ItemDataProviderInterface $itemDataProvider = null, bool $allowPlainIdentifiers = false, LoggerInterface $logger = null, iterable $dataTransformers = [], ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, ResourceAccessCheckerInterface $resourceAccessChecker = null)
51
    {
52
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $itemDataProvider, $allowPlainIdentifiers, $logger ?: new NullLogger(), $dataTransformers, $resourceMetadataCollectionFactory, $resourceAccessChecker);
30✔
53

54
        $this->identifiersExtractor = $identifiersExtractor;
30✔
55
    }
56

57
    public function supportsNormalization($data, $format = null, array $context = []): bool
58
    {
59
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
14✔
60
    }
61

62
    /**
63
     * @param array<string, mixed> $context
64
     * @param mixed|null           $format
65
     *
66
     * @throws UnexpectedValueException
67
     *
68
     * @return array|string|int|float|bool|\ArrayObject|null
69
     */
70
    public function normalize($object, $format = null, array $context = [])
71
    {
72
        $resourceClass = $this->getObjectClass($object);
2✔
73

74
        if ($outputClass = $this->getOutputClass($resourceClass, $context)) {
2✔
75
            $context['graphql_identifiers'] = [
×
76
                self::ITEM_RESOURCE_CLASS_KEY => $context['operation']->getClass(),
×
77
                self::ITEM_IDENTIFIERS_KEY => $this->identifiersExtractor->getIdentifiersFromItem($object),
×
78
            ];
×
79

80
            return parent::normalize($object, $format, $context);
×
81
        }
82

83
        unset($context['operation_name'], $context['operation']);
2✔
84
        $data = parent::normalize($object, $format, $context);
2✔
85
        if (!\is_array($data)) {
2✔
86
            throw new UnexpectedValueException('Expected data to be an array.');
×
87
        }
88

89
        if (isset($context['graphql_identifiers'])) {
2✔
90
            $data += $context['graphql_identifiers'];
×
91
        } elseif (!($context['no_resolver_data'] ?? false)) {
2✔
92
            $data[self::ITEM_RESOURCE_CLASS_KEY] = $resourceClass;
1✔
93
            $data[self::ITEM_IDENTIFIERS_KEY] = $this->identifiersExtractor->getIdentifiersFromItem($object, $context['operation'] ?? null);
1✔
94
        }
95

96
        return $data;
2✔
97
    }
98

99
    protected function normalizeCollectionOfRelations($propertyMetadata, $attributeValue, string $resourceClass, ?string $format, array $context): array
100
    {
101
        // to-many are handled directly by the GraphQL resolver
102
        return [];
×
103
    }
104

105
    public function supportsDenormalization($data, $type, $format = null, array $context = [])
106
    {
107
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
4✔
108
    }
109

110
    /**
111
     * @return array|bool
112
     */
113
    protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
114
    {
115
        $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
3✔
116

117
        if (($context['api_denormalize'] ?? false) && \is_array($allowedAttributes) && false !== ($indexId = array_search('id', $allowedAttributes, true))) {
3✔
118
            $allowedAttributes[] = '_id';
×
119
            array_splice($allowedAttributes, (int) $indexId, 1);
×
120
        }
121

122
        return $allowedAttributes;
3✔
123
    }
124

125
    protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = []): void
126
    {
127
        if ('_id' === $attribute) {
1✔
128
            $attribute = 'id';
×
129
        }
130

131
        parent::setAttributeValue($object, $attribute, $value, $format, $context);
1✔
132
    }
133
}
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