|
| 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 | +namespace ApiPlatform\Core\Metadata\Property\Factory; |
| 13 | + |
| 14 | +use ApiPlatform\Core\Exception\InvalidArgumentException; |
| 15 | +use ApiPlatform\Core\Exception\PropertyNotFoundException; |
| 16 | +use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
| 17 | +use Symfony\Component\Config\Util\XmlUtils; |
| 18 | + |
| 19 | +/** |
| 20 | + * Creates a property metadata from XML {@see Property} configuration. |
| 21 | + * |
| 22 | + * @author Baptiste Meyer <baptiste.meyer@gmail.com> |
| 23 | + */ |
| 24 | +class XmlPropertyMetadataFactory implements PropertyMetadataFactoryInterface |
| 25 | +{ |
| 26 | + const RESOURCE_SCHEMA = __DIR__.'/../../schema/metadata.xsd'; |
| 27 | + |
| 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 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 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 (!class_exists($resourceClass) || !property_exists($resourceClass, $property) || empty($propertyMetadata = $this->getMetadata($resourceClass, $property))) { |
| 56 | + return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property); |
| 57 | + } |
| 58 | + |
| 59 | + if ($parentPropertyMetadata) { |
| 60 | + return $this->update($parentPropertyMetadata, $propertyMetadata); |
| 61 | + } |
| 62 | + |
| 63 | + return new PropertyMetadata( |
| 64 | + null, |
| 65 | + $propertyMetadata['description'], |
| 66 | + $propertyMetadata['readable'], |
| 67 | + $propertyMetadata['writable'], |
| 68 | + $propertyMetadata['readableLink'], |
| 69 | + $propertyMetadata['writableLink'], |
| 70 | + $propertyMetadata['required'], |
| 71 | + $propertyMetadata['identifier'], |
| 72 | + $propertyMetadata['iri'], |
| 73 | + null, |
| 74 | + $propertyMetadata['attributes'] |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns the metadata from the decorated factory if available or throws an exception. |
| 80 | + * |
| 81 | + * @param PropertyMetadata|null $parentPropertyMetadata |
| 82 | + * @param string $resourceClass |
| 83 | + * @param string $property |
| 84 | + * |
| 85 | + * @throws PropertyNotFoundException |
| 86 | + * |
| 87 | + * @return PropertyMetadata |
| 88 | + */ |
| 89 | + private function handleNotFound(PropertyMetadata $parentPropertyMetadata = null, string $resourceClass, string $property) : PropertyMetadata |
| 90 | + { |
| 91 | + if ($parentPropertyMetadata) { |
| 92 | + return $parentPropertyMetadata; |
| 93 | + } |
| 94 | + |
| 95 | + throw new PropertyNotFoundException(sprintf('Property "%s" of the resource class "%s" not found.', $property, $resourceClass)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Extracts metadata from the XML tree. |
| 100 | + * |
| 101 | + * @param string $resourceClass |
| 102 | + * @param string $propertyName |
| 103 | + * |
| 104 | + * @throws InvalidArgumentException |
| 105 | + * |
| 106 | + * @return array |
| 107 | + */ |
| 108 | + private function getMetadata(string $resourceClass, string $propertyName) : array |
| 109 | + { |
| 110 | + foreach ($this->paths as $path) { |
| 111 | + try { |
| 112 | + $domDocument = XmlUtils::loadFile($path, self::RESOURCE_SCHEMA); |
| 113 | + } catch (\InvalidArgumentException $e) { |
| 114 | + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 115 | + } |
| 116 | + |
| 117 | + $properties = (new \DOMXPath($domDocument))->query(sprintf('//resources/resource[@class="%s"]/property[@name="%s"]', $resourceClass, $propertyName)); |
| 118 | + |
| 119 | + if (false === $properties || 0 >= $properties->length || null === $properties->item(0) || false === $property = simplexml_import_dom($properties->item(0))) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + return [ |
| 124 | + 'description' => (string) $property['description'] ?: null, |
| 125 | + 'readable' => $property['readable'] ? (bool) XmlUtils::phpize($property['readable']) : null, |
| 126 | + 'writable' => $property['writable'] ? (bool) XmlUtils::phpize($property['writable']) : null, |
| 127 | + 'readableLink' => $property['readableLink'] ? (bool) XmlUtils::phpize($property['readableLink']) : null, |
| 128 | + 'writableLink' => $property['writableLink'] ? (bool) XmlUtils::phpize($property['writableLink']) : null, |
| 129 | + 'required' => $property['required'] ? (bool) XmlUtils::phpize($property['required']) : null, |
| 130 | + 'identifier' => $property['identifier'] ? (bool) XmlUtils::phpize($property['identifier']) : null, |
| 131 | + 'iri' => (string) $property['iri'] ?: null, |
| 132 | + 'attributes' => $this->getAttributes($property), |
| 133 | + ]; |
| 134 | + } |
| 135 | + |
| 136 | + return []; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Recursively transforms an attribute structure into an associative array. |
| 141 | + * |
| 142 | + * @param \SimpleXMLElement $element |
| 143 | + * |
| 144 | + * @return array |
| 145 | + */ |
| 146 | + private function getAttributes(\SimpleXMLElement $element) : array |
| 147 | + { |
| 148 | + $attributes = []; |
| 149 | + foreach ($element->attribute as $attribute) { |
| 150 | + $value = isset($attribute->attribute[0]) ? $this->getAttributes($attribute) : (string) $attribute; |
| 151 | + |
| 152 | + if (isset($attribute['name'])) { |
| 153 | + $attributes[(string) $attribute['name']] = $value; |
| 154 | + } else { |
| 155 | + $attributes[] = $value; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return $attributes; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Creates a new instance of metadata if the property is not already set. |
| 164 | + * |
| 165 | + * @param PropertyMetadata $propertyMetadata |
| 166 | + * @param array $metadata |
| 167 | + * |
| 168 | + * @return PropertyMetadata |
| 169 | + */ |
| 170 | + private function update(PropertyMetadata $propertyMetadata, array $metadata) : PropertyMetadata |
| 171 | + { |
| 172 | + $metadataAccessors = [ |
| 173 | + 'description' => 'get', |
| 174 | + 'readable' => 'is', |
| 175 | + 'writable' => 'is', |
| 176 | + 'writableLink' => 'is', |
| 177 | + 'readableLink' => 'is', |
| 178 | + 'required' => 'is', |
| 179 | + 'identifier' => 'is', |
| 180 | + 'iri' => 'get', |
| 181 | + 'attributes' => 'get', |
| 182 | + ]; |
| 183 | + |
| 184 | + foreach ($metadataAccessors as $metadataKey => $accessorPrefix) { |
| 185 | + if (null === $metadata[$metadataKey] || null !== $propertyMetadata->{$accessorPrefix.ucfirst($metadataKey)}()) { |
| 186 | + continue; |
| 187 | + } |
| 188 | + |
| 189 | + $propertyMetadata = $propertyMetadata->{'with'.ucfirst($metadataKey)}($metadata[$metadataKey]); |
| 190 | + } |
| 191 | + |
| 192 | + return $propertyMetadata; |
| 193 | + } |
| 194 | +} |
0 commit comments