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

api-platform / core / 14131840738

28 Mar 2025 03:01PM UTC coverage: 8.517% (+0.002%) from 8.515%
14131840738

push

github

web-flow
fix(doctrine): joinColumn might be an array (#7060)

fixes #7052

3 of 4 new or added lines in 1 file covered. (75.0%)

2092 existing lines in 148 files now uncovered.

13389 of 157200 relevant lines covered (8.52%)

22.91 hits per line

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

93.62
/src/Metadata/Util/ContentNegotiationTrait.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\Metadata\Util;
15

16
use Negotiation\Negotiator;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20

21
/**
22
 * @internal
23
 */
24
trait ContentNegotiationTrait
25
{
26
    private Negotiator $negotiator;
27

28
    /**
29
     * Gets the format associated with the mime type.
30
     *
31
     * Adapted from {@see Request::getFormat}.
32
     *
33
     * @param array<string, string|string[]> $formats
34
     */
35
    private function getMimeTypeFormat(string $mimeType, array $formats): ?string
36
    {
37
        $canonicalMimeType = null;
1,187✔
38
        $pos = strpos($mimeType, ';');
1,187✔
39
        if (false !== $pos) {
1,187✔
40
            $canonicalMimeType = trim(substr($mimeType, 0, $pos));
×
41
        }
42

43
        foreach ($formats as $format => $mimeTypes) {
1,187✔
44
            if (\in_array($mimeType, $mimeTypes, true)) {
1,185✔
45
                return $format;
1,185✔
46
            }
47
            if (null !== $canonicalMimeType && \in_array($canonicalMimeType, $mimeTypes, true)) {
531✔
48
                return $format;
×
49
            }
50
        }
51

52
        return null;
7✔
53
    }
54

55
    /**
56
     * Flattened the list of MIME types.
57
     *
58
     * @param array<string, string|string[]> $formats
59
     *
60
     * @return array<string, string>
61
     */
62
    private function flattenMimeTypes(array $formats): array
63
    {
64
        $flattenedMimeTypes = [];
545✔
65
        foreach ($formats as $format => $mimeTypes) {
545✔
66
            foreach ($mimeTypes as $mimeType) {
545✔
67
                $flattenedMimeTypes[$mimeType] = $format;
545✔
68
            }
69
        }
70

71
        return $flattenedMimeTypes;
545✔
72
    }
73

74
    /**
75
     * @param array<string, string|string[]> $formats
76
     */
77
    private function getRequestFormat(Request $request, array $formats, bool $throw = true): string
78
    {
79
        $mimeTypes = [];
2,000✔
80
        $flattenedMimeTypes = [];
2,000✔
81

82
        if ($routeFormat = $request->attributes->get('_format') ?: null) {
2,000✔
83
            if (isset($formats[$routeFormat])) {
51✔
84
                $mimeTypes = Request::getMimeTypes($routeFormat);
47✔
85
                $flattenedMimeTypes = $this->flattenMimeTypes([$routeFormat => $mimeTypes]);
47✔
UNCOV
86
            } elseif ($throw) {
4✔
UNCOV
87
                throw new NotFoundHttpException(\sprintf('Format "%s" is not supported', $routeFormat));
4✔
88
            }
89
        }
90

91
        if (!$mimeTypes) {
2,000✔
92
            $flattenedMimeTypes = $this->flattenMimeTypes($formats);
1,953✔
93
            $mimeTypes = array_keys($flattenedMimeTypes);
1,953✔
94
        }
95

96
        // First, try to guess the format from the Accept header
97
        /** @var string|null $accept */
98
        $accept = $request->headers->get('Accept');
2,000✔
99
        if (null !== $accept) {
2,000✔
100
            if ($mediaType = $this->negotiator->getBest($accept, $mimeTypes)) {
948✔
101
                return $this->getMimeTypeFormat($mediaType->getType(), $formats);
920✔
102
            }
103

104
            if ($throw) {
56✔
UNCOV
105
                throw $this->getNotAcceptableHttpException($accept, $flattenedMimeTypes);
10✔
106
            }
107
        }
108

109
        // Then, use the Symfony request format if available and applicable
110
        $requestFormat = $request->getRequestFormat('') ?: null;
1,112✔
111
        if (null !== $requestFormat) {
1,112✔
UNCOV
112
            $mimeType = $request->getMimeType($requestFormat);
152✔
113

UNCOV
114
            if (isset($flattenedMimeTypes[$mimeType])) {
152✔
UNCOV
115
                return $requestFormat;
136✔
116
            }
117

UNCOV
118
            if ($throw) {
16✔
119
                throw $this->getNotAcceptableHttpException($mimeType, $flattenedMimeTypes);
×
120
            }
121
        }
122

123
        // Finally, if no Accept header nor Symfony request format is set, return the default format
124
        return array_key_first($formats);
1,051✔
125
    }
126

127
    /**
128
     * Retrieves an instance of NotAcceptableHttpException.
129
     */
130
    private function getNotAcceptableHttpException(string $accept, array $mimeTypes): NotAcceptableHttpException
131
    {
UNCOV
132
        return new NotAcceptableHttpException(\sprintf(
10✔
UNCOV
133
            'Requested format "%s" is not supported. Supported MIME types are "%s".',
10✔
UNCOV
134
            $accept,
10✔
UNCOV
135
            implode('", "', array_keys($mimeTypes))
10✔
UNCOV
136
        ));
10✔
137
    }
138

139
    /**
140
     * Adds the supported formats to the request.
141
     *
142
     * This is necessary for {@see Request::getMimeType} and {@see Request::getMimeTypes} to work.
143
     *
144
     * @param array<string, string|string[]> $formats
145
     */
146
    private function addRequestFormats(Request $request, array $formats): void
147
    {
148
        foreach ($formats as $format => $mimeTypes) {
33✔
149
            $request->setFormat($format, (array) $mimeTypes);
33✔
150
        }
151
    }
152
}
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