Created
September 23, 2020 08:17
-
-
Save ptomulik/e281e05ec8a08aaa910028f925727912 to your computer and use it in GitHub Desktop.
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 | |
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