diff --git a/application/Espo/ORM/Util.php b/application/Espo/ORM/Util.php index a2eccd3bf8..922b4589b7 100644 --- a/application/Espo/ORM/Util.php +++ b/application/Espo/ORM/Util.php @@ -41,78 +41,86 @@ class Util */ public static function areValuesEqual(string $type, mixed $v1, mixed $v2, bool $isUnordered = false): bool { - if ($type === AttributeType::JSON_ARRAY) { - if (is_array($v1) && is_array($v2)) { - if ($isUnordered) { - sort($v1); - sort($v2); - } + $stack = [[$type, $v1, $v2, $isUnordered]]; - if ($v1 != $v2) { - return false; - } + while ($stack) { + [$type, $v1, $v2, $isUnordered] = array_pop($stack); - foreach ($v1 as $i => $itemValue) { - $otherValue = $v2[$i]; + if ($v1 === $v2) { + continue; + } - if (is_object($itemValue) && is_object($otherValue)) { - if (!self::areValuesEqual(AttributeType::JSON_OBJECT, $itemValue, $otherValue)) { - return false; - } - - continue; + if ($type === AttributeType::JSON_ARRAY) { + if (is_array($v1) && is_array($v2)) { + if ($isUnordered) { + sort($v1); + sort($v2); } - if (is_array($itemValue) && is_array($otherValue)) { - if (!self::areValuesEqual(AttributeType::JSON_ARRAY, $itemValue, $otherValue)) { - return false; - } - - continue; - } - - if ($itemValue !== $otherValue) { + if ($v1 != $v2) { return false; } + + foreach ($v1 as $i => $itemValue) { + $otherValue = $v2[$i]; + + if (is_object($itemValue) && is_object($otherValue)) { + $stack[] = [AttributeType::JSON_OBJECT, $itemValue, $otherValue, false]; + + continue; + } + + if (is_array($itemValue) && is_array($otherValue)) { + $stack[] = [AttributeType::JSON_ARRAY, $itemValue, $otherValue, false]; + + continue; + } + + if ($itemValue !== $otherValue) { + return false; + } + } + + continue; } - return true; + return false; } - } else if ($type === AttributeType::JSON_OBJECT) { - if (is_object($v1) && is_object($v2)) { - if ($v1 != $v2) { - return false; - } - $a1 = get_object_vars($v1); - $a2 = get_object_vars($v2); - - foreach (get_object_vars($v1) as $key => $itemValue) { - if (is_object($a1[$key]) && is_object($a2[$key])) { - if (!self::areValuesEqual(AttributeType::JSON_OBJECT, $a1[$key], $a2[$key])) { - return false; - } - - continue; - } - - if (is_array($a1[$key]) && is_array($a2[$key])) { - if (!self::areValuesEqual(AttributeType::JSON_ARRAY, $a1[$key], $a2[$key])) { - return false; - } - - continue; - } - - if ($a1[$key] !== $a2[$key]) { + if ($type === AttributeType::JSON_OBJECT) { + if (is_object($v1) && is_object($v2)) { + if ($v1 != $v2) { return false; } - } - return true; + $a1 = get_object_vars($v1); + $a2 = get_object_vars($v2); + + foreach (get_object_vars($v1) as $key => $itemValue) { + if (is_object($a1[$key]) && is_object($a2[$key])) { + $stack[] = [AttributeType::JSON_OBJECT, $a1[$key], $a2[$key], false]; + + continue; + } + + if (is_array($a1[$key]) && is_array($a2[$key])) { + $stack[] = [AttributeType::JSON_ARRAY, $a1[$key], $a2[$key], false]; + + continue; + } + + if ($a1[$key] !== $a2[$key]) { + return false; + } + } + + continue; + } } + + return false; } - return $v1 === $v2; + return true; } }