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

Factory/ExtractorPropertyMetadataFactory.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\Property\Factory;
15
16
use ApiPlatform\Core\Exception\PropertyNotFoundException;
17
use ApiPlatform\Core\Metadata\Extractor\ExtractorInterface;
18
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
19
use ApiPlatform\Core\Metadata\Property\SubresourceMetadata;
20
21
/**
22
 * Creates properties's metadata using an extractor.
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 */
26
final class ExtractorPropertyMetadataFactory implements PropertyMetadataFactoryInterface
27
{
28
    private $extractor;
29
    private $decorated;
30
31
    public function __construct(ExtractorInterface $extractor, PropertyMetadataFactoryInterface $decorated = null)
32
    {
33
        $this->extractor = $extractor;
34
        $this->decorated = $decorated;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
41
    {
42
        $parentPropertyMetadata = null;
43
        if ($this->decorated) {
44
            try {
45
                $parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options);
46
            } catch (PropertyNotFoundException $propertyNotFoundException) {
47
                // Ignore not found exception from decorated factories
48
            }
49
        }
50
51
        $isInterface = interface_exists($resourceClass);
52
53
        if (
54
            !property_exists($resourceClass, $property) && !$isInterface ||
0 ignored issues
show
Consider adding parentheses for clarity. Current Interpretation: (! property_exists($reso...es'][$property] ?? null, Probably Intended Meaning: ! property_exists($resou...s'][$property] ?? null)
Loading history...
55
            null === ($propertyMetadata = $this->extractor->getResources()[$resourceClass]['properties'][$property] ?? null)
56
        ) {
57
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
58
        }
59
60
        if ($parentPropertyMetadata) {
61
            return $this->update($parentPropertyMetadata, $propertyMetadata);
62
        }
63
64
        return ($metadata = new PropertyMetadata(
65
            null,
66
            $propertyMetadata['description'],
67
            $propertyMetadata['readable'],
68
            $propertyMetadata['writable'],
69
            $propertyMetadata['readableLink'],
70
            $propertyMetadata['writableLink'],
71
            $propertyMetadata['required'],
72
            $propertyMetadata['identifier'],
73
            $propertyMetadata['iri'],
74
            null,
75
            $propertyMetadata['attributes']
76
        ))->withSubresource($this->createSubresourceMetadata($propertyMetadata['subresource'], $metadata));
77
    }
78
79
    /**
80
     * Returns the metadata from the decorated factory if available or throws an exception.
81
     *
82
     * @throws PropertyNotFoundException
83
     */
84
    private function handleNotFound(?PropertyMetadata $parentPropertyMetadata, string $resourceClass, string $property): PropertyMetadata
85
    {
86
        if ($parentPropertyMetadata) {
87
            return $parentPropertyMetadata;
88
        }
89
90
        throw new PropertyNotFoundException(sprintf('Property "%s" of the resource class "%s" not found.', $property, $resourceClass));
91
    }
92
93
    /**
94
     * Creates a new instance of metadata if the property is not already set.
95
     */
96
    private function update(PropertyMetadata $propertyMetadata, array $metadata): PropertyMetadata
97
    {
98
        $metadataAccessors = [
99
            'description' => 'get',
100
            'readable' => 'is',
101
            'writable' => 'is',
102
            'writableLink' => 'is',
103
            'readableLink' => 'is',
104
            'required' => 'is',
105
            'identifier' => 'is',
106
            'iri' => 'get',
107
            'attributes' => 'get',
108
        ];
109
110
        foreach ($metadataAccessors as $metadataKey => $accessorPrefix) {
111
            if (null === $metadata[$metadataKey]) {
112
                continue;
113
            }
114
115
            $propertyMetadata = $propertyMetadata->{'with'.ucfirst($metadataKey)}($metadata[$metadataKey]);
116
        }
117
118
        if ($propertyMetadata->hasSubresource()) {
119
            return $propertyMetadata;
120
        }
121
122
        return $propertyMetadata->withSubresource($this->createSubresourceMetadata($metadata['subresource'], $propertyMetadata));
123
    }
124
125
    /**
126
     * Creates a SubresourceMetadata.
127
     *
128
     * @param bool|array|null  $subresource      the subresource metadata coming from XML or YAML
129
     * @param PropertyMetadata $propertyMetadata the current property metadata
130
     */
131
    private function createSubresourceMetadata($subresource, PropertyMetadata $propertyMetadata): ?SubresourceMetadata
132
    {
133
        if (!$subresource) {
134
            return null;
135
        }
136
137
        $type = $propertyMetadata->getType();
138
        $maxDepth = \is_array($subresource) ? $subresource['maxDepth'] ?? null : null;
139
140
        if (null !== $type) {
141
            $isCollection = $type->isCollection();
142
            $resourceClass = $isCollection && ($collectionValueType = $type->getCollectionValueType()) ? $collectionValueType->getClassName() : $type->getClassName();
143
        } elseif (\is_array($subresource) && isset($subresource['resourceClass'])) {
144
            $resourceClass = $subresource['resourceClass'];
145
            $isCollection = $subresource['collection'] ?? true;
146
        } else {
147
            return null;
148
        }
149
150
        return new SubresourceMetadata($resourceClass, $isCollection, $maxDepth);
151
    }
152
}
153