/
XmlPropertyMetadataFactory.php
202 lines (178 loc) · 6.91 KB
/
XmlPropertyMetadataFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ApiPlatform\Core\Metadata\Property\Factory;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\PropertyNotFoundException;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use Symfony\Component\Config\Util\XmlUtils;
/**
* Creates a property metadata from XML {@see Property} configuration.
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
*/
class XmlPropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
const RESOURCE_SCHEMA = __DIR__.'/../../schema/metadata.xsd';
private $paths;
private $decorated;
/**
* @param string[] $paths
* @param PropertyMetadataFactoryInterface|null $decorated
*/
public function __construct(array $paths, PropertyMetadataFactoryInterface $decorated = null)
{
$this->paths = $paths;
$this->decorated = $decorated;
}
/**
* {@inheritdoc}
*/
public function create(string $resourceClass, string $property, array $options = []) : PropertyMetadata
{
$parentPropertyMetadata = null;
if ($this->decorated) {
try {
$parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options);
} catch (PropertyNotFoundException $propertyNotFoundException) {
// Ignore not found exception from decorated factories
}
}
if (
!property_exists($resourceClass, $property) ||
empty($propertyMetadata = $this->getMetadata($resourceClass, $property))
) {
return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
}
if ($parentPropertyMetadata) {
return $this->update($parentPropertyMetadata, $propertyMetadata);
}
return new PropertyMetadata(
null,
$propertyMetadata['description'],
$propertyMetadata['readable'],
$propertyMetadata['writable'],
$propertyMetadata['readableLink'],
$propertyMetadata['writableLink'],
$propertyMetadata['required'],
$propertyMetadata['identifier'],
$propertyMetadata['iri'],
null,
$propertyMetadata['attributes']
);
}
/**
* Returns the metadata from the decorated factory if available or throws an exception.
*
* @param PropertyMetadata|null $parentPropertyMetadata
* @param string $resourceClass
* @param string $property
*
* @throws PropertyNotFoundException
*
* @return PropertyMetadata
*/
private function handleNotFound(PropertyMetadata $parentPropertyMetadata = null, string $resourceClass, string $property) : PropertyMetadata
{
if ($parentPropertyMetadata) {
return $parentPropertyMetadata;
}
throw new PropertyNotFoundException(sprintf('Property "%s" of the resource class "%s" not found.', $property, $resourceClass));
}
/**
* Extracts metadata from the XML tree.
*
* @param string $resourceClass
* @param string $propertyName
*
* @throws InvalidArgumentException
*
* @return array
*/
private function getMetadata(string $resourceClass, string $propertyName) : array
{
foreach ($this->paths as $path) {
try {
$domDocument = XmlUtils::loadFile($path, self::RESOURCE_SCHEMA);
} catch (\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
$properties = (new \DOMXPath($domDocument))->query(sprintf('//resources/resource[@class="%s"]/property[@name="%s"]', $resourceClass, $propertyName));
if (
false === $properties ||
0 >= $properties->length ||
null === $properties->item(0) ||
false === $property = simplexml_import_dom($properties->item(0))
) {
continue;
}
return [
'description' => (string) $property['description'] ?: null,
'readable' => $property['readable'] ? (bool) XmlUtils::phpize($property['readable']) : null,
'writable' => $property['writable'] ? (bool) XmlUtils::phpize($property['writable']) : null,
'readableLink' => $property['readableLink'] ? (bool) XmlUtils::phpize($property['readableLink']) : null,
'writableLink' => $property['writableLink'] ? (bool) XmlUtils::phpize($property['writableLink']) : null,
'required' => $property['required'] ? (bool) XmlUtils::phpize($property['required']) : null,
'identifier' => $property['identifier'] ? (bool) XmlUtils::phpize($property['identifier']) : null,
'iri' => (string) $property['iri'] ?: null,
'attributes' => $this->getAttributes($property),
];
}
return [];
}
/**
* Recursively transforms an attribute structure into an associative array.
*
* @param \SimpleXMLElement $element
*
* @return array
*/
private function getAttributes(\SimpleXMLElement $element) : array
{
$attributes = [];
foreach ($element->attribute as $attribute) {
$value = isset($attribute->attribute[0]) ? $this->getAttributes($attribute) : (string) $attribute;
if (isset($attribute['name'])) {
$attributes[(string) $attribute['name']] = $value;
} else {
$attributes[] = $value;
}
}
return $attributes;
}
/**
* Creates a new instance of metadata if the property is not already set.
*
* @param PropertyMetadata $propertyMetadata
* @param array $metadata
*
* @return PropertyMetadata
*/
private function update(PropertyMetadata $propertyMetadata, array $metadata) : PropertyMetadata
{
$metadataAccessors = [
'description' => 'get',
'readable' => 'is',
'writable' => 'is',
'writableLink' => 'is',
'readableLink' => 'is',
'required' => 'is',
'identifier' => 'is',
'iri' => 'get',
'attributes' => 'get',
];
foreach ($metadataAccessors as $metadataKey => $accessorPrefix) {
if (null === $metadata[$metadataKey] || null !== $propertyMetadata->{$accessorPrefix.ucfirst($metadataKey)}()) {
continue;
}
$propertyMetadata = $propertyMetadata->{'with'.ucfirst($metadataKey)}($metadata[$metadataKey]);
}
return $propertyMetadata;
}
}