|
| 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\Tests\Fixtures\TestBundle\State\Issue5605; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Get; |
| 17 | +use ApiPlatform\Metadata\Operation; |
| 18 | +use ApiPlatform\State\ProviderInterface; |
| 19 | +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5605\MainResource; |
| 20 | +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5605\SubResource; |
| 21 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyWithSubEntity; |
| 22 | + |
| 23 | +class MainResourceProvider implements ProviderInterface |
| 24 | +{ |
| 25 | + public function __construct(private readonly ProviderInterface $itemProvider, private readonly ProviderInterface $collectionProvider) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null |
| 30 | + { |
| 31 | + if ($operation instanceof Get) { |
| 32 | + /** |
| 33 | + * @var DummyWithSubEntity $entity |
| 34 | + */ |
| 35 | + $entity = $this->itemProvider->provide($operation, $uriVariables, $context); |
| 36 | + |
| 37 | + return $this->getResource($entity); |
| 38 | + } |
| 39 | + $resources = []; |
| 40 | + $entities = $this->collectionProvider->provide($operation, $uriVariables, $context); |
| 41 | + foreach ($entities as $entity) { |
| 42 | + $resources[] = $this->getResource($entity); |
| 43 | + } |
| 44 | + |
| 45 | + return $resources; |
| 46 | + } |
| 47 | + |
| 48 | + protected function getResource(DummyWithSubEntity $entity): MainResource |
| 49 | + { |
| 50 | + $resource = new MainResource(); |
| 51 | + $resource->name = $entity->getName(); |
| 52 | + $resource->id = $entity->getId(); |
| 53 | + $resource->subResource = new SubResource(); |
| 54 | + $resource->subResource->name = $entity->getSubEntity()->getName(); |
| 55 | + $resource->subResource->strId = $entity->getSubEntity()->getStrId(); |
| 56 | + |
| 57 | + return $resource; |
| 58 | + } |
| 59 | +} |
0 commit comments