|
| 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\Yaml\Exception\ParseException; |
| 18 | +use Symfony\Component\Yaml\Yaml; |
| 19 | + |
| 20 | +/** |
| 21 | + * Creates a property metadata from YAML {@see Property} configuration files. |
| 22 | + * |
| 23 | + * @author Baptiste Meyer <baptiste.meyer@gmail.com> |
| 24 | + */ |
| 25 | +class YamlPropertyMetadataFactory implements PropertyMetadataFactoryInterface |
| 26 | +{ |
| 27 | + private $paths; |
| 28 | + private $decorated; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param array $paths |
| 32 | + * @param PropertyMetadataFactoryInterface|null $decorated |
| 33 | + */ |
| 34 | + public function __construct(array $paths, PropertyMetadataFactoryInterface $decorated = null) |
| 35 | + { |
| 36 | + $this->paths = $paths; |
| 37 | + $this->decorated = $decorated; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritdoc} |
| 42 | + */ |
| 43 | + public function create(string $resourceClass, string $property, array $options = []) : PropertyMetadata |
| 44 | + { |
| 45 | + $parentPropertyMetadata = null; |
| 46 | + if ($this->decorated) { |
| 47 | + try { |
| 48 | + $parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options); |
| 49 | + } catch (PropertyNotFoundException $propertyNotFoundException) { |
| 50 | + // Ignore not found exception from decorated factories |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if ( |
| 55 | + !property_exists($resourceClass, $property) || |
| 56 | + empty($propertyMetadata = $this->getMetadata($resourceClass, $property)) |
| 57 | + ) { |
| 58 | + return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property); |
| 59 | + } |
| 60 | + |
| 61 | + if ($parentPropertyMetadata) { |
| 62 | + return $this->update($parentPropertyMetadata, $propertyMetadata); |
| 63 | + } |
| 64 | + |
| 65 | + return new PropertyMetadata( |
| 66 | + null, |
| 67 | + $propertyMetadata['description'], |
| 68 | + $propertyMetadata['readable'], |
| 69 | + $propertyMetadata['writable'], |
| 70 | + $propertyMetadata['readableLink'], |
| 71 | + $propertyMetadata['writableLink'], |
| 72 | + $propertyMetadata['required'], |
| 73 | + $propertyMetadata['identifier'], |
| 74 | + $propertyMetadata['iri'], |
| 75 | + null, |
| 76 | + $propertyMetadata['attributes'] |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the metadata from the decorated factory if available or throws an exception. |
| 82 | + * |
| 83 | + * @param PropertyMetadata|null $parentPropertyMetadata |
| 84 | + * @param string $resourceClass |
| 85 | + * @param string $property |
| 86 | + * |
| 87 | + * @throws PropertyNotFoundException |
| 88 | + * |
| 89 | + * @return PropertyMetadata |
| 90 | + */ |
| 91 | + private function handleNotFound(PropertyMetadata $parentPropertyMetadata = null, string $resourceClass, string $property) : PropertyMetadata |
| 92 | + { |
| 93 | + if ($parentPropertyMetadata) { |
| 94 | + return $parentPropertyMetadata; |
| 95 | + } |
| 96 | + |
| 97 | + throw new PropertyNotFoundException(sprintf('Property "%s" of the resource class "%s" not found.', $property, $resourceClass)); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Extracts metadata from the YAML tree. |
| 102 | + * |
| 103 | + * @param string $resourceClass |
| 104 | + * @param string $property |
| 105 | + * |
| 106 | + * @throws ParseException |
| 107 | + * @throws InvalidArgumentException |
| 108 | + * |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + private function getMetadata(string $resourceClass, string $property) : array |
| 112 | + { |
| 113 | + foreach ($this->paths as $path) { |
| 114 | + try { |
| 115 | + $resources = Yaml::parse(file_get_contents($path)); |
| 116 | + } catch (ParseException $parseException) { |
| 117 | + $parseException->setParsedFile($path); |
| 118 | + |
| 119 | + throw $parseException; |
| 120 | + } |
| 121 | + |
| 122 | + if (null === $resources = $resources['resources'] ?? $resources) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if (!is_array($resources)) { |
| 127 | + throw new InvalidArgumentException(sprintf('"resources" setting is expected to be null or an array, %s given in "%s".', gettype($resources), $path)); |
| 128 | + } |
| 129 | + |
| 130 | + foreach ($resources as $resourceName => $resource) { |
| 131 | + if (null === $resource) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + if (!is_array($resource)) { |
| 136 | + throw new InvalidArgumentException(sprintf('"%s" setting is expected to be null or an array, %s given in "%s".', $resourceName, gettype($resource), $path)); |
| 137 | + } |
| 138 | + |
| 139 | + if (!isset($resource['class'])) { |
| 140 | + throw new InvalidArgumentException(sprintf('"class" setting is expected to be a string, none given in "%s".', $path)); |
| 141 | + } |
| 142 | + |
| 143 | + if ($resourceClass !== $resource['class'] || !isset($resource['properties'])) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + if (!is_array($resource['properties'])) { |
| 148 | + throw new InvalidArgumentException(sprintf('"properties" setting is expected to be null or an array, %s given in "%s".', gettype($resource['properties']), $path)); |
| 149 | + } |
| 150 | + |
| 151 | + foreach ($resource['properties'] as $propertyName => $propertyValues) { |
| 152 | + if (null === $propertyValues) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + if (!is_array($propertyValues)) { |
| 157 | + throw new InvalidArgumentException(sprintf('"%s" setting is expected to be null or an array, %s given in "%s".', $propertyName, gettype($propertyValues), $path)); |
| 158 | + } |
| 159 | + |
| 160 | + if ($property !== $propertyName) { |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + return [ |
| 165 | + 'description' => isset($propertyValues['description']) && is_scalar($propertyValues['description']) ? $propertyValues['description'] : null, |
| 166 | + 'readable' => isset($propertyValues['readable']) && is_bool($propertyValues['readable']) ? $propertyValues['readable'] : null, |
| 167 | + 'writable' => isset($propertyValues['writable']) && is_bool($propertyValues['writable']) ? $propertyValues['writable'] : null, |
| 168 | + 'readableLink' => isset($propertyValues['readableLink']) && is_bool($propertyValues['readableLink']) ? $propertyValues['readableLink'] : null, |
| 169 | + 'writableLink' => isset($propertyValues['writableLink']) && is_bool($propertyValues['writableLink']) ? $propertyValues['writableLink'] : null, |
| 170 | + 'required' => isset($propertyValues['required']) && is_bool($propertyValues['required']) ? $propertyValues['required'] : null, |
| 171 | + 'identifier' => isset($propertyValues['identifier']) && is_bool($propertyValues['identifier']) ? $propertyValues['identifier'] : null, |
| 172 | + 'iri' => isset($propertyValues['iri']) && is_scalar($propertyValues['iri']) ? $propertyValues['iri'] : null, |
| 173 | + 'attributes' => $propertyValues['attributes'] ?? null, |
| 174 | + ]; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return []; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Creates a new instance of metadata if the property is not already set. |
| 184 | + * |
| 185 | + * @param PropertyMetadata $propertyMetadata |
| 186 | + * @param array $metadata |
| 187 | + * |
| 188 | + * @return PropertyMetadata |
| 189 | + */ |
| 190 | + private function update(PropertyMetadata $propertyMetadata, array $metadata) : PropertyMetadata |
| 191 | + { |
| 192 | + $metadataAccessors = [ |
| 193 | + 'description' => 'get', |
| 194 | + 'readable' => 'is', |
| 195 | + 'writable' => 'is', |
| 196 | + 'writableLink' => 'is', |
| 197 | + 'readableLink' => 'is', |
| 198 | + 'required' => 'is', |
| 199 | + 'identifier' => 'is', |
| 200 | + 'iri' => 'get', |
| 201 | + 'attributes' => 'get', |
| 202 | + ]; |
| 203 | + |
| 204 | + foreach ($metadataAccessors as $metadataKey => $accessorPrefix) { |
| 205 | + if (null === $metadata[$metadataKey] || null !== $propertyMetadata->{$accessorPrefix.ucfirst($metadataKey)}()) { |
| 206 | + continue; |
| 207 | + } |
| 208 | + |
| 209 | + $propertyMetadata = $propertyMetadata->{'with'.ucfirst($metadataKey)}($metadata[$metadataKey]); |
| 210 | + } |
| 211 | + |
| 212 | + return $propertyMetadata; |
| 213 | + } |
| 214 | +} |
0 commit comments