2012-05-22 15:23:51 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Subscriber;
|
2012-05-22 15:23:51 +02:00
|
|
|
|
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
use Knp\Component\Pager\Event\ItemsEvent;
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
|
2013-03-27 19:58:34 +01:00
|
|
|
use FOS\ElasticaBundle\Paginator\PartialResultsInterface;
|
2012-05-22 15:23:51 +02:00
|
|
|
|
|
|
|
class PaginateElasticaQuerySubscriber implements EventSubscriberInterface
|
|
|
|
{
|
|
|
|
public function items(ItemsEvent $event)
|
|
|
|
{
|
|
|
|
if ($event->target instanceof PaginatorAdapterInterface) {
|
2013-03-02 12:23:44 +01:00
|
|
|
/** @var $results PartialResultsInterface */
|
2012-05-22 15:23:51 +02:00
|
|
|
$results = $event->target->getResults($event->getOffset(), $event->getLimit());
|
|
|
|
|
|
|
|
$event->count = $results->getTotalHits();
|
|
|
|
$event->items = $results->toArray();
|
|
|
|
$facets = $results->getFacets();
|
|
|
|
if (null != $facets) {
|
|
|
|
$event->setCustomPaginationParameter('facets', $facets);
|
|
|
|
}
|
|
|
|
|
|
|
|
$event->stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'knp_pager.items' => array('items', 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|