diff --git a/composer.json b/composer.json index 7f1b9703..4f8004cd 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,9 @@ "symfony/routing": "~2.0 || ~3.0", "symfony/dependency-injection": "~2.0 || ~3.0", "pagerfanta/pagerfanta": "~1.0", - "twig/twig": "~1.12" + "twig/twig": "~1.12", + "symfony/event-dispatcher": "~2.5", + "knplabs/knp-components": "~1.2" }, "suggest": { "symfony/yaml": "To use yaml based configuration.", diff --git a/src/Hateoas/Representation/Factory/KnpPaginatorFactory.php b/src/Hateoas/Representation/Factory/KnpPaginatorFactory.php new file mode 100644 index 00000000..6a25eae7 --- /dev/null +++ b/src/Hateoas/Representation/Factory/KnpPaginatorFactory.php @@ -0,0 +1,75 @@ +pageParameterName = $pageParameterName; + $this->limitParameterName = $limitParameterName; + } + + /** + * @param SlidingPagination $slidingPagination + * @param Route $route + * @param null $inline + * @return PaginatedRepresentation + */ + public function createRepresentation(SlidingPagination $slidingPagination, Route $route, $inline = null) + { + if (null === $inline) { + $inline = new CollectionRepresentation($slidingPagination->getItems()); + } + + return new PaginatedRepresentation( + $inline, + $route->getName(), + $route->getParameters(), + $slidingPagination->getCurrentPageNumber(), + $slidingPagination->getItemNumberPerPage(), + $slidingPagination->getPaginationData()['pageCount'], + $this->getPageParameterName(), + $this->getLimitParameterName(), + $route->isAbsolute(), + $slidingPagination->getTotalItemCount() + ); + + } + + /** + * @return string + */ + public function getPageParameterName() + { + return $this->pageParameterName; + } + + /** + * @return string + */ + public function getLimitParameterName() + { + return $this->limitParameterName; + } + +} \ No newline at end of file diff --git a/tests/Hateoas/Tests/Representation/Factory/KnpPaginatorFactoryTest.php b/tests/Hateoas/Tests/Representation/Factory/KnpPaginatorFactoryTest.php new file mode 100644 index 00000000..63704f47 --- /dev/null +++ b/tests/Hateoas/Tests/Representation/Factory/KnpPaginatorFactoryTest.php @@ -0,0 +1,141 @@ +prophesize('Knp\Component\Pager\Pagination\SlidingPagination'); + $pagerProhecy->getItems()->willReturn($results); + $pagerProhecy->getCurrentPageNumber()->willReturn(1); + $pagerProhecy->getItemNumberPerPage()->willReturn(20); + $pagerProhecy->getPaginationData()->willReturn(array('pageCount' => 3)); + $pagerProhecy->getTotalItemCount()->willReturn(50); + + $factory = new KnpPaginatorFactory('page', 'limit'); + + $represention1 = $factory->createRepresentation( + $pagerProhecy->reveal(), + new Route( + 'users', + array( + 'query' => 'hateoas' + ) + ), + null + ); + $represention2 = $factory->createRepresentation( + $pagerProhecy->reveal(), + new Route( + 'users', + array( + 'query' => 'hateoas' + ) + ), + null + ); + $this->assertEquals(new CollectionRepresentation($results), $represention1->getInline()); + $this->assertEquals(new CollectionRepresentation($results), $represention2->getInline()); + + foreach(array($represention1, $represention2) as $representation){ + $this->assertInstanceOf('Hateoas\Representation\PaginatedRepresentation', $representation); + $this->assertSame(1, $representation->getPage()); + $this->assertSame(50, $representation->getTotal()); + $this->assertSame(20, $representation->getLimit()); + $this->assertSame(3, $representation->getPages()); + $this->assertSame('limit', $representation->getLimitParameterName()); + $this->assertSame('page', $representation->getPageParameterName()); + $this->assertSame( + [ + 'query' => 'hateoas', + 'page' => 1, + 'limit' => 20 + ], + $representation->getParameters() + ); + } + } + + public function testSerialize(){ + + $factory = new KnpPaginatorFactory(); + + $paginator = new Paginator; + + $paginate = $paginator->paginate( + array( + 'mela', + 'meli', + 'melo' + ), 1, 10 + ); + + $collection = $factory->createRepresentation($paginate, new Route('my_route')); + + $this->assertJsonStringEqualsJsonString( + <<json($this->hateoas->serialize($collection, 'json')) + + ); + + + $this->assertXmlStringEqualsXmlString( + << + + + + + + + + + + + +XML + , + $this->hateoas->serialize($collection, 'xml') + ); + } +} + \ No newline at end of file