Skip to content

Commit

Permalink
Merge pull request #2 from mrsuh/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mrsuh authored Jul 3, 2017
2 parents 0244115 + c265bb0 commit 8405792
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Note extends ODM\Document\Document
}

/* Initialize connection */
$dbal = new ODM\DBAL('127.0.0.1', '27017', 'test2');
$dbal = new ODM\DBAL('127.0.0.1', '27017', 'test');
$factory = new ODM\DocumentMapper\DataMapperFactory($dbal);

/* Initialize DataMapper for your Document */
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "mongo odm",
"type": "library",
"require": {
"mongodb/mongodb": "^1.1"
"mongodb/mongodb": "^1.1",
"php": "7.*"
},
"autoload": {
"psr-4": { "ODM\\": "src/" }
Expand Down
68 changes: 37 additions & 31 deletions src/DBAL.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,85 @@ class DBAL
{
private $db;

public function __construct($host, $port, $db_name)
/**
* DBAL constructor.
* @param string $host
* @param string $port
* @param string $db_name
*/
public function __construct(string $host, string $port, string $db_name)
{
$this->db = (new Client("mongodb://$host:$port"))->$db_name;
}

/**
* @param $table_name
* @param array $data
* @return mixed
* @param string $table_name
* @param array $data
* @return \MongoDB\InsertOneResult
*/
public function insert($table_name, array $data)
public function insert(string $table_name, array $data)
{
return $this->db->$table_name->insertOne($data);
}

/**
* @param $table_name
* @param array $data
* @return mixed
* @param string $table_name
* @param array $data
* @return \MongoDB\InsertManyResult
*/
public function insertMany($table_name, array $data)
public function insertMany(string $table_name, array $data)
{
return $this->db->$table_name->insertMany($data);
}

/**
* @param $table_name
* @param array $filter
* @param array $data
* @return mixed
* @param string $table_name
* @param array $filter
* @param array $data
* @return \MongoDB\UpdateResult
*/
public function update($table_name, array $filter, array $data)
public function update(string $table_name, array $filter, array $data)
{
return $this->db->$table_name->updateOne($filter, ['$set' => $data]);
}

/**
* @param $table_name
* @param array $filter
* @return mixed
* @param string $table_name
* @param array $filter
* @return \MongoDB\DeleteResult
*/
public function delete($table_name, array $filter)
public function delete(string $table_name, array $filter)
{
return $this->db->$table_name->deleteOne($filter);
}

/**
* @param $table_name
* @param array $filter
* @param array $options
* @return mixed
* @param string $table_name
* @param array $filter
* @param array $options
* @return \MongoDB\Driver\Cursor
*/
public function find($table_name, array $filter, array $options = [])
public function find(string $table_name, array $filter, array $options = [])
{
return $this->db->$table_name->find($filter, $options);
}

/**
* @param $table_name
* @param array $filter
* @param array $options
* @return mixed
* @param string $table_name
* @param array $filter
* @param array $options
* @return array|null|object
*/
public function findOne($table_name, array $filter, array $options = [])
public function findOne(string $table_name, array $filter, array $options = [])
{
return $this->db->$table_name->findOne($filter, $options);
}

/**
* @param $table_name
* @return mixed
* @param string $table_name
* @return array|object
*/
public function drop($table_name)
public function drop(string $table_name)
{
return $this->db->$table_name->drop();
}
Expand Down
7 changes: 3 additions & 4 deletions src/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@

class Document
{
private $id;
private $id;

/**
* @return mixed
*/
public function getId()
{
return $this->id;
return (string)$this->id;
}

/**
* @param $id
* @return $this
*/
public function setId($id)
public function setId(string $id)
{
$this->id = $id;

return $this;
}

}
106 changes: 58 additions & 48 deletions src/DocumentMapper/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,43 @@

use ODM\DBAL;
use ODM\Document\Document;
use ODM\Instantiator\Instantiator;

class DataMapper
{
private $dbal;

private $table_name;
private $class;
private $class_name;
private $instantiator;

const MONGO_ID = '_id';
const ID = 'id';

/**
* DataMapper constructor.
* @param DBAL $dbal
* @param $class
* @param DBAL $dbal
* @param string $class_name
*/
public function __construct(DBAL $dbal, $class)
public function __construct(DBAL $dbal, string $class_name)
{
$this->dbal = $dbal;
$this->class = $class;
$path = explode('\\', $class);
$this->table_name = mb_strtolower($this->camelCaseToSnake(array_pop($path)));
$this->dbal = $dbal;
$this->class_name = $class_name;
$path = explode('\\', $class_name);
$this->table_name = mb_strtolower($this->camelCaseToSnake(array_pop($path)));
$this->instantiator = new Instantiator();
}

/**
* @param Document $obj
* @return mixed
* @return Document
*/
public function insert(Document $obj)
{
$data = $this->objToArray($obj);

if (array_key_exists(self::MONGO_ID, $data) && empty($data[self::MONGO_ID])) {
unset($data[self::MONGO_ID]);
$data[self::MONGO_ID] = $this->generateId();
}

$result = $this->dbal->insert($this->table_name, $data);
Expand All @@ -61,19 +64,19 @@ public function update(Document $obj)

/**
* @param Document $obj
* @return mixed
* @return \MongoDB\DeleteResult
*/
public function delete(Document $obj)
{
return $this->dbal->delete($this->table_name, [self::MONGO_ID => $obj->getId()]);
}

/**
* @return mixed
* @return bool
*/
public function drop()
{
return $this->dbal->drop($this->table_name);
return (bool)$this->dbal->drop($this->table_name);
}

/**
Expand All @@ -90,7 +93,7 @@ public function find(array $filter = [], array $options = [])

$result = [];
foreach ($this->dbal->find($this->table_name, $filter, $options) as $r) {
$result[] = $this->mapObj(new $this->class, $r);
$result[] = $this->mapObj($this->class_name, (array)$r);
}

return $result;
Expand All @@ -110,35 +113,51 @@ public function findOne(array $filter = [], array $options = [])

$data = $this->dbal->findOne($this->table_name, $filter, $options);

return empty($data) ? null : $this->mapObj(new $this->class, $data);
return empty($data) ? null : $this->mapObj($this->class_name, (array)$data);
}

/**
* @param $obj
* @param $data
* @return mixed
* @return string
*/
private function mapObj($obj, $data)
private function generateId()
{
$methods = get_class_methods(get_class($obj));
return bin2hex(random_bytes(15));
}

foreach ((array)$data as $field => $value) {
/**
* @param string $class_name
* @param array $data
* @return Document
*/
private function mapObj(string $class_name, array $data)
{
$obj = $this->instantiator->instantiate($class_name);
$reflect = new \ReflectionClass($obj);
$properties = array_merge($reflect->getProperties(), $reflect->getParentClass()->getProperties());

$field_name = self::MONGO_ID === (string)$field ? self::ID : $field;
$value = self::MONGO_ID === (string)$field ? (string)$value : $value;
$value = is_object($value) ? (array)$value : $value;
foreach($properties as $prop) {

if (null === $value) {
if ($prop->getName() === self::ID) {
$prop_name = self::MONGO_ID;
} else {
$prop_name = $prop->getName();
}

if (!array_key_exists($prop_name, $data)) {
continue;
}

$setter = 'set' . $this->snakeToCamelCase($field_name);
if ($prop->isPrivate() || $prop->isProtected()) {
$prop->setAccessible(true);
}

if ($prop->getName() === self::ID) {
$prop->setValue($obj, (string)$data[$prop_name]);

if (!in_array($setter, $methods, true)) {
continue;
}

$obj->$setter($value);
$prop->setValue($obj, $data[$prop_name]);
}

return $obj;
Expand All @@ -148,21 +167,7 @@ private function mapObj($obj, $data)
* @param $input
* @return string
*/
private function snakeToCamelCase($input)
{
$new = [];
foreach (explode('_', $input) as $key => $word) {
$new[] = ucfirst($word);
}

return implode('', $new);
}

/**
* @param $input
* @return string
*/
private function camelCaseToSnake($input)
private function camelCaseToSnake(string $input)
{
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$res = $matches[0];
Expand All @@ -174,23 +179,28 @@ private function camelCaseToSnake($input)
}

/**
* @param $obj
* @param Document $obj
* @return array
*/
public function objToArray($obj)
private function objToArray(Document $obj)
{
$data = [];
$reflect = new \ReflectionClass($obj);
foreach ($reflect->getProperties() as $prop) {
$getter = 'get' . $this->snakeToCamelCase($prop->getName());
$properties = array_merge($reflect->getProperties(), $reflect->getParentClass()->getProperties());

foreach ($properties as $prop) {

if ($prop->getName() === self::ID) {
$prop_name = self::MONGO_ID;
} else {
$prop_name = $prop->getName();
}

$data[$prop_name] = $obj->$getter();
if ($prop->isPrivate() || $prop->isProtected()) {
$prop->setAccessible(true);
}

$data[$prop_name] = $prop->getValue($obj);
}

return $data;
Expand Down
6 changes: 3 additions & 3 deletions src/DocumentMapper/DataMapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public function __construct(DBAL $dbal)
}

/**
* @param $class
* @param $class_name
* @return DataMapper
*/
public function init($class)
public function init($class_name)
{
return new DataMapper($this->dbal, $class);
return new DataMapper($this->dbal, $class_name);
}
}
Loading

0 comments on commit 8405792

Please sign in to comment.