|
| 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\Metadata\Resource\Factory; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\ApiResource; |
| 17 | +use ApiPlatform\Metadata\Mutator\OperationMutatorCollectionInterface; |
| 18 | +use ApiPlatform\Metadata\Mutator\ResourceMutatorCollectionInterface; |
| 19 | +use ApiPlatform\Metadata\Operation; |
| 20 | +use ApiPlatform\Metadata\Operations; |
| 21 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 22 | + |
| 23 | +final class MutatorResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private readonly ResourceMutatorCollectionInterface $resourceMutators, |
| 27 | + private readonly OperationMutatorCollectionInterface $operationMutators, |
| 28 | + private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + public function create(string $resourceClass): ResourceMetadataCollection |
| 33 | + { |
| 34 | + $resourceMetadataCollection = new ResourceMetadataCollection($resourceClass); |
| 35 | + if ($this->decorated) { |
| 36 | + $resourceMetadataCollection = $this->decorated->create($resourceClass); |
| 37 | + } |
| 38 | + |
| 39 | + $newMetadataCollection = new ResourceMetadataCollection($resourceClass); |
| 40 | + |
| 41 | + foreach ($resourceMetadataCollection as $resource) { |
| 42 | + $resource = $this->mutateResource($resource, $resourceClass); |
| 43 | + $operations = $this->mutateOperations($resource->getOperations() ?? new Operations()); |
| 44 | + $resource = $resource->withOperations($operations); |
| 45 | + |
| 46 | + $newMetadataCollection[] = $resource; |
| 47 | + } |
| 48 | + |
| 49 | + return $newMetadataCollection; |
| 50 | + } |
| 51 | + |
| 52 | + private function mutateResource(ApiResource $resource, string $resourceClass): ApiResource |
| 53 | + { |
| 54 | + foreach ($this->resourceMutators->get($resourceClass) as $mutator) { |
| 55 | + $resource = $mutator($resource); |
| 56 | + } |
| 57 | + |
| 58 | + return $resource; |
| 59 | + } |
| 60 | + |
| 61 | + private function mutateOperations(Operations $operations): Operations |
| 62 | + { |
| 63 | + $newOperations = new Operations(); |
| 64 | + |
| 65 | + /** @var Operation $operation */ |
| 66 | + foreach ($operations as $key => $operation) { |
| 67 | + foreach ($this->operationMutators->get($key) as $mutator) { |
| 68 | + $operation = $mutator($operation); |
| 69 | + } |
| 70 | + |
| 71 | + $newOperations->add($key, $operation); |
| 72 | + } |
| 73 | + |
| 74 | + return $newOperations; |
| 75 | + } |
| 76 | +} |
0 commit comments