Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

Factory/InputOutputResourceMetadataFactory.php (1 issue)

1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\Metadata\Resource\Factory;
15
16
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
17
18
/**
19
 * Transforms the given input/output metadata to a normalized one.
20
 *
21
 * @author Antoine Bluchet <[email protected]>
22
 */
23
final class InputOutputResourceMetadataFactory implements ResourceMetadataFactoryInterface
24
{
25
    private $decorated;
26
27
    public function __construct(ResourceMetadataFactoryInterface $decorated)
28
    {
29
        $this->decorated = $decorated;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function create(string $resourceClass): ResourceMetadata
36
    {
37
        $resourceMetadata = $this->decorated->create($resourceClass);
38
39
        $attributes = $resourceMetadata->getAttributes() ?: [];
40
        $attributes['input'] = isset($attributes['input']) ? $this->transformInputOutput($attributes['input']) : null;
41
        $attributes['output'] = isset($attributes['output']) ? $this->transformInputOutput($attributes['output']) : null;
42
43
        if (null !== $collectionOperations = $resourceMetadata->getCollectionOperations()) {
44
            $resourceMetadata = $resourceMetadata->withCollectionOperations($this->getTransformedOperations($collectionOperations, $attributes));
45
        }
46
47
        if (null !== $itemOperations = $resourceMetadata->getItemOperations()) {
48
            $resourceMetadata = $resourceMetadata->withItemOperations($this->getTransformedOperations($itemOperations, $attributes));
49
        }
50
51
        if (null !== $graphQlAttributes = $resourceMetadata->getGraphql()) {
52
            $resourceMetadata = $resourceMetadata->withGraphql($this->getTransformedOperations($graphQlAttributes, $attributes));
53
        }
54
55
        return $resourceMetadata->withAttributes($attributes);
56
    }
57
58
    private function getTransformedOperations(array $operations, array $resourceAttributes): array
59
    {
60
        foreach ($operations as $key => &$operation) {
61
            if (!\is_array($operation)) {
62
                continue;
63
            }
64
65
            $operation['input'] = isset($operation['input']) ? $this->transformInputOutput($operation['input']) : $resourceAttributes['input'];
66
            $operation['output'] = isset($operation['output']) ? $this->transformInputOutput($operation['output']) : $resourceAttributes['output'];
67
68
            if (
69
                isset($operation['input'])
70
                && \array_key_exists('class', $operation['input'])
0 ignored issues
show
It seems like $operation['input'] can also be of type null; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                && \array_key_exists('class', /** @scrutinizer ignore-type */ $operation['input'])
Loading history...
71
                && null === $operation['input']['class']
72
            ) {
73
                $operation['deserialize'] ?? $operation['deserialize'] = false;
74
                $operation['validate'] ?? $operation['validate'] = false;
75
            }
76
77
            if (
78
                isset($operation['output'])
79
                && \array_key_exists('class', $operation['output'])
80
                && null === $operation['output']['class']
81
            ) {
82
                $operation['status'] ?? $operation['status'] = 204;
83
            }
84
        }
85
86
        return $operations;
87
    }
88
89
    private function transformInputOutput($attribute): ?array
90
    {
91
        if (null === $attribute) {
92
            return null;
93
        }
94
95
        if (false === $attribute) {
96
            return ['class' => null];
97
        }
98
99
        if (\is_string($attribute)) {
100
            $attribute = ['class' => $attribute];
101
        }
102
103
        if (!isset($attribute['name']) && isset($attribute['class'])) {
104
            $attribute['name'] = (new \ReflectionClass($attribute['class']))->getShortName();
105
        }
106
107
        return $attribute;
108
    }
109
}
110