Skip to content

Commit

Permalink
store mongo object id outside values array.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Apr 12, 2017
1 parent dc462fb commit 1dae546
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
2 changes: 0 additions & 2 deletions src/ChangesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public function changes($object)
return Converter::convertJsonPatchToMongoUpdate($diff);
}

unset($values['_id']);

return ['$set' => $values];
})->call($object);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use function Makasim\Values\build_object;
use function Makasim\Values\set_values;
use MongoDB\BSON\ObjectID;

class Hydrator
{
Expand Down Expand Up @@ -41,7 +42,9 @@ public function hydrate(array $values, $model = null)
$model = $model ?: $this->create($values);

if (isset($values['_id'])) {
$values['_id'] = (string) $values['_id'];
set_object_id($model, new ObjectID((string) $values['_id']));

unset($values['_id']);
}

set_values($model, $values);
Expand Down
22 changes: 6 additions & 16 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function insert($model, array $options = [])
throw new \LogicException('Operation is not acknowledged');
}

set_object_id($model, $result->getInsertedId());
set_object_id($model, new ObjectID((string) $result->getInsertedId()));
$this->changesCollector->register($model);

return $result;
Expand Down Expand Up @@ -111,21 +111,18 @@ public function insertMany(array $models, array $options = [])
public function update($model, $filter = null, array $options = [])
{
if (null === $filter) {
$filter = ['_id' => new ObjectID(get_object_id($model))];
$filter = ['_id' => get_object_id($model)];
}

$update = $this->changesCollector->changes($model);
if (empty($update)) {
return;
}

if (false == $update) {
return;
}

$result = $this->collection->updateOne($filter, $update, $options);
if (false == $result->isAcknowledged()) {
throw new \LogicException('Operation is not acknowledged');

if ($result->getUpsertedCount()) {
set_object_id($model, new ObjectID((string) $result->getUpsertedId()));
}

$this->changesCollector->register($model);
Expand All @@ -141,14 +138,7 @@ public function update($model, $filter = null, array $options = [])
*/
public function delete($model, array $options = [])
{
$modelId = new ObjectID(get_object_id($model));

$result = $this->collection->deleteOne(['_id' => $modelId], $options);
if (false == $result->isAcknowledged()) {
throw new \LogicException('Operation is not acknowledged');
}

return $result;
return $this->collection->deleteOne(['_id' => get_object_id($model)], $options);
}

/**
Expand Down
28 changes: 17 additions & 11 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@

/**
* @param object $object
* @param bool $orNull
*
* @return string
* @return ObjectID|null
*/
function get_object_id($object)
function get_object_id($object, $orNull = false)
{
return (function () {
return (string) isset($this->values['_id']) ? $this->values['_id'] : null;
return (function () use ($orNull) {
$id = isset($this->_id) ? $this->_id : null;


if (false == $id && false == $orNull) {
throw new \LogicException('The object id is not set.');
}

return $id;
})->call($object);
}

/**
* @param object $object
* @param ObjectID|string $objectId
* @param object $object
* @param ObjectID $id
*/
function set_object_id($object, $objectId)
function set_object_id($object, ObjectID $id)
{
return (function () use ($objectId) {
$this->values['_id'] = (string) $objectId;
})->call($object);
}
(function () use ($id) { $this->_id = $id; })->call($object);
}

0 comments on commit 1dae546

Please sign in to comment.