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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 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
    {
23
    }
1,004✔
24

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
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