Last active
April 1, 2018 18:44
-
-
Save mysiar/2439467394f6cfb0792c2ee2a75a12bf to your computer and use it in GitHub Desktop.
Comparing two ArrayCollections
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 | |
/** | |
* Diff of two Object Collections | |
* | |
* @param Collection $collection1 | |
* @param Collection $collection2 | |
* @param string[] $fieldsToOmit - fields to omit from comparison | |
* @return Collection|ArrayCollection | |
*/ | |
public function diffArrayCollection( | |
Collection $collection1, | |
Collection $collection2, | |
array $fieldsToOmit = ['id'] | |
): Collection { | |
$diff = array_udiff( | |
$collection1->toArray(), | |
$collection2->toArray(), | |
function (object $obj1, object $obj2) use ($fieldsToOmit) { | |
$obj1array = json_decode( | |
json_encode( | |
$this->arrayFilterByKeys($this->normalize($obj1), $fieldsToOmit) | |
), | |
true | |
); | |
$obj2array = json_decode( | |
json_encode( | |
$this->arrayFilterByKeys($this->normalize($obj2), $fieldsToOmit) | |
), | |
true | |
); | |
return strcmp(http_build_query($obj1array), http_build_query($obj2array)); | |
} | |
); | |
return new ArrayCollection($diff); | |
} | |
private function normalize(object $object): array | |
{ | |
$serializer = SerializerBuilder::create()->build(); | |
return json_decode($serializer->serialize($object, 'json'), true); | |
} | |
private function arrayFilterByKeys(array $array, array $keys): array | |
{ | |
$result = []; | |
foreach ($array as $name => $value) { | |
if (!in_array($name, $keys)) { | |
if (is_array($value)) { | |
$result[$name] = $this->arrayFilterByKeys($value, $keys); | |
} else { | |
$result[$name] = $value; | |
} | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment