Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Merge ae73cd6 into df5f349
Browse files Browse the repository at this point in the history
  • Loading branch information
maniolek committed Jan 2, 2015
2 parents df5f349 + ae73cd6 commit c7261a7
Show file tree
Hide file tree
Showing 8 changed files with 560 additions and 165 deletions.
66 changes: 66 additions & 0 deletions src/Paginator/Adapter/AggregateMongo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* This file is part of Vegas package
*
* @author Mateusz Aniolek <mateusz.aniolek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Vegas\Paginator\Adapter;

use Vegas\Paginator\Adapter\Mongo\AggregateCursor;

/**
* Class AggregateMongo
* @package Vegas\Paginator\Adapter
*/
class AggregateMongo extends MongoAbstract
{
/**
* Returns results for current page
*
* @return array
*/
public function getResults()
{
$skip = ($this->page-1)*$this->limit;

$cursor = $this->getCursor();
$cursor->skip($skip);

$results = array();
$i = 0;

while($cursor->valid() && $i++ < $this->limit) {

$object = new $this->modelName();
$object->writeAttributes($cursor->current());

$pseudoCursor = new \stdClass();
foreach ($object as $key => $value) {
$pseudoCursor->$key = $value;
}

$results[] = $pseudoCursor;
$cursor->skip();
}

return $results;
}

/**
* @return mixed
* @internal
*/
public function getCursor()
{
$source = $this->model->getSource();
$cursor = new AggregateCursor($this->db, $source, $this->aggregate);

return $cursor;
}

}
168 changes: 3 additions & 165 deletions src/Paginator/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,175 +11,12 @@
*/
namespace Vegas\Paginator\Adapter;

use Phalcon\Paginator\AdapterInterface;
use Vegas\Paginator\Page;

/**
* Class Mongo
* @package Vegas\Paginator\Adapter
*/
class Mongo implements AdapterInterface
class Mongo extends MongoAbstract
{
/**
* @var
* @internal
*/
private $db;

/**
* @var string
* @internal
*/
private $modelName;

/**
* @var
* @internal
*/
private $model;

/**
* @var int
* @internal
*/
private $totalPages;

/**
* @var array
* @internal
*/
private $query = array();

/**
* @var int
* @internal
*/
private $limit = 10;

/**
* @var int
* @internal
*/
private $page = 1;

/**
* @var mixed
* @internal
*/
private $sort;

/**
* Constructor
* Sets config as class properties
*
* @param $config
*/
public function __construct($config)
{
foreach ($config as $key => $value) {
$this->$key = $value;
}

$this->validate();
}

/**
* Validates model and database
*
* @throws Exception\ModelNotSetException
* @throws Exception\DbNotSetException
* @internal
*/
private function validate()
{
if (empty($this->modelName) && empty($this->model)) {
throw new Exception\ModelNotSetException();
}

if (empty($this->model)) {
$this->model = new $this->modelName();
}

if (empty($this->modelName)) {
$this->modelName = get_class($this->model);
}

if (empty($this->db)) {
$this->db = $this->model->getConnection();
}
}

/**
* {@inheritdoc}
*/
public function getPaginate()
{
$page = new Page();

$page->current = $this->page;
$page->next = $this->getNextPage();
$page->before = $this->getPreviousPage();
$page->total_pages = $this->getTotalPages();
$page->items = $this->getResults();

return $page;
}

/**
* Returns previous page number
*
* @return int|null
*/
public function getPreviousPage()
{
if ($this->page > 1) {
return ($this->page-1);
}

return null;
}

/**
* Returns next page number
*
* @return int|null
*/
public function getNextPage()
{
if ($this->page < $this->getTotalPages()) {
return ($this->page+1);
}

return null;
}

/**
* Returns number of pages
*
* @return int
*/
public function getTotalPages()
{
if (empty($this->totalPages)) {
$this->totalPages = (int)ceil($this->getCursor()->count()/$this->limit);
}

return $this->totalPages;
}

/**
* Sets current page
*
* @param int $page
* @return $this
*/
public function setCurrentPage($page)
{
$this->page = $page;

return $this;
}

/**
* Returns results for current page
*
Expand Down Expand Up @@ -212,11 +49,12 @@ public function getResults()
* @return mixed
* @internal
*/
private function getCursor()
public function getCursor()
{
$source = $this->model->getSource();
$cursor = $this->db->$source->find($this->query);

return $cursor;
}

}
97 changes: 97 additions & 0 deletions src/Paginator/Adapter/Mongo/AggregateCursor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* This file is part of Vegas package
*
* @author Mateusz Aniołek <mateusz.aniolek@amsterdam-standard.pl>
* @copyright Amsterdam Standard Sp. Z o.o.
* @homepage http://vegas-cmf.github.io
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Vegas\Paginator\Adapter\Mongo;

/**
* Class AggregateCursor
* @package Vegas\Paginator\Adapter\Mongo
*/
class AggregateCursor
{
/**
* Database connection
*
* @var
*/
private $db;

/**
* Stores model name
*
* @var string
*/
private $model;

/**
* Default constructor
*
* @param $db
* @param $model
* @param $aggregateQuery
* @param array $options
*/
public function __construct($db, $model, $aggregateQuery, $options = array())
{
$this->db = $db;
$this->model = $model;
$this->cursor = $this->db->{$this->model}->aggregateCursor($aggregateQuery, $options);
$this->cursor->rewind();
}

/**
* Method for moving cursor by specified amount of record
*
* @param int $by
* @return $this
*/
public function skip($by = 1)
{
$i = 0;
while($this->cursor->valid() && $i < $by) {
$this->cursor->next();
$i++;
}
return $this;
}

/**
* Method for total records count in collections
*
* @return int
*/
public function count()
{
return $this->db->{$this->model}->count();
}

/**
* Gets current record under the cursor
*
* @return mixed
*/
public function current()
{
return $this->cursor->current()['current'];
}

/**
* Checks if next element exists
*
* @return mixed
*/
public function valid()
{
return $this->cursor->valid();
}

}

Loading

0 comments on commit c7261a7

Please sign in to comment.