Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Adapter/DoctrineMongoDBAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/*
* This file is part of the PagerBundle package.
*
* (c) Marcin Butlak <contact@maker-labs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MakerLabs\PagerBundle\Adapter;

use MakerLabs\PagerBundle\Adapter\PagerAdapterInterface;
use Doctrine\ODM\MongoDB\Query\Builder;

/**
* Pager Doctrine MongoDB adapter
*
* @author Phil A. <github@smurfy.de>
*/
class DoctrineMongoDBAdapter implements PagerAdapterInterface
{
protected $query;
protected $totalResults = null;

public function __construct(Builder $query)
{
$this->query = $query;
}

/**
* Returns the count query instance
*
* @return QueryBuilder
*/
public function getCountQuery()
{
$countQuery = clone $this->query->getQuery();
return $countQuery;
}

/**
* Returns the total number of results
*
* @return integer
*/
public function getTotalResults()
{
if (null === $this->totalResults) {
$this->totalResults = $this->getCountQuery()->count();
}
return $this->totalResults;
}

/**
* Returns the list of results
*
* @return array
*/
public function getResults($offset, $limit)
{
return $this->query->limit($limit)->skip($offset)->getQuery()->execute();
}
}