• 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

95.74
/src/Metadata/Resource/Factory/FormatsResourceMetadataCollectionFactory.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\Resource\Factory;
15

16
use ApiPlatform\Metadata\CollectionOperationInterface;
17
use ApiPlatform\Metadata\ErrorResource;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
20
use ApiPlatform\Metadata\Operations;
21
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
22

23
/**
24
 * Normalizes enabled formats.
25
 *
26
 * Formats hierarchy:
27
 * * resource formats
28
 *   * resource input/output formats
29
 *     * operation formats
30
 *       * operation input/output formats
31
 *
32
 * @author Kévin Dunglas <dunglas@gmail.com>
33
 */
34
final class FormatsResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
35
{
36
    public function __construct(
37
        private readonly ResourceMetadataCollectionFactoryInterface $decorated,
38
        private readonly array $formats,
39
        private readonly array $patchFormats,
40
        private readonly ?array $errorFormats = null,
41
    ) {
UNCOV
42
    }
978✔
43

44
    /**
45
     * Adds the formats attributes.
46
     *
47
     * @see UriTemplateResourceMetadataCollectionFactory
48
     *
49
     * @throws ResourceClassNotFoundException
50
     */
51
    public function create(string $resourceClass): ResourceMetadataCollection
52
    {
UNCOV
53
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
69✔
54

UNCOV
55
        foreach ($resourceMetadataCollection as $index => $resourceMetadata) {
69✔
UNCOV
56
            $rawResourceFormats = $resourceMetadata->getFormats();
65✔
UNCOV
57
            $resourceFormats = null === $rawResourceFormats ? $this->formats : $this->normalizeFormats($rawResourceFormats);
65✔
UNCOV
58
            $resourceInputFormats = $resourceMetadata->getInputFormats() ? $this->normalizeFormats($resourceMetadata->getInputFormats()) : $resourceFormats;
65✔
UNCOV
59
            $resourceOutputFormats = $resourceMetadata->getOutputFormats() ? $this->normalizeFormats($resourceMetadata->getOutputFormats()) : $resourceFormats;
65✔
UNCOV
60
            if ($resourceMetadata instanceof ErrorResource) {
65✔
UNCOV
61
                $resourceOutputFormats = $resourceMetadata->getOutputFormats() ? $this->normalizeFormats($resourceMetadata->getOutputFormats()) : ($this->errorFormats ?? []);
5✔
62
            }
63

UNCOV
64
            $resourceMetadataCollection[$index] = $resourceMetadataCollection[$index]->withOperations($this->normalize($resourceInputFormats, $resourceOutputFormats, $resourceMetadata->getOperations()));
65✔
65
        }
66

UNCOV
67
        return $resourceMetadataCollection;
69✔
68
    }
69

70
    private function normalize(array $resourceInputFormats, array $resourceOutputFormats, Operations $operations): Operations
71
    {
UNCOV
72
        $newOperations = [];
65✔
UNCOV
73
        $patchFormats = null;
65✔
UNCOV
74
        foreach ($operations as $operationName => $operation) {
65✔
UNCOV
75
            if ($operation->getFormats()) {
65✔
UNCOV
76
                $operation = $operation->withFormats($this->normalizeFormats($operation->getFormats()));
2✔
77
            }
78

UNCOV
79
            if (($isPatch = 'PATCH' === $operation->getMethod()) && !$operation->getFormats() && !$operation->getInputFormats()) {
65✔
UNCOV
80
                $operation = $operation->withInputFormats($this->patchFormats);
27✔
81
            }
82

UNCOV
83
            $operation = $operation->withInputFormats($operation->getInputFormats() ? $this->normalizeFormats($operation->getInputFormats()) : $operation->getFormats() ?? $resourceInputFormats);
65✔
UNCOV
84
            $operation = $operation->withOutputFormats($operation->getOutputFormats() ? $this->normalizeFormats($operation->getOutputFormats()) : $operation->getFormats() ?? $resourceOutputFormats);
65✔
85

UNCOV
86
            if ($isPatch) {
65✔
UNCOV
87
                $patchFormats = $operation->getInputFormats();
27✔
88
            }
89

UNCOV
90
            $newOperations[$operationName] = $operation;
65✔
91
        }
92

UNCOV
93
        if (!$patchFormats) {
65✔
UNCOV
94
            return new Operations($newOperations);
52✔
95
        }
96

97
        // Prepare an Accept-Patch header
UNCOV
98
        foreach ($newOperations as $operationName => $operation) {
27✔
UNCOV
99
            if ($operation instanceof CollectionOperationInterface) {
27✔
UNCOV
100
                continue;
27✔
101
            }
102

UNCOV
103
            $patchMimeTypes = [];
27✔
104

UNCOV
105
            foreach ($patchFormats as $mimeTypes) {
27✔
UNCOV
106
                foreach ($mimeTypes as $mimeType) {
27✔
UNCOV
107
                    $patchMimeTypes[] = $mimeType;
27✔
108
                }
109
            }
110

UNCOV
111
            $newOperations[$operationName] = $operation->withAcceptPatch(implode(', ', $patchMimeTypes));
27✔
112
        }
113

UNCOV
114
        return new Operations($newOperations);
27✔
115
    }
116

117
    /**
118
     * @throws InvalidArgumentException
119
     */
120
    private function normalizeFormats(array|string $currentFormats): array
121
    {
UNCOV
122
        $currentFormats = (array) $currentFormats;
33✔
123

UNCOV
124
        $normalizedFormats = [];
33✔
UNCOV
125
        foreach ($currentFormats as $format => $value) {
33✔
UNCOV
126
            if (!is_numeric($format)) {
33✔
UNCOV
127
                $normalizedFormats[$format] = (array) $value;
32✔
UNCOV
128
                continue;
32✔
129
            }
UNCOV
130
            if (!\is_string($value)) {
2✔
131
                throw new InvalidArgumentException(\sprintf("The 'formats' attributes value must be a string when trying to include an already configured format, %s given.", \gettype($value)));
×
132
            }
UNCOV
133
            if (\array_key_exists($value, $this->formats)) {
2✔
UNCOV
134
                $normalizedFormats[$value] = $this->formats[$value];
2✔
UNCOV
135
                continue;
2✔
136
            }
137

138
            throw new InvalidArgumentException(\sprintf("You either need to add the format '%s' to your project configuration or declare a mime type for it in your annotation.", $value));
×
139
        }
140

UNCOV
141
        return $normalizedFormats;
33✔
142
    }
143
}
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