|
12 | 12 | namespace ApiPlatform\Core\Metadata\Property\Factory;
|
13 | 13 |
|
14 | 14 | use ApiPlatform\Core\Exception\InvalidArgumentException;
|
15 |
| -use ApiPlatform\Core\Exception\PropertyNotFoundException; |
16 |
| -use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
17 | 15 | use Symfony\Component\Config\Util\XmlUtils;
|
18 | 16 |
|
19 | 17 | /**
|
20 | 18 | * Creates a property metadata from XML {@see Property} configuration.
|
21 | 19 | *
|
22 | 20 | * @author Baptiste Meyer <[email protected]>
|
23 | 21 | */
|
24 |
| -class XmlPropertyMetadataFactory implements PropertyMetadataFactoryInterface |
| 22 | +final class XmlPropertyMetadataFactory extends AbstractFilePropertyMetadataFactory |
25 | 23 | {
|
26 | 24 | const RESOURCE_SCHEMA = __DIR__.'/../../schema/metadata.xsd';
|
27 | 25 |
|
28 |
| - private $paths; |
29 |
| - private $decorated; |
30 |
| - |
31 |
| - /** |
32 |
| - * @param string[] $paths |
33 |
| - * @param PropertyMetadataFactoryInterface|null $decorated |
34 |
| - */ |
35 |
| - public function __construct(array $paths, PropertyMetadataFactoryInterface $decorated = null) |
36 |
| - { |
37 |
| - $this->paths = $paths; |
38 |
| - $this->decorated = $decorated; |
39 |
| - } |
40 |
| - |
41 | 26 | /**
|
42 | 27 | * {@inheritdoc}
|
43 | 28 | */
|
44 |
| - public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata |
45 |
| - { |
46 |
| - $parentPropertyMetadata = null; |
47 |
| - if ($this->decorated) { |
48 |
| - try { |
49 |
| - $parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options); |
50 |
| - } catch (PropertyNotFoundException $propertyNotFoundException) { |
51 |
| - // Ignore not found exception from decorated factories |
52 |
| - } |
53 |
| - } |
54 |
| - |
55 |
| - if ( |
56 |
| - !property_exists($resourceClass, $property) || |
57 |
| - empty($propertyMetadata = $this->getMetadata($resourceClass, $property)) |
58 |
| - ) { |
59 |
| - return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property); |
60 |
| - } |
61 |
| - |
62 |
| - if ($parentPropertyMetadata) { |
63 |
| - return $this->update($parentPropertyMetadata, $propertyMetadata); |
64 |
| - } |
65 |
| - |
66 |
| - return new PropertyMetadata( |
67 |
| - null, |
68 |
| - $propertyMetadata['description'], |
69 |
| - $propertyMetadata['readable'], |
70 |
| - $propertyMetadata['writable'], |
71 |
| - $propertyMetadata['readableLink'], |
72 |
| - $propertyMetadata['writableLink'], |
73 |
| - $propertyMetadata['required'], |
74 |
| - $propertyMetadata['identifier'], |
75 |
| - $propertyMetadata['iri'], |
76 |
| - null, |
77 |
| - $propertyMetadata['attributes'] |
78 |
| - ); |
79 |
| - } |
80 |
| - |
81 |
| - /** |
82 |
| - * Returns the metadata from the decorated factory if available or throws an exception. |
83 |
| - * |
84 |
| - * @param PropertyMetadata|null $parentPropertyMetadata |
85 |
| - * @param string $resourceClass |
86 |
| - * @param string $property |
87 |
| - * |
88 |
| - * @throws PropertyNotFoundException |
89 |
| - * |
90 |
| - * @return PropertyMetadata |
91 |
| - */ |
92 |
| - private function handleNotFound(PropertyMetadata $parentPropertyMetadata = null, string $resourceClass, string $property): PropertyMetadata |
93 |
| - { |
94 |
| - if ($parentPropertyMetadata) { |
95 |
| - return $parentPropertyMetadata; |
96 |
| - } |
97 |
| - |
98 |
| - throw new PropertyNotFoundException(sprintf('Property "%s" of the resource class "%s" not found.', $property, $resourceClass)); |
99 |
| - } |
100 |
| - |
101 |
| - /** |
102 |
| - * Extracts metadata from the XML tree. |
103 |
| - * |
104 |
| - * @param string $resourceClass |
105 |
| - * @param string $propertyName |
106 |
| - * |
107 |
| - * @throws InvalidArgumentException |
108 |
| - * |
109 |
| - * @return array |
110 |
| - */ |
111 |
| - private function getMetadata(string $resourceClass, string $propertyName): array |
| 29 | + protected function getMetadata(string $resourceClass, string $propertyName): array |
112 | 30 | {
|
113 | 31 | foreach ($this->paths as $path) {
|
114 | 32 | try {
|
@@ -166,37 +84,4 @@ private function getAttributes(\SimpleXMLElement $element): array
|
166 | 84 |
|
167 | 85 | return $attributes;
|
168 | 86 | }
|
169 |
| - |
170 |
| - /** |
171 |
| - * Creates a new instance of metadata if the property is not already set. |
172 |
| - * |
173 |
| - * @param PropertyMetadata $propertyMetadata |
174 |
| - * @param array $metadata |
175 |
| - * |
176 |
| - * @return PropertyMetadata |
177 |
| - */ |
178 |
| - private function update(PropertyMetadata $propertyMetadata, array $metadata): PropertyMetadata |
179 |
| - { |
180 |
| - $metadataAccessors = [ |
181 |
| - 'description' => 'get', |
182 |
| - 'readable' => 'is', |
183 |
| - 'writable' => 'is', |
184 |
| - 'writableLink' => 'is', |
185 |
| - 'readableLink' => 'is', |
186 |
| - 'required' => 'is', |
187 |
| - 'identifier' => 'is', |
188 |
| - 'iri' => 'get', |
189 |
| - 'attributes' => 'get', |
190 |
| - ]; |
191 |
| - |
192 |
| - foreach ($metadataAccessors as $metadataKey => $accessorPrefix) { |
193 |
| - if (null === $metadata[$metadataKey] || null !== $propertyMetadata->{$accessorPrefix.ucfirst($metadataKey)}()) { |
194 |
| - continue; |
195 |
| - } |
196 |
| - |
197 |
| - $propertyMetadata = $propertyMetadata->{'with'.ucfirst($metadataKey)}($metadata[$metadataKey]); |
198 |
| - } |
199 |
| - |
200 |
| - return $propertyMetadata; |
201 |
| - } |
202 | 87 | }
|
0 commit comments