This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
560 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.