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

api-platform / core / 4498909317

pending completion
4498909317

push

github

soyuka
Merge 3.1

364 of 364 new or added lines in 61 files covered. (100.0%)

10781 of 17989 relevant lines covered (59.93%)

11.36 hits per line

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

95.56
/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\Exception\InvalidArgumentException;
18
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
19
use ApiPlatform\Metadata\HttpOperation;
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(private readonly ResourceMetadataCollectionFactoryInterface $decorated, private readonly array $formats, private readonly array $patchFormats)
37
    {
38
    }
34✔
39

40
    /**
41
     * Adds the formats attributes.
42
     *
43
     * @see OperationResourceMetadataFactory
44
     *
45
     * @throws ResourceClassNotFoundException
46
     */
47
    public function create(string $resourceClass): ResourceMetadataCollection
48
    {
49
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
3✔
50

51
        foreach ($resourceMetadataCollection as $index => $resourceMetadata) {
3✔
52
            $rawResourceFormats = $resourceMetadata->getFormats();
3✔
53
            $resourceFormats = null === $rawResourceFormats ? $this->formats : $this->normalizeFormats($rawResourceFormats);
3✔
54
            $resourceInputFormats = $resourceMetadata->getInputFormats() ? $this->normalizeFormats($resourceMetadata->getInputFormats()) : $resourceFormats;
3✔
55
            $resourceOutputFormats = $resourceMetadata->getOutputFormats() ? $this->normalizeFormats($resourceMetadata->getOutputFormats()) : $resourceFormats;
3✔
56

57
            $resourceMetadataCollection[$index] = $resourceMetadataCollection[$index]->withOperations($this->normalize($resourceInputFormats, $resourceOutputFormats, $resourceMetadata->getOperations()));
3✔
58
        }
59

60
        return $resourceMetadataCollection;
3✔
61
    }
62

63
    private function normalize(array $resourceInputFormats, array $resourceOutputFormats, Operations $operations): Operations
64
    {
65
        $newOperations = [];
3✔
66
        $patchFormats = null;
3✔
67
        foreach ($operations as $operationName => $operation) {
3✔
68
            if ($operation->getFormats()) {
3✔
69
                $operation = $operation->withFormats($this->normalizeFormats($operation->getFormats()));
1✔
70
            }
71

72
            if (($isPatch = HttpOperation::METHOD_PATCH === $operation->getMethod()) && !$operation->getFormats() && !$operation->getInputFormats()) {
3✔
73
                $operation = $operation->withInputFormats($this->patchFormats);
2✔
74
            }
75

76
            $operation = $operation->withInputFormats($operation->getInputFormats() ? $this->normalizeFormats($operation->getInputFormats()) : $operation->getFormats() ?? $resourceInputFormats);
3✔
77
            $operation = $operation->withOutputFormats($operation->getOutputFormats() ? $this->normalizeFormats($operation->getOutputFormats()) : $operation->getFormats() ?? $resourceOutputFormats);
3✔
78

79
            if ($isPatch) {
3✔
80
                $patchFormats = $operation->getInputFormats();
2✔
81
            }
82

83
            $newOperations[$operationName] = $operation;
3✔
84
        }
85

86
        if (!$patchFormats) {
3✔
87
            return new Operations($newOperations);
3✔
88
        }
89

90
        // Prepare an Accept-Patch header
91
        foreach ($newOperations as $operationName => $operation) {
2✔
92
            if ($operation instanceof CollectionOperationInterface) {
2✔
93
                continue;
2✔
94
            }
95

96
            $patchMimeTypes = [];
2✔
97

98
            foreach ($patchFormats as $mimeTypes) {
2✔
99
                foreach ($mimeTypes as $mimeType) {
2✔
100
                    $patchMimeTypes[] = $mimeType;
2✔
101
                }
102
            }
103

104
            $newOperations[$operationName] = $operation->withAcceptPatch(implode(', ', $patchMimeTypes));
2✔
105
        }
106

107
        return new Operations($newOperations);
2✔
108
    }
109

110
    /**
111
     * @throws InvalidArgumentException
112
     */
113
    private function normalizeFormats(array|string $currentFormats): array
114
    {
115
        $currentFormats = (array) $currentFormats;
2✔
116

117
        $normalizedFormats = [];
2✔
118
        foreach ($currentFormats as $format => $value) {
2✔
119
            if (!is_numeric($format)) {
2✔
120
                $normalizedFormats[$format] = (array) $value;
2✔
121
                continue;
2✔
122
            }
123
            if (!\is_string($value)) {
1✔
124
                throw new InvalidArgumentException(sprintf("The 'formats' attributes value must be a string when trying to include an already configured format, %s given.", \gettype($value)));
×
125
            }
126
            if (\array_key_exists($value, $this->formats)) {
1✔
127
                $normalizedFormats[$value] = $this->formats[$value];
1✔
128
                continue;
1✔
129
            }
130

131
            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));
×
132
        }
133

134
        return $normalizedFormats;
2✔
135
    }
136
}
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

© 2026 Coveralls, Inc