26 lines
621 B
PHP
26 lines
621 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
/** Normalize object key order only. Ordered coordinates and lists retain their meaning. */
|
|
class OpportunityCanonicalValue
|
|
{
|
|
public static function hash(mixed $value): string
|
|
{
|
|
return hash('sha256', json_encode(self::normalize($value), JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
private static function normalize(mixed $value): mixed
|
|
{
|
|
if (! is_array($value)) {
|
|
return $value;
|
|
}
|
|
$result = array_map(self::normalize(...), $value);
|
|
if (! array_is_list($result)) {
|
|
ksort($result);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|