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

api-platform / core / 13944674159

19 Mar 2025 10:47AM UTC coverage: 7.275% (-0.01%) from 7.289%
13944674159

push

github

soyuka
Merge 3.4

6 of 23 new or added lines in 4 files covered. (26.09%)

2380 existing lines in 154 files now uncovered.

12415 of 170643 relevant lines covered (7.28%)

12.04 hits per line

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

100.0
/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
    }
1,180✔
31

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

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

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

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

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

54
        return $resourceMetadataCollection;
113✔
55
    }
56

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

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

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

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

85
        return $operations;
107✔
86
    }
87

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

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

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

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

106
        return $attribute;
13✔
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