Last active
January 9, 2017 11:11
-
-
Save dunglas/4a4114455b33b0c009ba to your computer and use it in GitHub Desktop.
FOSElastica paginator for API Platform v1 Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\ApiPlatform\FOSElastica; | |
use Dunglas\ApiBundle\Api\ResourceInterface; | |
use Dunglas\ApiBundle\Model\DataProviderInterface; | |
use Elastica\Query; | |
use FOS\ElasticaBundle\Finder\PaginatedFinderInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
/** | |
* Data provider to use FOSElasticaBundle with DunglasApiBundle. | |
* | |
* @author Kévin Dunglas <dunglas@gmail.com> | |
*/ | |
class DataProvider implements DataProviderInterface | |
{ | |
private $finder; | |
/** | |
* @param PaginatedFinderInterface $finder | |
*/ | |
public function __construct(PaginatedFinderInterface $finder) | |
{ | |
$this->finder = $finder; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getItem(ResourceInterface $resource, $id, $fetchData = false) | |
{ | |
// Always fallback to the Doctrine provider for items | |
return null; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCollection(ResourceInterface $resource, Request $request) | |
{ | |
$query = new Query(); | |
// update the query here with your custom parameters here | |
return new Paginator( | |
$finder->createPaginatorAdapter($query), | |
(int) $request->query->get('page', 1), // page number | |
30 // items per page | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function supports(ResourceInterface $resource) | |
{ | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\ApiPlatform\FOSElastica; | |
use Dunglas\ApiBundle\Model\PaginatorInterface; | |
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface; | |
/** | |
* Allows to use FOSElasticaBundle paginators with DunglasApiBundle. | |
* | |
* @author Kévin Dunglas <dunglas@gmail.com> | |
*/ | |
class Paginator implements \IteratorAggregate, PaginatorInterface | |
{ | |
private $currentPage; | |
private $lastPage; | |
private $itemsPerPage; | |
private $totalItems; | |
private $count; | |
private $results; | |
/** | |
* @param PaginatorAdapterInterface $paginatorAdapter | |
* @param int $currentPage | |
* @param int $itemsPerPage | |
*/ | |
public function __construct(PaginatorAdapterInterface $paginatorAdapter, $currentPage, $itemsPerPage) | |
{ | |
$this->currentPage = (float) $currentPage; | |
$this->totalItems = (float) $paginatorAdapter->getTotalHits(); | |
$this->lastPage = ceil($this->totalItems / $itemsPerPage) ?: 1.; | |
$this->itemsPerPage = $itemsPerPage; | |
$results = $paginatorAdapter->getResults(($currentPage - 1) * $itemsPerPage, $itemsPerPage); | |
$this->count = (float) $results->getTotalHits(); | |
$this->results = new \ArrayObject($results->toArray()); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getIterator() | |
{ | |
return $this->results->getIterator(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCurrentPage() | |
{ | |
return $this->currentPage; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getLastPage() | |
{ | |
return $this->lastPage; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getItemsPerPage() | |
{ | |
return $this->itemsPerPage; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getTotalItems() | |
{ | |
return $this->totalItems; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function count() | |
{ | |
return $this->count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment