|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 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\RuntimeResource\Odm; |
| 15 | + |
| 16 | +use ApiPlatform\Doctrine\Odm\State\Options; |
| 17 | +use ApiPlatform\Metadata\CollectionOperationInterface; |
| 18 | +use ApiPlatform\Metadata\Operation; |
| 19 | +use ApiPlatform\State\Pagination\PaginatorInterface; |
| 20 | +use ApiPlatform\State\Pagination\TraversablePaginator; |
| 21 | +use ApiPlatform\State\ProviderInterface; |
| 22 | +use ApiPlatform\Tests\Fixtures\TestBundle\Document\RuntimeResource\Document; |
| 23 | +use ApiPlatform\Tests\Fixtures\TestBundle\RuntimeResource\RuntimeMapper; |
| 24 | +use ApiPlatform\Tests\Fixtures\TestBundle\RuntimeResource\RuntimeObject; |
| 25 | +use ApiPlatform\Tests\Fixtures\TestBundle\RuntimeResource\RuntimeResources; |
| 26 | +use Doctrine\ODM\MongoDB\Aggregation\Builder; |
| 27 | +use Doctrine\ODM\MongoDB\DocumentManager; |
| 28 | +use Doctrine\Persistence\ManagerRegistry; |
| 29 | +use MongoDB\BSON\UTCDateTime; |
| 30 | + |
| 31 | +final class DocumentProvider implements ProviderInterface |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + private readonly ProviderInterface $itemProvider, |
| 35 | + private readonly ProviderInterface $collectionProvider, |
| 36 | + private readonly ManagerRegistry $registry, |
| 37 | + private readonly RuntimeMapper $mapper, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null |
| 42 | + { |
| 43 | + $name = RuntimeResources::nameOf((string) $operation->getClass()); |
| 44 | + if (null === $name) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + $operation = $operation->withStateOptions(new Options(documentClass: Document::class, handleLinks: [self::class, 'handleLinks'])); |
| 49 | + if (!$operation instanceof CollectionOperationInterface) { |
| 50 | + $document = $this->itemProvider->provide($operation, $uriVariables, $context); |
| 51 | + |
| 52 | + return $document instanceof Document ? $this->find($name, (int) $document->getId()) : null; |
| 53 | + } |
| 54 | + |
| 55 | + $documents = $this->collectionProvider->provide($operation, $uriVariables, $context); |
| 56 | + $objects = $this->mapper->hydrate($name, array_map(static fn (Document $document): int => (int) $document->getId(), iterator_to_array($documents, false)), $this->load(...)); |
| 57 | + |
| 58 | + return $documents instanceof PaginatorInterface ? new TraversablePaginator(new \ArrayIterator($objects), $documents->getCurrentPage(), $documents->getItemsPerPage(), $documents->getTotalItems()) : $objects; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param array<string, mixed> $uriVariables |
| 63 | + * @param array<string, mixed> $context |
| 64 | + */ |
| 65 | + public static function handleLinks(Builder $aggregationBuilder, array $uriVariables, array $context): void |
| 66 | + { |
| 67 | + $aggregationBuilder->match()->field('resource')->equals(RuntimeResources::nameOf((string) $context['operation']->getClass())); |
| 68 | + if (isset($uriVariables['id'])) { |
| 69 | + $aggregationBuilder->match()->field('id')->equals((int) $uriVariables['id']); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function find(string $name, int $id): ?RuntimeObject |
| 74 | + { |
| 75 | + return $this->mapper->hydrate($name, [$id], $this->load(...))[0] ?? null; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param list<int> $ids |
| 80 | + * |
| 81 | + * @return array<int, array<string, mixed>> |
| 82 | + */ |
| 83 | + private function load(string $name, array $ids): array |
| 84 | + { |
| 85 | + $manager = $this->registry->getManagerForClass(Document::class); |
| 86 | + \assert($manager instanceof DocumentManager); |
| 87 | + |
| 88 | + $values = []; |
| 89 | + foreach ($manager->getDocumentCollection(Document::class)->find(['resource' => $name, '_id' => ['$in' => $ids]], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]) as $row) { |
| 90 | + $values[$row['_id']] = array_map(static fn (mixed $value): mixed => $value instanceof UTCDateTime ? \DateTimeImmutable::createFromInterface($value->toDateTime()) : $value, $row['values'] ?? []); |
| 91 | + } |
| 92 | + |
| 93 | + return $values; |
| 94 | + } |
| 95 | +} |
0 commit comments