Skip to content

Instantly share code, notes, and snippets.

@lpj145
Created August 25, 2017 14:01
Show Gist options
  • Save lpj145/2e27f2dba60b3a9ad98c05707e5f039e to your computer and use it in GitHub Desktop.
Save lpj145/2e27f2dba60b3a9ad98c05707e5f039e to your computer and use it in GitHub Desktop.
<?php
namespace FiremonPHP\Database\Query;
class ReadQuery implements QueryInterface
{
/**
* @var \FiremonPHP\Database\Connection\ConnectionInterface
*/
private $_connection;
private $_options = [];
private $_data;
private $_conditions = [];
public function __construct(\FiremonPHP\Database\Connection\ConnectionInterface $connection, $data)
{
$this->_data = $data;
$this->_connection = $connection;
}
/**
* Set fields of documents
* @param array $fieldsName
*/
public function fields(array $fieldsName)
{
array_map(function($value){
$this->_options['projection'][$value] = '1';
}, $fieldsName);
return $this;
}
public function ascBy(string $fieldName)
{
$this->_options['sort'][$fieldName] = '1';
return $this;
}
public function descBy(string $fieldName)
{
$this->_options['sort'][$fieldName] = '-1';
return $this;
}
public function limit(int $limitNumber)
{
$this->_options['limit'] = $limitNumber;
return $this;
}
public function skip(int $skipNumber)
{
$this->_options['skip'] = $skipNumber;
return $this;
}
public function notEqual(string $field, $value)
{
$this->_conditions[$field]['$ne'] = $value;
return $this;
}
public function equalTo(string $field, $value)
{
$this->_conditions[$field]['$eq'] = $value;
return $this;
}
public function startAt(string $field, $value)
{
$this->_conditions[$field]['$gte'] = $value;
return $this;
}
public function endAt(string $field, $value)
{
$this->_conditions[$field]['$lt'] = $value;
return $this;
}
public function execute()
{
$query = new \MongoDB\Driver\Query($this->_conditions, $this->_options);
return $this->_connection->executeQuery('read', $this->_data, $query);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment