-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from makasim/partial-update
Partial update.
- Loading branch information
Showing
7 changed files
with
321 additions
and
19 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ services: | |
|
||
mongo: | ||
image: mongo:3 | ||
ports: | ||
- "27017:27017" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
namespace Makasim\Yadm; | ||
|
||
use function Makasim\Values\get_values; | ||
use mikemccabe\JsonPatch\JsonPatch; | ||
|
||
class ChangesCollector | ||
{ | ||
private $originalValues; | ||
|
||
public function register($object) | ||
{ | ||
if ($id = get_object_id($object)) { | ||
$this->originalValues[$id] = get_values($object); | ||
} | ||
} | ||
|
||
public function unregister($object) | ||
{ | ||
if ($id = get_object_id($object)) { | ||
unset($this->originalValues[$id]); | ||
} | ||
} | ||
|
||
public function changes($object) | ||
{ | ||
if (false == $id = get_object_id($object)) { | ||
throw new \LogicException(sprintf('Object does not have an id set.')); | ||
} | ||
|
||
if (false == array_key_exists($id, $this->originalValues)) { | ||
throw new \LogicException(sprintf('Changes has not been collected. The object with id "%s" original data is missing.')); | ||
} | ||
|
||
$diff = JsonPatch::diff($this->originalValues[$id], get_values($object)); | ||
|
||
$update = ['$set' => [], '$unset' => []]; | ||
foreach ($diff as $op) { | ||
switch ($op['op']) { | ||
case 'add': | ||
if (is_array($op['value'])) { | ||
foreach ($op['value'] as $key => $value) { | ||
$update['$set'][$this->pathToDot($op['path']).'.'.$key] = $value; | ||
} | ||
} else { | ||
$update['$set'][$this->pathToDot($op['path'])] = $op['value']; | ||
} | ||
|
||
break; | ||
case 'remove': | ||
$update['$unset'][$this->pathToDot($op['path'])] = ''; | ||
|
||
break; | ||
case 'replace': | ||
$update['$set'][$this->pathToDot($op['path'])] = $op['value']; | ||
|
||
break; | ||
default: | ||
throw new \LogicException('JSON Patch operation "'.$op['op'].'"" is not supported.'); | ||
} | ||
|
||
|
||
} | ||
|
||
if (empty($update['$set'])) { | ||
unset($update['$set']); | ||
} | ||
if (empty($update['$unset'])) { | ||
unset($update['$unset']); | ||
} | ||
|
||
return $update; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* | ||
* @return string | ||
*/ | ||
private function pathToDot($path) | ||
{ | ||
$path = ltrim($path, '/'); | ||
|
||
return str_replace('/', '.', $path); | ||
} | ||
} |
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
Oops, something went wrong.