Created
August 25, 2017 14:01
-
-
Save lpj145/2e27f2dba60b3a9ad98c05707e5f039e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?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