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

api-platform / core / 9710673650

25 Jun 2024 02:01PM UTC coverage: 61.693% (-0.9%) from 62.637%
9710673650

push

github

web-flow
feat(laravel): laravel component (#5882)

* feat(laravel): laravel component

* try to skip laravel

* feat(jsonapi): component

* feat(laravel): json api support (needs review)

* work on relations

* relations (needs toMany) + skolem + IRI to resource

* links handler

* ulid

* validation

* slug post

* remove deprecations

* move classes

* fix tests

* fix tests metadata

* phpstan

* missing class

* fix laravel tests

* fix stan

33 of 77 new or added lines in 20 files covered. (42.86%)

140 existing lines in 15 files now uncovered.

10787 of 17485 relevant lines covered (61.69%)

59.64 hits per line

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

48.98
/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
    }
304✔
24

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
115
        return [$included, $properties];
×
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