From e7227ea21eacb33a2156761f1f94ecbcb0982b39 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Mon, 8 Apr 2024 23:37:39 +0700 Subject: [PATCH] feat: add `PagerFactoryInterface` for API Platform --- CHANGELOG.md | 4 ++ .../config/services.php | 20 +++++--- .../src/Implementation/PagerFactory.php | 19 +------ ...PageNormalizer.php => PagerNormalizer.php} | 2 +- .../src/PagerFactoryInterface.php | 50 +++++++++++++++++++ psalm.xml | 6 +-- tests/src/App/ApiState/PostProvider.php | 4 +- 7 files changed, 75 insertions(+), 30 deletions(-) rename packages/rekapager-api-platform/src/Implementation/{PageNormalizer.php => PagerNormalizer.php} (97%) create mode 100644 packages/rekapager-api-platform/src/PagerFactoryInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f3c26a..e374391 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +# 0.8.1 + +* feat: add `PagerFactoryInterface` for API Platform + # 0.8.0 * build: spinoff encoder service definition diff --git a/packages/rekapager-api-platform/config/services.php b/packages/rekapager-api-platform/config/services.php index f9d672d..6703210 100644 --- a/packages/rekapager-api-platform/config/services.php +++ b/packages/rekapager-api-platform/config/services.php @@ -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; @@ -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), @@ -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%', ]) diff --git a/packages/rekapager-api-platform/src/Implementation/PagerFactory.php b/packages/rekapager-api-platform/src/Implementation/PagerFactory.php index 18ee889..5e4cbd0 100644 --- a/packages/rekapager-api-platform/src/Implementation/PagerFactory.php +++ b/packages/rekapager-api-platform/src/Implementation/PagerFactory.php @@ -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, @@ -38,14 +39,6 @@ public function __construct( ) { } - /** - * @template TKey of array-key - * @template T - * @template TIdentifier of object - * @param PageableInterface $pageable - * @param array $context - * @return PageInterface - */ public function getPage( PageableInterface $pageable, ?Operation $operation = null, @@ -77,14 +70,6 @@ public function getPage( return $page; } - /** - * @template TKey of array-key - * @template T - * @template TIdentifier of object - * @param PageableInterface $pageable - * @param array $context - * @return TraversablePagerInterface - */ public function createPager( PageableInterface $pageable, ?Operation $operation = null, diff --git a/packages/rekapager-api-platform/src/Implementation/PageNormalizer.php b/packages/rekapager-api-platform/src/Implementation/PagerNormalizer.php similarity index 97% rename from packages/rekapager-api-platform/src/Implementation/PageNormalizer.php rename to packages/rekapager-api-platform/src/Implementation/PagerNormalizer.php index b8c1fea..e37584a 100644 --- a/packages/rekapager-api-platform/src/Implementation/PageNormalizer.php +++ b/packages/rekapager-api-platform/src/Implementation/PagerNormalizer.php @@ -21,7 +21,7 @@ /** * @see PartialCollectionViewNormalizer */ -class PageNormalizer implements NormalizerInterface, NormalizerAwareInterface +class PagerNormalizer implements NormalizerInterface, NormalizerAwareInterface { public function __construct( private readonly NormalizerInterface $collectionNormalizer, diff --git a/packages/rekapager-api-platform/src/PagerFactoryInterface.php b/packages/rekapager-api-platform/src/PagerFactoryInterface.php new file mode 100644 index 0000000..2b3065a --- /dev/null +++ b/packages/rekapager-api-platform/src/PagerFactoryInterface.php @@ -0,0 +1,50 @@ + + * + * 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 $pageable + * @param array $context + * @return PageInterface + */ + public function getPage( + PageableInterface $pageable, + ?Operation $operation = null, + array $context = [] + ): PageInterface; + + /** + * @template TKey of array-key + * @template T + * @template TIdentifier of object + * @param PageableInterface $pageable + * @param array $context + * @return TraversablePagerInterface + */ + public function createPager( + PageableInterface $pageable, + ?Operation $operation = null, + array $context = [], + ): TraversablePagerInterface; +} diff --git a/psalm.xml b/psalm.xml index ce00552..14b4463 100644 --- a/psalm.xml +++ b/psalm.xml @@ -71,17 +71,17 @@ - + - + - + diff --git a/tests/src/App/ApiState/PostProvider.php b/tests/src/App/ApiState/PostProvider.php index 758b87b..060e652 100644 --- a/tests/src/App/ApiState/PostProvider.php +++ b/tests/src/App/ApiState/PostProvider.php @@ -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; @@ -29,7 +29,7 @@ class PostProvider implements ProviderInterface { public function __construct( private PostRepository $postRepository, - private PagerFactory $pagerFactory, + private PagerFactoryInterface $pagerFactory, ) { }