Skip to content

Commit

Permalink
Decouple PagerfantaUtils from deprecated DataTransformerInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Jul 29, 2024
1 parent 2a2c2d1 commit acf6034
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/Paginator/Util/PagerfantaUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Laminas\Stdlib\ArrayUtils;
use Pagerfanta\Pagerfanta;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;

use function array_map;
use function count;
Expand All @@ -19,16 +18,17 @@ final class PagerfantaUtils
{
/**
* @param Pagerfanta<T> $paginator
* @param null|callable(T): mixed $serializer
*/
public static function serializePaginator(
Pagerfanta $paginator,
?DataTransformerInterface $transformer = null,
?callable $serializer = null,
string $dataProp = 'data',
): array {
$currentPageItems = ArrayUtils::iteratorToArray($paginator->getCurrentPageResults());

return [
$dataProp => self::serializeItems($currentPageItems, $transformer),
$dataProp => self::serializeItems($currentPageItems, $serializer),
'pagination' => [
'currentPage' => $paginator->getCurrentPage(),
'pagesCount' => $paginator->getNbPages(),
Expand All @@ -39,9 +39,13 @@ public static function serializePaginator(
];
}

private static function serializeItems(array $items, ?DataTransformerInterface $transformer = null): array
/**
* @param T[] $items
* @param null|callable(T): array $serializer
*/
private static function serializeItems(array $items, ?callable $serializer = null): array
{
return $transformer === null ? $items : array_map([$transformer, 'transform'], $items);
return $serializer === null ? $items : array_map($serializer, $items);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Paginator/Util/PagerfantaUtilsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ private function serializePaginator(
?DataTransformerInterface $transformer = null,
string $dataProp = 'data',
): array {
return PagerfantaUtils::serializePaginator($paginator, $transformer, $dataProp);
$serializer = $transformer !== null ? fn ($value) => $transformer->transform($value) : null;
return PagerfantaUtils::serializePaginator($paginator, $serializer, $dataProp);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion test/Paginator/Util/PagerfantaUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function providePaginatorAdapters(): iterable
#[Test, DataProvider('provideDataProps')]
public function paginatorIsSerializedWithExpectedDataProp(string $prop): void
{
$result = PagerfantaUtils::serializePaginator(new Pagerfanta(new ArrayAdapter([])), null, $prop);
$result = PagerfantaUtils::serializePaginator(new Pagerfanta(new ArrayAdapter([])), dataProp: $prop);

self::assertArrayNotHasKey('data', $result);
self::assertArrayHasKey($prop, $result);
Expand All @@ -103,6 +103,17 @@ public static function provideDataProps(): iterable
yield 'something' => ['something'];
}

#[Test]
public function paginatorIsSerializedWithProvidedCallback(): void
{
['data' => $data] = PagerfantaUtils::serializePaginator(
new Pagerfanta(new ArrayAdapter(range(1, 10))),
static fn (int $value) => $value * 2,
);

self::assertEquals([2, 4, 6, 8, 10, 12, 14, 16, 18, 20], $data);
}

/**
* @param Pagerfanta<T> $paginator
*/
Expand Down

0 comments on commit acf6034

Please sign in to comment.