| 1 | <?php |
||
| 32 | final class IriConverter implements IriConverterInterface |
||
| 33 | { |
||
| 34 | use ClassInfoTrait; |
||
| 35 | |||
| 36 | private $propertyNameCollectionFactory; |
||
| 37 | private $propertyMetadataFactory; |
||
| 38 | private $itemDataProvider; |
||
| 39 | private $routeNameResolver; |
||
| 40 | private $router; |
||
| 41 | private $propertyAccessor; |
||
| 42 | private $itemIdentifiersExtractor; |
||
| 43 | |||
| 44 | public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null, ItemIdentifiersExtractorInterface $itemIdentifiersExtractor = null) |
||
| 45 | { |
||
| 46 | $this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
||
| 47 | $this->propertyMetadataFactory = $propertyMetadataFactory; |
||
| 48 | $this->itemDataProvider = $itemDataProvider; |
||
| 49 | $this->routeNameResolver = $routeNameResolver; |
||
| 50 | $this->router = $router; |
||
| 51 | $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); |
||
| 52 | |||
| 53 | if (!$itemIdentifiersExtractor) { |
||
| 54 | @trigger_error('Not injecting ItemIdentifiersExtractor is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3'); |
||
|
1 ignored issue
–
show
|
|||
| 55 | $this->itemIdentifiersExtractor = new ItemIdentifiersExtractor($this->propertyNameCollectionFactory, $this->propertyMetadataFactory, $this->propertyAccessor); |
||
| 56 | } else { |
||
| 57 | $this->itemIdentifiersExtractor = $itemIdentifiersExtractor; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | public function getItemFromIri(string $iri, array $context = []) |
||
| 65 | { |
||
| 66 | try { |
||
| 67 | $parameters = $this->router->match($iri); |
||
| 68 | } catch (RoutingExceptionInterface $e) { |
||
| 69 | throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (!isset($parameters['_api_resource_class'], $parameters['id'])) { |
||
| 73 | throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri)); |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($item = $this->itemDataProvider->getItem($parameters['_api_resource_class'], $parameters['id'], null, $context)) { |
||
| 77 | return $item; |
||
| 78 | } |
||
| 79 | |||
| 80 | throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri)); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
||
| 87 | { |
||
| 88 | $resourceClass = $this->getObjectClass($item); |
||
| 89 | $routeName = $this->routeNameResolver->getRouteName($resourceClass, false); |
||
| 90 | |||
| 91 | $identifiers = $this->generateIdentifiersUrl($this->itemIdentifiersExtractor->getIdentifiersFromItem($item)); |
||
| 92 | |||
| 93 | return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Generate the identifier url. |
||
| 110 | * |
||
| 111 | * @param array $identifiers |
||
| 112 | * |
||
| 113 | * @return string[] |
||
| 114 | */ |
||
| 115 | private function generateIdentifiersUrl(array $identifiers): array |
||
| 127 | } |
||
| 128 |
If you suppress an error, we recommend checking for the error condition explicitly: