Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use array_diff_key to get diff for new or deleted documents #2388

Merged
merged 5 commits into from
Nov 12, 2021
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Doctrine\ODM\MongoDB\UnitOfWork;
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;

use function array_udiff;
use function array_combine;
use function array_diff_key;
use function array_map;
use function array_udiff_assoc;
use function array_values;
use function count;
Expand Down Expand Up @@ -255,18 +257,15 @@ static function ($a, $b) {
/** {@inheritdoc} */
public function getDeletedDocuments()
{
$compare = static function ($a, $b) {
$compareA = is_object($a) ? spl_object_hash($a) : $a;
$compareb = is_object($b) ? spl_object_hash($b) : $b;

return $compareA === $compareb ? 0 : ($compareA > $compareb ? 1 : -1);
$callback = static function ($val) {
return is_object($val) ? spl_object_hash($val) : $val;
IonBazan marked this conversation as resolved.
Show resolved Hide resolved
};

return array_values(array_udiff(
$this->snapshot,
$this->coll->toArray(),
$compare
));
$coll = $this->coll->toArray();
$loadedObjectsByOid = array_combine(array_map($callback, $this->snapshot), $this->snapshot);
$newObjectsByOid = array_combine(array_map($callback, $coll), $coll);

return array_values(array_diff_key($loadedObjectsByOid, $newObjectsByOid));
}

/** {@inheritdoc} */
Expand All @@ -284,18 +283,15 @@ static function ($a, $b) {
/** {@inheritdoc} */
public function getInsertedDocuments()
{
$compare = static function ($a, $b) {
$compareA = is_object($a) ? spl_object_hash($a) : $a;
$compareb = is_object($b) ? spl_object_hash($b) : $b;

return $compareA === $compareb ? 0 : ($compareA > $compareb ? 1 : -1);
$callback = static function ($val) {
return is_object($val) ? spl_object_hash($val) : $val;
};

return array_values(array_udiff(
$this->coll->toArray(),
$this->snapshot,
$compare
));
$coll = $this->coll->toArray();
$newObjectsByOid = array_combine(array_map($callback, $coll), $coll);
$loadedObjectsByOid = array_combine(array_map($callback, $this->snapshot), $this->snapshot);

return array_values(array_diff_key($newObjectsByOid, $loadedObjectsByOid));
}

/** {@inheritdoc} */
Expand Down