|
| 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 | +namespace ApiPlatform\Core\Metadata\Extractor; |
| 13 | + |
| 14 | +use ApiPlatform\Core\Exception\InvalidArgumentException; |
| 15 | +use Symfony\Component\Config\Util\XmlUtils; |
| 16 | + |
| 17 | +/** |
| 18 | + * Extracts an array of metadata from a list of XML files. |
| 19 | + * |
| 20 | + * @author Kévin Dunglas <[email protected]> |
| 21 | + * @author Antoine Bluchet <[email protected]> |
| 22 | + * @author Baptiste Meyer <[email protected]> |
| 23 | + */ |
| 24 | +final class XmlExtractor extends AbstractExtractor |
| 25 | +{ |
| 26 | + const RESOURCE_SCHEMA = __DIR__.'/../schema/metadata.xsd'; |
| 27 | + |
| 28 | + /** |
| 29 | + * {@inheritdoc} |
| 30 | + */ |
| 31 | + protected function extractPath(string $path) |
| 32 | + { |
| 33 | + try { |
| 34 | + $xml = simplexml_import_dom(XmlUtils::loadFile($path, self::RESOURCE_SCHEMA)); |
| 35 | + } catch (\InvalidArgumentException $e) { |
| 36 | + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 37 | + } |
| 38 | + |
| 39 | + foreach ($xml->resource as $resource) { |
| 40 | + $resourceClass = (string) $resource['class']; |
| 41 | + |
| 42 | + $this->resources[$resourceClass] = [ |
| 43 | + 'shortName' => $this->phpize($resource, 'shortName', 'string'), |
| 44 | + 'description' => $this->phpize($resource, 'description', 'string'), |
| 45 | + 'iri' => $this->phpize($resource, 'iri', 'string'), |
| 46 | + 'itemOperations' => $this->getAttributes($resource, 'itemOperation') ?: null, |
| 47 | + 'collectionOperations' => $this->getAttributes($resource, 'collectionOperation') ?: null, |
| 48 | + 'attributes' => $this->getAttributes($resource, 'attribute') ?: null, |
| 49 | + 'properties' => $this->getProperties($resource) ?: null, |
| 50 | + ]; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Recursively transforms an attribute structure into an associative array. |
| 56 | + * |
| 57 | + * @param \SimpleXMLElement $resource |
| 58 | + * @param string $elementName |
| 59 | + * |
| 60 | + * @return array |
| 61 | + */ |
| 62 | + private function getAttributes(\SimpleXMLElement $resource, string $elementName): array |
| 63 | + { |
| 64 | + $attributes = []; |
| 65 | + foreach ($resource->$elementName as $attribute) { |
| 66 | + if (isset($attribute->attribute[0])) { |
| 67 | + $value = $this->getAttributes($attribute, 'attribute'); |
| 68 | + } else { |
| 69 | + $value = (string) $attribute; |
| 70 | + } |
| 71 | + |
| 72 | + if (isset($attribute['name'])) { |
| 73 | + $attributes[(string) $attribute['name']] = $value; |
| 74 | + } else { |
| 75 | + $attributes[] = $value; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return $attributes; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Gets metadata of a property. |
| 84 | + * |
| 85 | + * @param \SimpleXMLElement $resource |
| 86 | + * |
| 87 | + * @return array |
| 88 | + */ |
| 89 | + private function getProperties(\SimpleXMLElement $resource): array |
| 90 | + { |
| 91 | + $properties = []; |
| 92 | + foreach ($resource->property as $property) { |
| 93 | + $properties[(string) $property['name']] = [ |
| 94 | + 'description' => $this->phpize($property, 'description', 'string'), |
| 95 | + 'readable' => $this->phpize($property, 'readable', 'bool'), |
| 96 | + 'writable' => $this->phpize($property, 'writable', 'bool'), |
| 97 | + 'readableLink' => $this->phpize($property, 'readableLink', 'bool'), |
| 98 | + 'writableLink' => $this->phpize($property, 'writableLink', 'bool'), |
| 99 | + 'required' => $this->phpize($property, 'required', 'bool'), |
| 100 | + 'identifier' => $this->phpize($property, 'identifier', 'bool'), |
| 101 | + 'iri' => $this->phpize($property, 'iri', 'string'), |
| 102 | + 'attributes' => $this->getAttributes($property, 'attribute'), |
| 103 | + ]; |
| 104 | + } |
| 105 | + |
| 106 | + return $properties; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Transforms an XML attribute's value in a PHP value. |
| 111 | + * |
| 112 | + * @param \SimpleXMLElement $array |
| 113 | + * @param string $key |
| 114 | + * @param string $type |
| 115 | + * |
| 116 | + * @return bool|string|null |
| 117 | + */ |
| 118 | + private function phpize(\SimpleXMLElement $array, string $key, string $type) |
| 119 | + { |
| 120 | + if (!isset($array[$key])) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + switch ($type) { |
| 125 | + case 'string': |
| 126 | + return (string) $array[$key]; |
| 127 | + |
| 128 | + case 'bool': |
| 129 | + return (bool) XmlUtils::phpize($array[$key]); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments