You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
2.6 KiB
120 lines
2.6 KiB
<?php
|
|
/**
|
|
* @copyright NetMonsters <team@netmonsters.ru>
|
|
* @link http://netmonsters.ru
|
|
* @package Majestic
|
|
* @subpackage db
|
|
* @since 2011-11-15
|
|
*/
|
|
|
|
class MongoCommandBuilder
|
|
{
|
|
|
|
const FIND = 'Find';
|
|
|
|
const INSERT = 'Insert';
|
|
|
|
const UPDATE = 'Update';
|
|
|
|
const REMOVE = 'Remove';
|
|
|
|
static public function factory($type)
|
|
{
|
|
$class = ucfirst($type) . 'MongoCommand';
|
|
return new $class();
|
|
}
|
|
}
|
|
|
|
abstract class MongoDbCommand
|
|
{
|
|
public function execute()
|
|
{
|
|
if ($this->checkParams()) {
|
|
return $this->concreteExecute();
|
|
} else {
|
|
throw new Exception(get_called_class() . ' error. Bind all required params first.');
|
|
}
|
|
}
|
|
|
|
public function bindParam($name, $value)
|
|
{
|
|
$this->$name = $value;
|
|
return $this;
|
|
}
|
|
|
|
abstract protected function concreteExecute();
|
|
|
|
abstract protected function checkParams();
|
|
}
|
|
|
|
class FindMongoCommand extends MongoDbCommand
|
|
{
|
|
protected function concreteExecute()
|
|
{
|
|
return $this->collection->find($this->condition, $this->fields);
|
|
}
|
|
|
|
protected function checkParams()
|
|
{
|
|
if (isset($this->collection) && isset($this->condition) && isset($this->fields)) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
class InsertMongoCommand extends MongoDbCommand
|
|
{
|
|
protected function concreteExecute()
|
|
{
|
|
return $this->collection->insert($this->data, array('safe' => $this->safe));
|
|
}
|
|
|
|
protected function checkParams()
|
|
{
|
|
if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
class UpdateMongoCommand extends MongoDbCommand
|
|
{
|
|
protected function concreteExecute()
|
|
{
|
|
return $this->collection->update($this->condition, $this->data, array('upsert' => $this->upsert, 'safe' => $this->safe));
|
|
}
|
|
|
|
protected function checkParams()
|
|
{
|
|
if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
|
|
isset($this->upsert) && isset($this->safe)
|
|
) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
class RemoveMongoCommand extends MongoDbCommand
|
|
{
|
|
protected function concreteExecute()
|
|
{
|
|
return $this->collection->remove($this->condition, array('safe' => $this->safe));
|
|
}
|
|
|
|
protected function checkParams()
|
|
{
|
|
if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
|