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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 hits per line

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

91.67
/src/Metadata/Resource/Factory/InputOutputResourceMetadataCollectionFactory.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\ApiResource;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Operations;
19
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
20

21
/**
22
 * Transforms the given input/output metadata to a normalized one.
23
 *
24
 * @author Antoine Bluchet <soyuka@gmail.com>
25
 */
26
final class InputOutputResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
27
{
28
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $decorated)
29
    {
30
    }
465✔
31

32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function create(string $resourceClass): ResourceMetadataCollection
36
    {
37
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
84✔
38

39
        foreach ($resourceMetadataCollection as $key => $resourceMetadata) {
84✔
40
            $resourceMetadata = $resourceMetadata->withInput($this->transformInputOutput($resourceMetadata->getInput()));
81✔
41
            $resourceMetadata = $resourceMetadata->withOutput($this->transformInputOutput($resourceMetadata->getOutput()));
81✔
42

43
            if ($resourceMetadata->getOperations()) {
81✔
44
                $resourceMetadata = $resourceMetadata->withOperations($this->getTransformedOperations($resourceMetadata->getOperations(), $resourceMetadata));
81✔
45
            }
46

47
            if ($resourceMetadata->getGraphQlOperations()) {
81✔
48
                $resourceMetadata = $resourceMetadata->withGraphQlOperations($this->getTransformedOperations($resourceMetadata->getGraphQlOperations(), $resourceMetadata));
63✔
49
            }
50

51
            $resourceMetadataCollection[$key] = $resourceMetadata;
81✔
52
        }
53

54
        return $resourceMetadataCollection;
84✔
55
    }
56

57
    private function getTransformedOperations(Operations|array $operations, ApiResource $resourceMetadata): Operations|array
58
    {
59
        foreach ($operations as $key => $operation) {
81✔
60
            $operation = $operation->withInput(null !== $operation->getInput() ? $this->transformInputOutput($operation->getInput()) : $resourceMetadata->getInput());
81✔
61
            $operation = $operation->withOutput(null !== $operation->getOutput() ? $this->transformInputOutput($operation->getOutput()) : $resourceMetadata->getOutput());
81✔
62

63
            if (
64
                $operation->getInput()
81✔
65
                && \array_key_exists('class', $operation->getInput())
81✔
66
                && null === $operation->getInput()['class']
81✔
67
            ) {
68
                $operation = $operation->withDeserialize(null === $operation->canDeserialize() ? false : $operation->canDeserialize());
×
69
                $operation = $operation->withValidate(null === $operation->canValidate() ? false : $operation->canValidate());
×
70
            }
71

72
            if (
73
                $operation instanceof HttpOperation
81✔
74
                && $operation->getOutput()
81✔
75
                && \array_key_exists('class', $operation->getOutput())
81✔
76
                && null === $operation->getOutput()['class']
81✔
77
                && null === $operation->getStatus()
81✔
78
            ) {
79
                $operation = $operation->withStatus(204);
×
80
            }
81

82
            $operations instanceof Operations ? $operations->add($key, $operation) : $operations[$key] = $operation;
81✔
83
        }
84

85
        return $operations;
81✔
86
    }
87

88
    private function transformInputOutput(mixed $attribute): ?array
89
    {
90
        if (false === $attribute) {
81✔
91
            return ['class' => null];
21✔
92
        }
93

94
        if (!$attribute) {
81✔
95
            return null;
81✔
96
        }
97

98
        if (\is_string($attribute)) {
12✔
99
            $attribute = ['class' => $attribute];
12✔
100
        }
101

102
        if (!isset($attribute['name']) && isset($attribute['class'])) {
12✔
103
            $attribute['name'] = (new \ReflectionClass($attribute['class']))->getShortName();
12✔
104
        }
105

106
        return $attribute;
12✔
107
    }
108
}
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