-
-
Notifications
You must be signed in to change notification settings - Fork 964
Expand file tree
/
Copy pathBarResourceProvider.php
More file actions
64 lines (52 loc) · 2.01 KB
/
BarResourceProvider.php
File metadata and controls
64 lines (52 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Tests\Fixtures\TestBundle\State\Issue6590;
use ApiPlatform\Doctrine\Orm\Paginator;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Pagination\TraversablePaginator;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6590\OrmBarResource;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6590\Bar as BarEntity;
class BarResourceProvider implements ProviderInterface
{
public function __construct(
private readonly ProviderInterface $itemProvider,
private readonly ProviderInterface $collectionProvider,
) {
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if ($operation instanceof CollectionOperationInterface) {
$entities = $this->collectionProvider->provide($operation, $uriVariables, $context);
\assert($entities instanceof Paginator);
$resources = [];
foreach ($entities as $entity) {
$resources[] = $this->getResource($entity);
}
return new TraversablePaginator(
new \ArrayIterator($resources),
$entities->getCurrentPage(),
$entities->getItemsPerPage(),
$entities->getTotalItems()
);
}
$entity = $this->itemProvider->provide($operation, $uriVariables, $context);
return $this->getResource($entity);
}
protected function getResource(BarEntity $entity): OrmBarResource
{
$resource = new OrmBarResource();
$resource->id = $entity->getId();
$resource->name = $entity->getName();
return $resource;
}
}