Created
August 26, 2017 02:36
-
-
Save lpj145/daec917c3fac343e58ea91b50a29bbe7 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace FiremonPHP\Database\Operations; | |
class WriteOperation | |
{ | |
private $_data = []; | |
/** | |
* @var \MongoDB\Driver\BulkWrite[] | |
*/ | |
private $_bulk = []; | |
private $_options = []; | |
private $_concerns = []; | |
private $_validations = []; | |
private $_errors = []; | |
private $_indexes = []; | |
public function __construct(array $data, array $indexes, array $options = []) | |
{ | |
$this->_data = $data; | |
$this->_indexes = $indexes; | |
$this->_options = $options; | |
} | |
/** | |
* @return \Generator | |
*/ | |
public function getBulkWrite() | |
{ | |
yield $this->_bulk; | |
} | |
private function iterateData(array $data) | |
{ | |
$firstKey = key($data); | |
if ($this->isFilterIndex($firstKey, $data)) { | |
return; | |
} | |
//Todo Aqui é o bode! | |
if (count($data[$firstKey]) > 0) { | |
$this->insertMany($data, $firstKey); | |
} | |
$this->insert($data, $firstKey); | |
unset($data[$firstKey]); | |
if (count($data) > 0) { | |
$this->iterateData($data); | |
} | |
} | |
/** | |
* @param $keyIndex | |
* @param int $barPosition | |
* @return bool|string | |
*/ | |
private function getIndex($keyIndex, int $barPosition) | |
{ | |
return substr($keyIndex, $barPosition); | |
} | |
/** | |
* @param $keyIndex | |
* @param int $barPosition | |
* @return bool|string | |
*/ | |
private function getNamespace($keyIndex, int $barPosition) | |
{ | |
return substr($keyIndex, 0, $barPosition); | |
} | |
private function insertMany(array $data) | |
{ | |
return true; | |
} | |
/** | |
* If '/' founded on keyArray get id and namespace and execute setFilteredData | |
* @param $keyIndex | |
* @return bool | |
*/ | |
private function isFilterIndex($keyIndex, $data) | |
{ | |
$barPosition = strpos($keyIndex, '/'); | |
if ($barPosition > 0) { | |
$id = $this->getIndex($keyIndex, $barPosition); | |
$namespace = $this->getNamespace($keyIndex, $barPosition); | |
$this->setFilteredData($id, $namespace, $data[$keyIndex]); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* If id and namespace not false execute | |
* If data is null -> go delete action | |
* else go -> update action | |
* @param $id | |
* @param $namespace | |
* @param $data | |
*/ | |
private function setFilteredData($id, $namespace, $data) | |
{ | |
if ($id === false || $namespace === false) { | |
$this->_errors[] = 'Id or Namespace not found on array data!'; | |
return; | |
} | |
if ($data === null) { | |
$this->delete($id, $namespace); | |
return; | |
} | |
$this->update($id, $namespace, $data); | |
} | |
/** | |
* Set insert action on namespace bulk | |
* @param array $data | |
* @param $namespace | |
*/ | |
private function insert(array $data, $namespace) | |
{ | |
//Todo: implement validations methods! | |
$this->_bulk[$namespace]->insert($data); | |
} | |
/** | |
* Set update action on namespace bulk | |
* @param $key | |
* @param $namespace | |
* @param $data | |
*/ | |
private function update($key, $namespace, $data) | |
{ | |
//Todo: implement validations methods! | |
$index = $this->_indexes[$namespace] ?? '_id'; | |
$this->_bulk[$namespace]->update([$index => $key], ['$set' => $data], $this->_options); | |
} | |
/** | |
* Set delete action on namespace bulk | |
* If not have index to namespace, set default mongodb index '_id' | |
* @param $key | |
* @param $namespace | |
*/ | |
private function delete($key, $namespace) | |
{ | |
$index = $this->_indexes[$namespace] ?? '_id'; | |
$this->_bulk[$namespace]->delete([$index => $key], ['limit' => 1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment