protected static function _getResultset($params, CollectionInterface $collection, $connection, $unique)
{
// @codingStandardsIgnoreEnd
/**
* Check if "class" clause was defined
*/
if (isset($params['class'])) {
$classname = $params['class'];
$base = new $classname();
if (!$base instanceof CollectionInterface || $base instanceof Document) {
throw new Exception("Object of class '" . $classname . "' must be an implementation of \n Phalcon\\Mvc\\CollectionInterface or an instance of Phalcon\\Mvc\\Collection\\Document");
}
} else {
$base = $collection;
}
$source = $collection->getSource();
if (empty($source)) {
throw new Exception("Method getSource() returns empty string");
}
/**
* @var \Phalcon\Db\Adapter\MongoDB\Collection $mongoCollection
*/
$mongoCollection = $connection->selectCollection($source);
if (!is_object($mongoCollection)) {
throw new Exception("Couldn't select mongo collection");
}
$conditions = [];
if (isset($params[0]) || isset($params['conditions'])) {
$conditions = isset($params[0]) ? $params[0] : $params['conditions'];
}
/**
* Convert the string to an array
*/
if (!is_array($conditions)) {
throw new Exception("Find parameters must be an array");
}
$options = [];
/**
* Check if a "limit" clause was defined
*/
if (isset($params['limit'])) {
$limit = $params['limit'];
$options['limit'] = (int) $limit;
if ($unique) {
$options['limit'] = 1;
}
}
/**
* Check if a "sort" clause was defined
*/
if (isset($params['sort'])) {
$sort = $params["sort"];
$options['sort'] = $sort;
}
/**
* Check if a "skip" clause was defined
*/
if (isset($params['skip'])) {
$skip = $params["skip"];
$options['skip'] = (int) $skip;
}
if (isset($params['fields']) && is_array($params['fields']) && !empty($params['fields'])) {
$options['projection'] = [];
foreach ($params['fields'] as $key => $show) {
$options['projection'][$key] = $show;
}
}
/**
* Perform the find
*/
$cursor = $mongoCollection->find($conditions, $options);
$cursor->setTypeMap(['root' => get_called_class(), 'document' => 'object']);
if (true === $unique) {
/**
* Looking for only the first result.
*/
return current($cursor->toArray());
}
/**
* Requesting a complete resultset
*/
$collections = [];
foreach ($cursor as $document) {
/**
* Assign the values to the base object
*/
$collections[] = $document;
}
return $collections;
}