|
13 | 13 |
|
14 | 14 | namespace ApiPlatform\Api;
|
15 | 15 |
|
16 |
| -class_exists(\ApiPlatform\Metadata\IdentifiersExtractor::class); |
| 16 | +use ApiPlatform\Metadata\Exception\RuntimeException; |
| 17 | +use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation; |
| 18 | +use ApiPlatform\Metadata\HttpOperation; |
| 19 | +use ApiPlatform\Metadata\Operation; |
| 20 | +use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
| 21 | +use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
| 22 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 23 | +use ApiPlatform\Metadata\Util\ResourceClassInfoTrait; |
| 24 | +use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
| 25 | +use Symfony\Component\PropertyAccess\PropertyAccess; |
| 26 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
17 | 27 |
|
18 |
| -if (false) { |
19 |
| - final class IdentifiersExtractor extends \ApiPlatform\Metadata\IdentifiersExtractor |
| 28 | +/** |
| 29 | + * {@inheritdoc} |
| 30 | + * |
| 31 | + * @deprecated use ApiPlatform\Metadata\IdentifiersExtractor instead |
| 32 | + * |
| 33 | + * @author Antoine Bluchet <[email protected]> |
| 34 | + */ |
| 35 | +final class IdentifiersExtractor implements IdentifiersExtractorInterface |
| 36 | +{ |
| 37 | + use ResourceClassInfoTrait; |
| 38 | + private readonly PropertyAccessorInterface $propertyAccessor; |
| 39 | + |
| 40 | + public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, PropertyAccessorInterface $propertyAccessor = null) |
| 41 | + { |
| 42 | + $this->resourceMetadataFactory = $resourceMetadataFactory; |
| 43 | + $this->resourceClassResolver = $resourceClassResolver; |
| 44 | + $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + * |
| 50 | + * TODO: 3.0 identifiers should be stringable? |
| 51 | + */ |
| 52 | + public function getIdentifiersFromItem(object $item, Operation $operation = null, array $context = []): array |
| 53 | + { |
| 54 | + if (!$this->isResourceClass($this->getObjectClass($item))) { |
| 55 | + return ['id' => $this->propertyAccessor->getValue($item, 'id')]; |
| 56 | + } |
| 57 | + |
| 58 | + if ($operation && $operation->getClass()) { |
| 59 | + return $this->getIdentifiersFromOperation($item, $operation, $context); |
| 60 | + } |
| 61 | + |
| 62 | + $resourceClass = $this->getResourceClass($item, true); |
| 63 | + $operation ??= $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true); |
| 64 | + |
| 65 | + return $this->getIdentifiersFromOperation($item, $operation, $context); |
| 66 | + } |
| 67 | + |
| 68 | + private function getIdentifiersFromOperation(object $item, Operation $operation, array $context = []): array |
| 69 | + { |
| 70 | + if ($operation instanceof HttpOperation) { |
| 71 | + $links = $operation->getUriVariables(); |
| 72 | + } elseif ($operation instanceof GraphQlOperation) { |
| 73 | + $links = $operation->getLinks(); |
| 74 | + } |
| 75 | + |
| 76 | + $identifiers = []; |
| 77 | + foreach ($links ?? [] as $link) { |
| 78 | + if (1 < (is_countable($link->getIdentifiers()) ? \count($link->getIdentifiers()) : 0)) { |
| 79 | + $compositeIdentifiers = []; |
| 80 | + foreach ($link->getIdentifiers() as $identifier) { |
| 81 | + $compositeIdentifiers[$identifier] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $identifier, $link->getParameterName()); |
| 82 | + } |
| 83 | + |
| 84 | + $identifiers[$link->getParameterName()] = CompositeIdentifierParser::stringify($compositeIdentifiers); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + $parameterName = $link->getParameterName(); |
| 89 | + $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0], $parameterName, $link->getToProperty()); |
| 90 | + } |
| 91 | + |
| 92 | + return $identifiers; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Gets the value of the given class property. |
| 97 | + */ |
| 98 | + private function getIdentifierValue(object $item, string $class, string $property, string $parameterName, string $toProperty = null): float|bool|int|string |
20 | 99 | {
|
| 100 | + if ($item instanceof $class) { |
| 101 | + try { |
| 102 | + return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName); |
| 103 | + } catch (NoSuchPropertyException $e) { |
| 104 | + throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if ($toProperty) { |
| 109 | + return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName); |
| 110 | + } |
| 111 | + |
| 112 | + $resourceClass = $this->getResourceClass($item, true); |
| 113 | + foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
| 114 | + $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
| 115 | + |
| 116 | + $types = $propertyMetadata->getBuiltinTypes(); |
| 117 | + if (null === ($type = $types[0] ?? null)) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + try { |
| 122 | + if ($type->isCollection()) { |
| 123 | + $collectionValueType = $type->getCollectionValueTypes()[0] ?? null; |
| 124 | + |
| 125 | + if (null !== $collectionValueType && $collectionValueType->getClassName() === $class) { |
| 126 | + return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, sprintf('%s[0].%s', $propertyName, $property)), $parameterName); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if ($type->getClassName() === $class) { |
| 131 | + return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName); |
| 132 | + } |
| 133 | + } catch (NoSuchPropertyException $e) { |
| 134 | + throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + throw new RuntimeException('Not able to retrieve identifiers.'); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * TODO: in 3.0 this method just uses $identifierValue instanceof \Stringable and we remove the weird behavior. |
| 143 | + * |
| 144 | + * @param mixed|\Stringable $identifierValue |
| 145 | + */ |
| 146 | + private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string |
| 147 | + { |
| 148 | + if (null === $identifierValue) { |
| 149 | + throw new RuntimeException('No identifier value found, did you forget to persist the entity?'); |
| 150 | + } |
| 151 | + |
| 152 | + if (\is_scalar($identifierValue)) { |
| 153 | + return $identifierValue; |
| 154 | + } |
| 155 | + |
| 156 | + if ($identifierValue instanceof \Stringable) { |
| 157 | + return (string) $identifierValue; |
| 158 | + } |
| 159 | + |
| 160 | + if ($identifierValue instanceof \BackedEnum) { |
| 161 | + return (string) $identifierValue->value; |
| 162 | + } |
| 163 | + |
| 164 | + throw new RuntimeException(sprintf('We were not able to resolve the identifier matching parameter "%s".', $parameterName)); |
21 | 165 | }
|
22 | 166 | }
|
0 commit comments