/**
* Utility method to get the hash after removing selected values. This lets us detect changes other than
* modified date (which will change on every save).
*
* @param mixed $jsonData Either an object or a string with json-encoded data
* @param ContentType $typeTable Table object with data for this content type
*
* @return string SHA1 hash on success. Empty string on failure.
*
* @since 3.2
*/
public function getSha1($jsonData, ContentType $typeTable)
{
$object = \is_object($jsonData) ? $jsonData : json_decode($jsonData);
if (isset($typeTable->content_history_options) && \is_object(json_decode($typeTable->content_history_options))) {
$options = json_decode($typeTable->content_history_options);
$this->ignoreChanges = $options->ignoreChanges ?? $this->ignoreChanges;
$this->convertToInt = $options->convertToInt ?? $this->convertToInt;
}
foreach ($this->ignoreChanges as $remove) {
if (property_exists($object, $remove)) {
unset($object->{$remove});
}
}
// Convert integers, booleans, and nulls to strings to get a consistent hash value
foreach ($object as $name => $value) {
if (\is_object($value)) {
// Go one level down for JSON column values
foreach ($value as $subName => $subValue) {
$object->{$subName} = \is_int($subValue) || \is_bool($subValue) || $subValue === null ? (string) $subValue : $subValue;
}
} else {
$object->{$name} = \is_int($value) || \is_bool($value) || $value === null ? (string) $value : $value;
}
}
// Work around empty values
foreach ($this->convertToInt as $convert) {
if (isset($object->{$convert})) {
$object->{$convert} = (int) $object->{$convert};
}
}
if (isset($object->review_time)) {
$object->review_time = (int) $object->review_time;
}
return sha1(json_encode($object));
}