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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

GitHub
Merge b2ec54b3c into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

197 of 197 new or added lines in 5 files covered. (100.0%)

10372 of 12438 relevant lines covered (83.39%)

11.97 hits per line

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

91.18
/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\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use Psr\Container\ContainerInterface;
21
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
22
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
23
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
24
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
25
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
26

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

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

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

52
    public function hasCacheableSupportsMethod(): bool
53
    {
54
        return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
14✔
55
    }
56

57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
61
    {
62
        if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && $object instanceof \ArrayObject && !\count($object)) {
15✔
63
            return $object;
×
64
        }
65

66
        $data = $this->collectionNormalizer->normalize($object, $format, $context);
15✔
67
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
15✔
68
            return $data;
5✔
69
        }
70

71
        if (!\is_array($data)) {
10✔
72
            throw new UnexpectedValueException('Expected data to be an array');
×
73
        }
74
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
10✔
75
        $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
10✔
76
        $resourceFilters = $operation->getFilters();
10✔
77
        if (!$resourceFilters) {
10✔
78
            return $data;
6✔
79
        }
80
        $requestParts = parse_url($context['request_uri'] ?? '');
4✔
81
        if (!\is_array($requestParts)) {
4✔
82
            return $data;
×
83
        }
84
        $currentFilters = [];
4✔
85
        foreach ($resourceFilters as $filterId) {
4✔
86
            if ($filter = $this->getFilter($filterId)) {
4✔
87
                $currentFilters[] = $filter;
3✔
88
            }
89
        }
90
        if ($currentFilters) {
4✔
91
            $data['hydra:search'] = $this->getSearch($resourceClass, $requestParts, $currentFilters);
3✔
92
        }
93

94
        return $data;
4✔
95
    }
96

97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function setNormalizer(NormalizerInterface $normalizer): void
101
    {
102
        if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
26✔
103
            $this->collectionNormalizer->setNormalizer($normalizer);
26✔
104
        }
105
    }
106

107
    /**
108
     * Returns the content of the Hydra search property.
109
     *
110
     * @param FilterInterface[] $filters
111
     */
112
    private function getSearch(string $resourceClass, array $parts, array $filters): array
113
    {
114
        $variables = [];
3✔
115
        $mapping = [];
3✔
116
        foreach ($filters as $filter) {
3✔
117
            foreach ($filter->getDescription($resourceClass) as $variable => $data) {
3✔
118
                $variables[] = $variable;
3✔
119
                $mapping[] = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $data['property'], 'required' => $data['required']];
3✔
120
            }
121
        }
122

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