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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

67.35
/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 as LegacyFilterInterface;
17
use ApiPlatform\Api\FilterLocatorTrait;
18
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
19
use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions;
20
use ApiPlatform\Doctrine\Orm\State\Options;
21
use ApiPlatform\Metadata\FilterInterface;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\ResourceClassResolverInterface;
24
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
25
use Psr\Container\ContainerInterface;
26
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
27
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
28
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
29
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
30
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
31
use Symfony\Component\Serializer\Serializer;
32

33
/**
34
 * Enhances the result of collection by adding the filters applied on collection.
35
 *
36
 * @author Samuel ROZE <samuel.roze@gmail.com>
37
 */
38
final class CollectionFiltersNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
39
{
40
    use FilterLocatorTrait;
41

42
    /**
43
     * @param ContainerInterface $filterLocator The new filter locator or the deprecated filter collection
44
     */
45
    public function __construct(private readonly NormalizerInterface $collectionNormalizer, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly LegacyResourceClassResolverInterface|ResourceClassResolverInterface $resourceClassResolver, ContainerInterface $filterLocator)
46
    {
47
        $this->setFilterLocator($filterLocator);
148✔
48
    }
49

50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
54
    {
55
        return $this->collectionNormalizer->supportsNormalization($data, $format, $context);
44✔
56
    }
57

58
    public function getSupportedTypes($format): array
59
    {
60
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
61
        if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
72✔
62
            return ['*' => $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod()];
×
63
        }
64

65
        return $this->collectionNormalizer->getSupportedTypes($format);
72✔
66
    }
67

68
    public function hasCacheableSupportsMethod(): bool
69
    {
70
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
71
            trigger_deprecation(
×
72
                'api-platform/core',
×
73
                '3.1',
×
74
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
75
                __METHOD__
×
76
            );
×
77
        }
78

79
        return $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
×
80
    }
81

82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
86
    {
87
        if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && $object instanceof \ArrayObject && !\count($object)) {
64✔
88
            return $object;
×
89
        }
90

91
        $data = $this->collectionNormalizer->normalize($object, $format, $context);
64✔
92
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
64✔
93
            return $data;
20✔
94
        }
95

96
        if (!\is_array($data)) {
44✔
97
            throw new UnexpectedValueException('Expected data to be an array');
×
98
        }
99
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
44✔
100
        $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
44✔
101
        $resourceFilters = $operation->getFilters();
44✔
102
        if (!$resourceFilters) {
44✔
103
            return $data;
28✔
104
        }
105

106
        $requestParts = parse_url($context['request_uri'] ?? '');
16✔
107
        if (!\is_array($requestParts)) {
16✔
108
            return $data;
×
109
        }
110
        $currentFilters = [];
16✔
111
        foreach ($resourceFilters as $filterId) {
16✔
112
            if ($filter = $this->getFilter($filterId)) {
16✔
113
                $currentFilters[] = $filter;
12✔
114
            }
115
        }
116

117
        if ($options = $operation->getStateOptions()) {
16✔
118
            if ($options instanceof Options && $options->getEntityClass()) {
×
119
                $resourceClass = $options->getEntityClass();
×
120
            }
121

122
            if ($options instanceof ODMOptions && $options->getDocumentClass()) {
×
123
                $resourceClass = $options->getDocumentClass();
×
124
            }
125
        }
126

127
        if ($currentFilters) {
16✔
128
            $data['hydra:search'] = $this->getSearch($resourceClass, $requestParts, $currentFilters);
12✔
129
        }
130

131
        return $data;
16✔
132
    }
133

134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function setNormalizer(NormalizerInterface $normalizer): void
138
    {
139
        if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
116✔
140
            $this->collectionNormalizer->setNormalizer($normalizer);
116✔
141
        }
142
    }
143

144
    /**
145
     * Returns the content of the Hydra search property.
146
     *
147
     * @param LegacyFilterInterface[]|FilterInterface[] $filters
148
     */
149
    private function getSearch(string $resourceClass, array $parts, array $filters): array
150
    {
151
        $variables = [];
12✔
152
        $mapping = [];
12✔
153
        foreach ($filters as $filter) {
12✔
154
            foreach ($filter->getDescription($resourceClass) as $variable => $data) {
12✔
155
                $variables[] = $variable;
12✔
156
                $mapping[] = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $data['property'], 'required' => $data['required']];
12✔
157
            }
158
        }
159

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