|
| 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\Api; |
| 15 | + |
| 16 | +use ApiPlatform\Api\IriConverterInterface; |
| 17 | +use ApiPlatform\Api\UrlGeneratorInterface; |
| 18 | +use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface; |
| 19 | +use ApiPlatform\Metadata\Operation; |
| 20 | + |
| 21 | +/** |
| 22 | + * This IRI converter calls the legacy IriConverter on legacy resources. |
| 23 | + * |
| 24 | + * @author Antoine Bluchet <[email protected]> |
| 25 | + * |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +final class LegacyIriConverter implements IriConverterInterface |
| 29 | +{ |
| 30 | + private $legacyIriConverter; |
| 31 | + private $iriConverter; |
| 32 | + |
| 33 | + public function __construct(LegacyIriConverterInterface $legacyIriConverter, IriConverterInterface $iriConverter) |
| 34 | + { |
| 35 | + $this->legacyIriConverter = $legacyIriConverter; |
| 36 | + $this->iriConverter = $iriConverter; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritdoc} |
| 41 | + */ |
| 42 | + public function getResourceFromIri(string $iri, array $context = [], ?Operation $operation = null) |
| 43 | + { |
| 44 | + if (!$operation && !($operation = $context['operation'] ?? null)) { |
| 45 | + return $this->iriConverter->getResourceFromIri($iri, $context); |
| 46 | + } |
| 47 | + |
| 48 | + if (!($operation->getExtraProperties()['is_legacy_resource_metadata'] ?? false) && !($operation->getExtraProperties()['is_legacy_subresource'] ?? false)) { |
| 49 | + return $this->iriConverter->getResourceFromIri($iri, $context, $operation); |
| 50 | + } |
| 51 | + |
| 52 | + return $this->legacyIriConverter->getItemFromIri($iri, $context); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + */ |
| 58 | + public function getIriFromResource($item, int $referenceType = UrlGeneratorInterface::ABS_PATH, Operation $operation = null, array $context = []): ?string |
| 59 | + { |
| 60 | + return $this->iriConverter->getIriFromResource($item, $referenceType, $operation, $context); |
| 61 | + } |
| 62 | +} |
0 commit comments