Skip to content

Instantly share code, notes, and snippets.

@ptomulik
Created September 23, 2020 08:17
Show Gist options
  • Save ptomulik/e281e05ec8a08aaa910028f925727912 to your computer and use it in GitHub Desktop.
Save ptomulik/e281e05ec8a08aaa910028f925727912 to your computer and use it in GitHub Desktop.
<?php
abstract class AbstractProperties extends \ArrayObject implements PropertiesInterface
{
final public function getComparableArray() : array
{
return $this->getComparableArrayRecursive([]);
}
final protected function getComparableArrayRecursive(array $stack)
{
$hash = spl_object_hash($this);
$stack[$hash] = $this;
$array = $this->getArrayCopy();
array_walk_recursive($array, [$this, 'makeValueComparable']);
unset($stack[$hash]);
return $array;
}
final protected function makeValueComparable(&$value, array $stack) : void
{
if ($value instanceof static) {
$hash = spl_object_hash($value);
if (($stack[$hash] ?? null) === null) {
$value = $value->getComparableArrayRecursive($stack);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment