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

api-platform / core / 10729306835

05 Sep 2024 10:46PM UTC coverage: 7.655% (-0.01%) from 7.665%
10729306835

push

github

web-flow
Merge pull request #6586 from soyuka/merge-342

Merge 3.4

0 of 54 new or added lines in 12 files covered. (0.0%)

8760 existing lines in 277 files now uncovered.

12505 of 163357 relevant lines covered (7.66%)

22.84 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(private readonly ResourceMetadataCollectionFactoryInterface $decorated, private readonly array $formats, private readonly array $patchFormats, private readonly ?array $errorFormats = null)
37
    {
UNCOV
38
    }
2,248✔
39

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

UNCOV
51
        foreach ($resourceMetadataCollection as $index => $resourceMetadata) {
42✔
UNCOV
52
            $rawResourceFormats = $resourceMetadata->getFormats();
34✔
UNCOV
53
            $resourceFormats = null === $rawResourceFormats ? $this->formats : $this->normalizeFormats($rawResourceFormats);
34✔
UNCOV
54
            $resourceInputFormats = $resourceMetadata->getInputFormats() ? $this->normalizeFormats($resourceMetadata->getInputFormats()) : $resourceFormats;
34✔
UNCOV
55
            $resourceOutputFormats = $resourceMetadata->getOutputFormats() ? $this->normalizeFormats($resourceMetadata->getOutputFormats()) : $resourceFormats;
34✔
UNCOV
56
            if ($resourceMetadata instanceof ErrorResource) {
34✔
UNCOV
57
                $resourceOutputFormats = $resourceMetadata->getOutputFormats() ? $this->normalizeFormats($resourceMetadata->getOutputFormats()) : ($this->errorFormats ?? []);
4✔
58
            }
59

UNCOV
60
            $resourceMetadataCollection[$index] = $resourceMetadataCollection[$index]->withOperations($this->normalize($resourceInputFormats, $resourceOutputFormats, $resourceMetadata->getOperations()));
34✔
61
        }
62

UNCOV
63
        return $resourceMetadataCollection;
42✔
64
    }
65

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

UNCOV
75
            if (($isPatch = 'PATCH' === $operation->getMethod()) && !$operation->getFormats() && !$operation->getInputFormats()) {
34✔
UNCOV
76
                $operation = $operation->withInputFormats($this->patchFormats);
23✔
77
            }
78

UNCOV
79
            $operation = $operation->withInputFormats($operation->getInputFormats() ? $this->normalizeFormats($operation->getInputFormats()) : $operation->getFormats() ?? $resourceInputFormats);
34✔
UNCOV
80
            $operation = $operation->withOutputFormats($operation->getOutputFormats() ? $this->normalizeFormats($operation->getOutputFormats()) : $operation->getFormats() ?? $resourceOutputFormats);
34✔
81

UNCOV
82
            if ($isPatch) {
34✔
UNCOV
83
                $patchFormats = $operation->getInputFormats();
24✔
84
            }
85

UNCOV
86
            $newOperations[$operationName] = $operation;
34✔
87
        }
88

UNCOV
89
        if (!$patchFormats) {
34✔
UNCOV
90
            return new Operations($newOperations);
22✔
91
        }
92

93
        // Prepare an Accept-Patch header
UNCOV
94
        foreach ($newOperations as $operationName => $operation) {
24✔
UNCOV
95
            if ($operation instanceof CollectionOperationInterface) {
24✔
UNCOV
96
                continue;
23✔
97
            }
98

UNCOV
99
            $patchMimeTypes = [];
24✔
100

UNCOV
101
            foreach ($patchFormats as $mimeTypes) {
24✔
UNCOV
102
                foreach ($mimeTypes as $mimeType) {
24✔
UNCOV
103
                    $patchMimeTypes[] = $mimeType;
24✔
104
                }
105
            }
106

UNCOV
107
            $newOperations[$operationName] = $operation->withAcceptPatch(implode(', ', $patchMimeTypes));
24✔
108
        }
109

UNCOV
110
        return new Operations($newOperations);
24✔
111
    }
112

113
    /**
114
     * @throws InvalidArgumentException
115
     */
116
    private function normalizeFormats(array|string $currentFormats): array
117
    {
UNCOV
118
        $currentFormats = (array) $currentFormats;
25✔
119

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

134
            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));
×
135
        }
136

UNCOV
137
        return $normalizedFormats;
25✔
138
    }
139
}
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