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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 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\HttpOperation;
21
use ApiPlatform\Metadata\Operations;
22
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
23

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

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

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

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

68
        return $resourceMetadataCollection;
102✔
69
    }
70

71
    /**
72
     * @param Operations<HttpOperation> $operations
73
     */
74
    private function normalize(array $resourceInputFormats, array $resourceOutputFormats, Operations $operations): Operations
75
    {
76
        $newOperations = [];
100✔
77
        $patchFormats = null;
100✔
78
        foreach ($operations as $operationName => $operation) {
100✔
79
            if ($operation->getFormats()) {
100✔
80
                $operation = $operation->withFormats($this->normalizeFormats($operation->getFormats()));
2✔
81
            }
82

83
            if (($isPatch = 'PATCH' === $operation->getMethod()) && !$operation->getFormats() && !$operation->getInputFormats()) {
100✔
84
                $operation = $operation->withInputFormats($this->patchFormats);
30✔
85
            }
86

87
            $operation = $operation->withInputFormats($operation->getInputFormats() ? $this->normalizeFormats($operation->getInputFormats()) : $operation->getFormats() ?? $resourceInputFormats);
100✔
88
            $operation = $operation->withOutputFormats($operation->getOutputFormats() ? $this->normalizeFormats($operation->getOutputFormats()) : $operation->getFormats() ?? $resourceOutputFormats);
100✔
89

90
            if ($isPatch) {
100✔
91
                $patchFormats = $operation->getInputFormats();
30✔
92
            }
93

94
            $newOperations[$operationName] = $operation;
100✔
95
        }
96

97
        if (!$patchFormats) {
100✔
98
            return new Operations($newOperations);
88✔
99
        }
100

101
        // Prepare an Accept-Patch header
102
        foreach ($newOperations as $operationName => $operation) {
30✔
103
            if ($operation instanceof CollectionOperationInterface) {
30✔
104
                continue;
30✔
105
            }
106

107
            $patchMimeTypes = [];
30✔
108

109
            foreach ($patchFormats as $mimeTypes) {
30✔
110
                foreach ($mimeTypes as $mimeType) {
30✔
111
                    $patchMimeTypes[] = $mimeType;
30✔
112
                }
113
            }
114

115
            $newOperations[$operationName] = $operation->withAcceptPatch(implode(', ', $patchMimeTypes));
30✔
116
        }
117

118
        return new Operations($newOperations);
30✔
119
    }
120

121
    /**
122
     * @throws InvalidArgumentException
123
     */
124
    private function normalizeFormats(array|string $currentFormats): array
125
    {
126
        $currentFormats = (array) $currentFormats;
38✔
127

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

142
            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));
×
143
        }
144

145
        return $normalizedFormats;
38✔
146
    }
147
}
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