|
| 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 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Core\Metadata\Property\Factory; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
| 17 | +use ApiPlatform\Exception\PropertyNotFoundException; |
| 18 | +use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * PropertyInfo metadata loader decorator. |
| 22 | + * |
| 23 | + * @author Kévin Dunglas <[email protected]> |
| 24 | + */ |
| 25 | +final class PropertyInfoPropertyMetadataFactory implements PropertyMetadataFactoryInterface |
| 26 | +{ |
| 27 | + private $propertyInfo; |
| 28 | + private $decorated; |
| 29 | + |
| 30 | + public function __construct(PropertyInfoExtractorInterface $propertyInfo, PropertyMetadataFactoryInterface $decorated = null) |
| 31 | + { |
| 32 | + $this->propertyInfo = $propertyInfo; |
| 33 | + $this->decorated = $decorated; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata |
| 40 | + { |
| 41 | + if (null === $this->decorated) { |
| 42 | + $propertyMetadata = new PropertyMetadata(); |
| 43 | + } else { |
| 44 | + try { |
| 45 | + $propertyMetadata = $this->decorated->create($resourceClass, $property, $options); |
| 46 | + } catch (PropertyNotFoundException $propertyNotFoundException) { |
| 47 | + $propertyMetadata = new PropertyMetadata(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if ($propertyMetadata instanceof PropertyMetadata) { |
| 52 | + if (!$propertyMetadata->getBuiltinTypes()) { |
| 53 | + $propertyMetadata = $propertyMetadata->withBuiltinTypes($this->propertyInfo->getTypes($resourceClass, $property, $options) ?? []); |
| 54 | + } |
| 55 | + } elseif (null === $propertyMetadata->getType()) { |
| 56 | + $propertyMetadata = $propertyMetadata->withType($this->propertyInfo->getTypes($resourceClass, $property, $options)[0] ?? null); |
| 57 | + } |
| 58 | + |
| 59 | + if (null === $propertyMetadata->getDescription() && null !== $description = $this->propertyInfo->getShortDescription($resourceClass, $property, $options)) { |
| 60 | + $propertyMetadata = $propertyMetadata->withDescription($description); |
| 61 | + } |
| 62 | + |
| 63 | + if (null === $propertyMetadata->isReadable() && null !== $readable = $this->propertyInfo->isReadable($resourceClass, $property, $options)) { |
| 64 | + $propertyMetadata = $propertyMetadata->withReadable($readable); |
| 65 | + } |
| 66 | + |
| 67 | + if (null === $propertyMetadata->isWritable() && null !== $writable = $this->propertyInfo->isWritable($resourceClass, $property, $options)) { |
| 68 | + $propertyMetadata = $propertyMetadata->withWritable($writable); |
| 69 | + } |
| 70 | + |
| 71 | + if (method_exists($this->propertyInfo, 'isInitializable')) { |
| 72 | + if (null === $propertyMetadata->isInitializable() && null !== $initializable = $this->propertyInfo->isInitializable($resourceClass, $property, $options)) { |
| 73 | + $propertyMetadata = $propertyMetadata->withInitializable($initializable); |
| 74 | + } |
| 75 | + } else { |
| 76 | + // BC layer for Symfony < 4.2 |
| 77 | + $ref = new \ReflectionClass($resourceClass); |
| 78 | + if ($ref->isInstantiable() && $constructor = $ref->getConstructor()) { |
| 79 | + foreach ($constructor->getParameters() as $constructorParameter) { |
| 80 | + if ($constructorParameter->name === $property && null === $propertyMetadata->isInitializable()) { |
| 81 | + $propertyMetadata = $propertyMetadata->withInitializable(true); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return $propertyMetadata; |
| 88 | + } |
| 89 | +} |
0 commit comments