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

Sorting records based on mongo's aggregation; Tests #36

Merged
merged 6 commits into from
Jan 8, 2015
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ services:

before_install:
- mysql -e "CREATE DATABASE IF NOT EXISTS vegas_test;" -uroot
- sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
- echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
- sudo apt-get update
- sudo apt-get install mongodb-org-server

before_script:
- phpenv config-add travis/php.ini
- git clone -q --depth=1 https://github.com/phalcon/cphalcon.git -b master
- (cd cphalcon/ext; export CFLAGS="-g3 -O1 -fno-delete-null-pointer-checks -Wall"; phpize && ./configure --enable-phalcon && make -j4 && sudo make install && phpenv config-add ../unit-tests/ci/phalcon.ini)
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev

script:
- cp tests/config.sample.php tests/config.php
- mkdir -p tests/fixtures/cache
Expand Down
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;
}

}
105 changes: 105 additions & 0 deletions src/Paginator/Adapter/Mongo/AggregateCursor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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;


/**
* Mongo Aggregated Cursor
*
* @var string
*/
private $cursor;

/**
* 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