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

api-platform / core / 10739011304

06 Sep 2024 01:11PM UTC coverage: 7.159% (-0.5%) from 7.645%
10739011304

push

github

web-flow
 feat(laravel): eloquent filters (search, date, equals, or) (#6593)

* feat(laravel): Eloquent filters search date or

* feat(laravel): eloquent filters order range equals afterdate

* fix(laravel): order afterDate filters

* temp

* test(laravel): filter with eloquent

---------

Co-authored-by: Nathan <nathan@les-tilleuls.coop>

0 of 144 new or added lines in 16 files covered. (0.0%)

4792 existing lines in 155 files now uncovered.

11736 of 163930 relevant lines covered (7.16%)

22.8 hits per line

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

96.77
/src/State/Provider/ContentNegotiationProvider.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\State\Provider;
15

16
use ApiPlatform\Metadata\Error as ErrorOperation;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
20
use ApiPlatform\State\ProviderInterface;
21
use Negotiation\Negotiator;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
24

25
final class ContentNegotiationProvider implements ProviderInterface
26
{
27
    use ContentNegotiationTrait;
28

29
    /**
30
     * @param array<string, string[]> $formats
31
     * @param array<string, string[]> $errorFormats
32
     */
33
    public function __construct(private readonly ?ProviderInterface $decorated = null, ?Negotiator $negotiator = null, private readonly array $formats = [], private readonly array $errorFormats = [])
34
    {
UNCOV
35
        $this->negotiator = $negotiator ?? new Negotiator();
1,850✔
36
    }
37

38
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
39
    {
UNCOV
40
        if (!($request = $context['request'] ?? null) || !$operation instanceof HttpOperation) {
1,850✔
41
            return $this->decorated?->provide($operation, $uriVariables, $context);
×
42
        }
43

UNCOV
44
        $isErrorOperation = $operation instanceof ErrorOperation;
1,850✔
45

UNCOV
46
        $formats = $operation->getOutputFormats() ?? ($isErrorOperation ? $this->errorFormats : $this->formats);
1,850✔
UNCOV
47
        $this->addRequestFormats($request, $formats);
1,850✔
UNCOV
48
        $request->attributes->set('input_format', $this->getInputFormat($operation, $request));
1,850✔
49

UNCOV
50
        if (!$isErrorOperation) {
1,850✔
UNCOV
51
            $request->setRequestFormat($this->getRequestFormat($request, $formats));
1,827✔
52
        } else {
UNCOV
53
            $request->setRequestFormat($this->getRequestFormat($request, $formats, false));
235✔
54
        }
55

UNCOV
56
        return $this->decorated?->provide($operation, $uriVariables, $context);
1,850✔
57
    }
58

59
    /**
60
     * Adds the supported formats to the request.
61
     *
62
     * This is necessary for {@see Request::getMimeType} and {@see Request::getMimeTypes} to work.
63
     * Note that this replaces default mime types configured at {@see Request::initializeFormats}
64
     *
65
     * @param array<string, string|string[]> $formats
66
     */
67
    private function addRequestFormats(Request $request, array $formats): void
68
    {
UNCOV
69
        foreach ($formats as $format => $mimeTypes) {
1,850✔
UNCOV
70
            $request->setFormat($format, (array) $mimeTypes);
1,850✔
71
        }
72
    }
73

74
    /**
75
     * Flattened the list of MIME types.
76
     *
77
     * @param array<string, string|string[]> $formats
78
     *
79
     * @return array<string, string>
80
     */
81
    private function flattenMimeTypes(array $formats): array
82
    {
UNCOV
83
        $flattenedMimeTypes = [];
1,850✔
UNCOV
84
        foreach ($formats as $format => $mimeTypes) {
1,850✔
UNCOV
85
            foreach ($mimeTypes as $mimeType) {
1,850✔
UNCOV
86
                $flattenedMimeTypes[$mimeType] = $format;
1,850✔
87
            }
88
        }
89

UNCOV
90
        return $flattenedMimeTypes;
1,850✔
91
    }
92

93
    /**
94
     * Extracts the format from the Content-Type header and check that it is supported.
95
     *
96
     * @throws UnsupportedMediaTypeHttpException
97
     */
98
    private function getInputFormat(HttpOperation $operation, Request $request): ?string
99
    {
UNCOV
100
        $contentType = $request->headers->get('CONTENT_TYPE');
1,850✔
UNCOV
101
        if (null === $contentType || '' === $contentType) {
1,850✔
UNCOV
102
            return null;
981✔
103
        }
104

105
        /** @var string $contentType */
UNCOV
106
        $formats = $operation->getInputFormats() ?? [];
880✔
UNCOV
107
        if ($format = $this->getMimeTypeFormat($contentType, $formats)) {
880✔
UNCOV
108
            return $format;
852✔
109
        }
110

UNCOV
111
        $supportedMimeTypes = [];
32✔
UNCOV
112
        foreach ($formats as $mimeTypes) {
32✔
UNCOV
113
            foreach ($mimeTypes as $mimeType) {
29✔
UNCOV
114
                $supportedMimeTypes[] = $mimeType;
29✔
115
            }
116
        }
117

UNCOV
118
        if (!$request->isMethodSafe() && 'DELETE' !== $request->getMethod()) {
32✔
UNCOV
119
            throw new UnsupportedMediaTypeHttpException(\sprintf('The content-type "%s" is not supported. Supported MIME types are "%s".', $contentType, implode('", "', $supportedMimeTypes)));
3✔
120
        }
121

UNCOV
122
        return null;
32✔
123
    }
124
}
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