Skip to content

Commit 0e24b3a

Browse files
committed
fix(metadata): update tests to use MappedObjectPaginator instead of ArrayPaginator
1 parent 8f4bc08 commit 0e24b3a

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\State\Pagination;
15+
16+
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
17+
18+
final class MappedObjectPaginator implements \IteratorAggregate, PaginatorInterface
19+
{
20+
public function __construct(
21+
private readonly iterable $entities,
22+
private readonly ObjectMapperInterface $mapper,
23+
private readonly string $resourceClass,
24+
private readonly float $totalItems = 0.0,
25+
private readonly float $currentPage = 1.0,
26+
private readonly float $lastPage = 1.0,
27+
private readonly float $itemsPerPage = 30.0,
28+
) {
29+
}
30+
31+
public function count(): int
32+
{
33+
return (int) $this->totalItems;
34+
}
35+
36+
public function getLastPage(): float
37+
{
38+
return $this->lastPage;
39+
}
40+
41+
public function getTotalItems(): float
42+
{
43+
return $this->totalItems;
44+
}
45+
46+
public function getCurrentPage(): float
47+
{
48+
return $this->currentPage;
49+
}
50+
51+
public function getItemsPerPage(): float
52+
{
53+
return $this->itemsPerPage;
54+
}
55+
56+
public function getIterator(): \Traversable
57+
{
58+
foreach ($this->entities as $entity) {
59+
yield $this->mapper->map($entity, $this->resourceClass);
60+
}
61+
}
62+
}

src/State/Provider/ObjectMapperProvider.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use ApiPlatform\Metadata\Operation;
1717
use ApiPlatform\Metadata\Util\CloneTrait;
18-
use ApiPlatform\State\Pagination\ArrayPaginator;
18+
use ApiPlatform\State\Pagination\MappedObjectPaginator;
1919
use ApiPlatform\State\Pagination\PaginatorInterface;
2020
use ApiPlatform\State\ProviderInterface;
2121
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
@@ -52,7 +52,15 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5252
$request?->attributes->set('mapped_data', $data);
5353

5454
if ($data instanceof PaginatorInterface) {
55-
$data = new ArrayPaginator(array_map(fn ($v) => $this->objectMapper->map($v, $operation->getClass()), iterator_to_array($data)), 0, \count($data));
55+
$data = new MappedObjectPaginator(
56+
iterator_to_array($data),
57+
$this->objectMapper,
58+
$operation->getClass(),
59+
$data->getTotalItems(),
60+
$data->getCurrentPage(),
61+
$data->getLastPage(),
62+
$data->getItemsPerPage(),
63+
);
5664
} elseif (\is_array($data)) {
5765
foreach ($data as &$v) {
5866
if (\is_object($v)) {

tests/State/Provider/ObjectMapperProviderTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Metadata\Get;
1717
use ApiPlatform\State\Pagination\ArrayPaginator;
18+
use ApiPlatform\State\Pagination\MappedObjectPaginator;
1819
use ApiPlatform\State\Provider\ObjectMapperProvider;
1920
use ApiPlatform\State\ProviderInterface;
2021
use PHPUnit\Framework\TestCase;
@@ -160,7 +161,7 @@ public function testProvideMapsPaginator(): void
160161
$provider = new ObjectMapperProvider($objectMapper, $decorated);
161162

162163
$result = $provider->provide($operation);
163-
$this->assertInstanceOf(ArrayPaginator::class, $result);
164+
$this->assertInstanceOf(MappedObjectPaginator::class, $result);
164165
$items = iterator_to_array($result);
165166
$this->assertCount(2, $items);
166167
$this->assertSame($targetResource1, $items[0]);
@@ -192,7 +193,7 @@ public function testProvideMapsEmptyPaginator(): void
192193
$provider = new ObjectMapperProvider($objectMapper, $decorated);
193194

194195
$result = $provider->provide($operation);
195-
$this->assertInstanceOf(ArrayPaginator::class, $result);
196+
$this->assertInstanceOf(MappedObjectPaginator::class, $result);
196197
$this->assertCount(0, iterator_to_array($result));
197198
}
198199
}

0 commit comments

Comments
 (0)