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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

97.96
/src/JsonApi/State/JsonApiProvider.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\JsonApi\State;
15

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\State\ProviderInterface;
18

19
final class JsonApiProvider implements ProviderInterface
20
{
21
    public function __construct(private readonly ProviderInterface $decorated, private readonly string $orderParameterName = 'order')
22
    {
UNCOV
23
    }
799✔
24

25
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
26
    {
UNCOV
27
        $request = $context['request'] ?? null;
799✔
28

UNCOV
29
        if (!$request || 'jsonapi' !== $request->getRequestFormat()) {
799✔
UNCOV
30
            return $this->decorated->provide($operation, $uriVariables, $context);
726✔
31
        }
32

UNCOV
33
        $filters = $request->attributes->get('_api_filters', []);
73✔
UNCOV
34
        $queryParameters = $request->query->all();
73✔
UNCOV
35
        $orderParameter = $queryParameters['sort'] ?? null;
73✔
36

37
        if (
UNCOV
38
            null !== $orderParameter
73✔
UNCOV
39
            && !\is_array($orderParameter)
73✔
40
        ) {
41
            $orderParametersArray = explode(',', (string) $orderParameter);
3✔
42
            $transformedOrderParametersArray = [];
3✔
43

44
            foreach ($orderParametersArray as $orderParameter) {
3✔
45
                $sorting = 'asc';
3✔
46

47
                if ('-' === ($orderParameter[0] ?? null)) {
3✔
48
                    $sorting = 'desc';
2✔
49
                    $orderParameter = substr($orderParameter, 1);
2✔
50
                }
51

52
                $transformedOrderParametersArray[$orderParameter] = $sorting;
3✔
53
            }
54

55
            $filters[$this->orderParameterName] = $transformedOrderParametersArray;
3✔
56
        }
57

UNCOV
58
        $filterParameter = $queryParameters['filter'] ?? null;
73✔
59
        if (
UNCOV
60
            $filterParameter
73✔
UNCOV
61
            && \is_array($filterParameter)
73✔
62
        ) {
63
            $filters = array_merge($filterParameter, $filters);
4✔
64
        }
65

UNCOV
66
        $pageParameter = $queryParameters['page'] ?? null;
73✔
67
        if (
UNCOV
68
            \is_array($pageParameter)
73✔
69
        ) {
70
            $filters = array_merge($pageParameter, $filters);
5✔
71
        }
72

UNCOV
73
        [$included, $properties] = $this->transformFieldsetsParameters($queryParameters, $operation->getShortName() ?? '');
73✔
74

UNCOV
75
        if ($properties) {
73✔
76
            $request->attributes->set('_api_filter_property', $properties);
3✔
77
        }
78

UNCOV
79
        if ($included) {
73✔
80
            $request->attributes->set('_api_included', $included);
16✔
81
        }
82

UNCOV
83
        if ($filters) {
73✔
84
            $request->attributes->set('_api_filters', $filters);
11✔
85
        }
86

UNCOV
87
        return $this->decorated->provide($operation, $uriVariables, $context);
73✔
88
    }
89

90
    private function transformFieldsetsParameters(array $queryParameters, string $resourceShortName): array
91
    {
UNCOV
92
        $includeParameter = $queryParameters['include'] ?? null;
73✔
UNCOV
93
        $fieldsParameter = $queryParameters['fields'] ?? null;
73✔
94

UNCOV
95
        $includeParameter = \is_string($includeParameter) ? explode(',', $includeParameter) : [];
73✔
UNCOV
96
        if (!$fieldsParameter) {
73✔
UNCOV
97
            return [$includeParameter, []];
70✔
98
        }
99

100
        $properties = [];
3✔
101
        $included = [];
3✔
102
        foreach ($fieldsParameter as $resourceType => $fields) {
3✔
103
            $fields = explode(',', (string) $fields);
3✔
104

105
            if ($resourceShortName === $resourceType) {
3✔
106
                $properties = array_merge($properties, $fields);
2✔
107
            } elseif (\in_array($resourceType, $includeParameter, true)) {
2✔
108
                $properties[$resourceType] = $fields;
2✔
109
                $included[] = $resourceType;
2✔
110
            } else {
111
                $properties[$resourceType] = $fields;
×
112
            }
113
        }
114

115
        return [$included, $properties];
3✔
116
    }
117
}
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