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

api-platform / core / 5644825543

pending completion
5644825543

push

github

web-flow
feat(serializer): support for getSupportedTypes (symfony 6.3) (#5672)

109 of 109 new or added lines in 29 files covered. (100.0%)

10881 of 18245 relevant lines covered (59.64%)

20.04 hits per line

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

87.5
/src/Hydra/Serializer/CollectionFiltersNormalizer.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\Hydra\Serializer;
15

16
use ApiPlatform\Api\FilterInterface;
17
use ApiPlatform\Api\FilterLocatorTrait;
18
use ApiPlatform\Api\ResourceClassResolverInterface;
19
use ApiPlatform\Doctrine\Orm\State\Options;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use Psr\Container\ContainerInterface;
22
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
23
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
24
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
25
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
26
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
27

28
/**
29
 * Enhances the result of collection by adding the filters applied on collection.
30
 *
31
 * @author Samuel ROZE <samuel.roze@gmail.com>
32
 */
33
final class CollectionFiltersNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
34
{
35
    use FilterLocatorTrait;
36

37
    /**
38
     * @param ContainerInterface $filterLocator The new filter locator or the deprecated filter collection
39
     */
40
    public function __construct(private readonly NormalizerInterface $collectionNormalizer, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ResourceClassResolverInterface $resourceClassResolver, ContainerInterface $filterLocator)
41
    {
42
        $this->setFilterLocator($filterLocator);
68✔
43
    }
44

45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
49
    {
50
        return $this->collectionNormalizer->supportsNormalization($data, $format, $context);
22✔
51
    }
52

53
    public function getSupportedTypes($format): array
54
    {
55
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
56
        if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
28✔
57
            return ['*' => $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod()];
×
58
        }
59

60
        return $this->collectionNormalizer->getSupportedTypes($format);
28✔
61
    }
62

63
    public function hasCacheableSupportsMethod(): bool
64
    {
65
        trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
2✔
66

67
        return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
2✔
68
    }
69

70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
74
    {
75
        if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && $object instanceof \ArrayObject && !\count($object)) {
32✔
76
            return $object;
×
77
        }
78

79
        $data = $this->collectionNormalizer->normalize($object, $format, $context);
32✔
80
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
32✔
81
            return $data;
10✔
82
        }
83

84
        if (!\is_array($data)) {
22✔
85
            throw new UnexpectedValueException('Expected data to be an array');
×
86
        }
87
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
22✔
88
        $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
22✔
89
        $resourceFilters = $operation->getFilters();
22✔
90
        if (!$resourceFilters) {
22✔
91
            return $data;
14✔
92
        }
93
        $requestParts = parse_url($context['request_uri'] ?? '');
8✔
94
        if (!\is_array($requestParts)) {
8✔
95
            return $data;
×
96
        }
97
        $currentFilters = [];
8✔
98
        foreach ($resourceFilters as $filterId) {
8✔
99
            if ($filter = $this->getFilter($filterId)) {
8✔
100
                $currentFilters[] = $filter;
6✔
101
            }
102
        }
103

104
        if (($options = $operation?->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
8✔
105
            $resourceClass = $options->getEntityClass();
×
106
        }
107

108
        if ($currentFilters) {
8✔
109
            $data['hydra:search'] = $this->getSearch($resourceClass, $requestParts, $currentFilters);
6✔
110
        }
111

112
        return $data;
8✔
113
    }
114

115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function setNormalizer(NormalizerInterface $normalizer): void
119
    {
120
        if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
54✔
121
            $this->collectionNormalizer->setNormalizer($normalizer);
54✔
122
        }
123
    }
124

125
    /**
126
     * Returns the content of the Hydra search property.
127
     *
128
     * @param FilterInterface[] $filters
129
     */
130
    private function getSearch(string $resourceClass, array $parts, array $filters): array
131
    {
132
        $variables = [];
6✔
133
        $mapping = [];
6✔
134
        foreach ($filters as $filter) {
6✔
135
            foreach ($filter->getDescription($resourceClass) as $variable => $data) {
6✔
136
                $variables[] = $variable;
6✔
137
                $mapping[] = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $data['property'], 'required' => $data['required']];
6✔
138
            }
139
        }
140

141
        return ['@type' => 'hydra:IriTemplate', 'hydra:template' => sprintf('%s{?%s}', $parts['path'], implode(',', $variables)), 'hydra:variableRepresentation' => 'BasicRepresentation', 'hydra:mapping' => $mapping];
6✔
142
    }
143
}
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

© 2026 Coveralls, Inc