Skip to content

Commit

Permalink
feat: add PagerFactoryInterface for API Platform (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Apr 8, 2024
1 parent 1dbb6b7 commit 3d9288e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 0.8.1

* feat: add `PagerFactoryInterface` for API Platform

# 0.8.0

* build: spinoff encoder service definition
Expand Down
20 changes: 13 additions & 7 deletions packages/rekapager-api-platform/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
* that was distributed with this source code.
*/

use Rekalogika\Rekapager\ApiPlatform\Implementation\PageNormalizer;
use Rekalogika\Rekapager\ApiPlatform\Implementation\PagerFactory;
use Rekalogika\Rekapager\ApiPlatform\Implementation\PagerNormalizer;
use Rekalogika\Rekapager\ApiPlatform\Implementation\RekapagerExtension;
use Rekalogika\Rekapager\ApiPlatform\Implementation\RekapagerOpenApiFactoryDecorator;
use Rekalogika\Rekapager\ApiPlatform\PagerFactoryInterface;
use Rekalogika\Rekapager\Contracts\PageIdentifierEncoderLocatorInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand All @@ -31,14 +32,17 @@

$services = $containerConfigurator->services();

$services->set('rekalogika.rekapager.api_platform.open_api_factory_decorator')
$services
->set('rekalogika.rekapager.api_platform.open_api_factory_decorator')
->class(RekapagerOpenApiFactoryDecorator::class)
->decorate('api_platform.openapi.factory')
->args([
'$decorated' => service('.inner'),
]);

$services->set(PagerFactory::class)
$services
->set(PagerFactoryInterface::class)
->class(PagerFactory::class)
->args([
'$resourceMetadataFactory' => service('api_platform.metadata.resource.metadata_collection_factory'),
'$pageIdentifierEncoderLocator' => service(PageIdentifierEncoderLocatorInterface::class),
Expand All @@ -47,17 +51,19 @@
'$urlGenerationStrategy' => '%api_platform.url_generation_strategy%'
]);

$services->set('rekalogika.rekapager.api_platform.page_normalizer')
->class(PageNormalizer::class)
$services
->set('rekalogika.rekapager.api_platform.page_normalizer')
->class(PagerNormalizer::class)
->decorate('api_platform.hydra.normalizer.collection')
->args([
'$collectionNormalizer' => service('.inner'),
]);

$services->set('rekalogika.rekapager.api_platform.orm.extension')
$services
->set('rekalogika.rekapager.api_platform.orm.extension')
->class(RekapagerExtension::class)
->args([
'$pagerFactory' => service(PagerFactory::class),
'$pagerFactory' => service(PagerFactoryInterface::class),
'$pagination' => service('api_platform.pagination'),
'$enabledByDefault' => '%rekalogika.rekapager.api_platform.enable_orm_support_by_default%',
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
use Rekalogika\Contracts\Rekapager\Exception\UnexpectedValueException;
use Rekalogika\Contracts\Rekapager\PageableInterface;
use Rekalogika\Contracts\Rekapager\PageInterface;
use Rekalogika\Rekapager\ApiPlatform\PagerFactoryInterface;
use Rekalogika\Rekapager\ApiPlatform\Util\IriHelper;
use Rekalogika\Rekapager\Contracts\PageIdentifierEncoderLocatorInterface;
use Rekalogika\Rekapager\Contracts\TraversablePagerInterface;
use Rekalogika\Rekapager\Pager\Pager;
use Rekalogika\Rekapager\Pager\TraversablePager;

class PagerFactory
class PagerFactory implements PagerFactoryInterface
{
public function __construct(
private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory,
Expand All @@ -38,14 +39,6 @@ public function __construct(
) {
}

/**
* @template TKey of array-key
* @template T
* @template TIdentifier of object
* @param PageableInterface<TKey,T,TIdentifier> $pageable
* @param array<array-key,mixed> $context
* @return PageInterface<TKey,T,TIdentifier>
*/
public function getPage(
PageableInterface $pageable,
?Operation $operation = null,
Expand Down Expand Up @@ -77,14 +70,6 @@ public function getPage(
return $page;
}

/**
* @template TKey of array-key
* @template T
* @template TIdentifier of object
* @param PageableInterface<TKey,T,TIdentifier> $pageable
* @param array<array-key,mixed> $context
* @return TraversablePagerInterface<TKey,T,TIdentifier>
*/
public function createPager(
PageableInterface $pageable,
?Operation $operation = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* @see PartialCollectionViewNormalizer
*/
class PageNormalizer implements NormalizerInterface, NormalizerAwareInterface
class PagerNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
public function __construct(
private readonly NormalizerInterface $collectionNormalizer,
Expand Down
50 changes: 50 additions & 0 deletions packages/rekapager-api-platform/src/PagerFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/rekapager package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Rekapager\ApiPlatform;

use ApiPlatform\Metadata\Operation;
use Rekalogika\Contracts\Rekapager\PageableInterface;
use Rekalogika\Contracts\Rekapager\PageInterface;
use Rekalogika\Rekapager\Contracts\TraversablePagerInterface;

interface PagerFactoryInterface
{
/**
* @template TKey of array-key
* @template T
* @template TIdentifier of object
* @param PageableInterface<TKey,T,TIdentifier> $pageable
* @param array<array-key,mixed> $context
* @return PageInterface<TKey,T,TIdentifier>
*/
public function getPage(
PageableInterface $pageable,
?Operation $operation = null,
array $context = []
): PageInterface;

/**
* @template TKey of array-key
* @template T
* @template TIdentifier of object
* @param PageableInterface<TKey,T,TIdentifier> $pageable
* @param array<array-key,mixed> $context
* @return TraversablePagerInterface<TKey,T,TIdentifier>
*/
public function createPager(
PageableInterface $pageable,
?Operation $operation = null,
array $context = [],
): TraversablePagerInterface;
}
6 changes: 3 additions & 3 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@
</PossiblyNullReference>
<InternalClass>
<errorLevel type="suppress">
<file name="packages/rekapager-api-platform/src/Implementation/PageNormalizer.php" />
<file name="packages/rekapager-api-platform/src/Implementation/PagerNormalizer.php" />
</errorLevel>
</InternalClass>
<InternalMethod>
<errorLevel type="suppress">
<file name="packages/rekapager-api-platform/src/Implementation/PageNormalizer.php" />
<file name="packages/rekapager-api-platform/src/Implementation/PagerNormalizer.php" />
</errorLevel>
</InternalMethod>
<TooManyArguments>
<errorLevel type="suppress">
<file name="packages/rekapager-api-platform/src/Implementation/PageNormalizer.php" />
<file name="packages/rekapager-api-platform/src/Implementation/PagerNormalizer.php" />
</errorLevel>
</TooManyArguments>
</issueHandlers>
Expand Down
4 changes: 2 additions & 2 deletions tests/src/App/ApiState/PostProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
use ApiPlatform\State\ProviderInterface;
use Rekalogika\Rekapager\ApiPlatform\Implementation\PagerFactory;
use Rekalogika\Rekapager\ApiPlatform\PagerFactoryInterface;
use Rekalogika\Rekapager\Doctrine\Collections\SelectableAdapter;
use Rekalogika\Rekapager\Keyset\KeysetPageable;
use Rekalogika\Rekapager\Tests\App\Entity\Post;
Expand All @@ -29,7 +29,7 @@ class PostProvider implements ProviderInterface
{
public function __construct(
private PostRepository $postRepository,
private PagerFactory $pagerFactory,
private PagerFactoryInterface $pagerFactory,
) {
}

Expand Down

0 comments on commit 3d9288e

Please sign in to comment.