Add raw and doctrine paginator adapter implementations

This commit is contained in:
ornicar 2011-04-14 16:23:10 -07:00
parent 4ad7d9a525
commit f0db3bc1a5
3 changed files with 113 additions and 0 deletions

View file

@ -0,0 +1,54 @@
<?php
namespace FOQ\ElasticaBundle\Paginator;
use Zend\Paginator\Adapter;
use Elastica_Searchable;
use Elastica_Query;
/**
* Implements the Zend\Paginator\Adapter Interface for use with Zend\Paginator\Paginator
*
* Allows pagination of Elastica_Query. Does not map results
*/
abstract class AbstractPaginatorAdapter implements Adapter
{
/**
* @var Elastica_SearchableInterface the object to search in
*/
protected $searchable = null;
/**
* @var Elastica_Query the query to search
*/
protected $query = null;
/**
* @see PaginatorAdapterInterface::__construct
*
* @param Elastica_SearchableInterface the object to search in
* @param Elastica_Query the query to search
*/
public function __construct(Elastica_Searchable $searchable, Elastica_Query $query)
{
$this->searchable = $searchable;
$this->query = $query;
}
protected function getElasticaResults($offset, $itemCountPerPage)
{
$query = clone $this->query;
$query->setFrom($offset);
$query->setLimit($itemCountPerPage);
return $this->searchable->search($query)->getResults();
}
/**
* @see Zend\Paginator\Adapter::count
*/
public function count()
{
return $this->searchable->count($this->query);
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace FOQ\ElasticaBundle\Paginator;
use FOQ\ElasticaBundle\MapperInterface;
use Elastica_Searchable;
use Elastica_Query;
/**
* Implements the Zend\Paginator\Adapter Interface for use with Zend\Paginator\Paginator
*
* Allows pagination of Elastica_Query
*/
class DoctrinePaginatorAdapter extends AbstractPaginatorAdapter
{
protected $mapper;
/**
* @param Elastica_SearchableInterface the object to search in
* @param Elastica_Query the query to search
* @param MapperInterface the mapper for fetching the results
*/
public function __construct(Elastica_Searchable $searchable, Elastica_Query $query, MapperInterface $mapper)
{
parent::__construct($searchable, $query);
$this->mapper = $mapper;
}
/**
* @see Zend\Paginator\Adapter::getItems
*/
public function getItems($offset, $itemCountPerPage)
{
$results = $this->getElasticaResults($offset, $itemCountPerPage);
return $this->mapper->fromElasticaObjects($results);
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace FOQ\ElasticaBundle\Paginator;
/**
* Implements the Zend\Paginator\Adapter Interface for use with Zend\Paginator\Paginator
*
* Allows pagination of Elastica_Query. Does not map results
*/
class RawPaginatorAdapter extends AbstractPaginatorAdapter
{
/**
* @see Zend\Paginator\Adapter::getItems
*/
public function getItems($offset, $itemCountPerPage)
{
$results = $this->getElasticaResults($offset, $itemCountPerPage);
return array_map(function($result) { return $result->getSource(); }, $results);
}
}