Skip to content

Commit

Permalink
Improve compare by stripping empty dates, strings and supertable keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyzant committed Feb 27, 2024
1 parent 671dffc commit 85df7ff
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions src/helpers/DataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ public static function compareElementContent($content, $element): ?bool
$newValue = null;
}

// If the values match exactly they are not changed
if ($existingValue === $newValue) {
unset($trackedChanges[$key]);
continue;
}

// If array key & values are already within the existing array
if (is_array($newValue) && is_array($existingValue) && Hash::contains($existingValue,$newValue)) {
unset($trackedChanges[$key]);
Expand Down Expand Up @@ -445,14 +451,31 @@ private static function _compareSimpleValues($fields, $key, $firstValue, $second
private static function _recursiveCompare($firstValue, $secondValue): bool
{
if (is_array($firstValue) && is_array($secondValue)) {
// Ignore values that are `null` or empty arrays
// Ignore values that are empty: null values, empty strings, empty arrays, empty dates
$firstValue = array_filter($firstValue, static function($value) {
return !($value === null || $value === []);
return
!($value === null || $value === '' || $value === []) &&
!(is_array($value) && isset($value['date']) && $value['date'] === '');
});
$secondValue = array_filter($secondValue, static function($value) {
return !($value === null || $value === []);
return
!($value === null || $value === '' || $value === []) &&
!(is_array($value) && isset($value['date']) && $value['date'] === '');
});

// Ignore extra SuperTable fields when comparing
if (isset($firstValue['type'])
&& isset($firstValue['fields'])
&& isset($secondValue['type'])
&& isset($secondValue['fields'])
) {
foreach (['order', 'enabled'] as $field) {
if (! isset($firstValue[$field])) {
unset($secondValue[$field]);
}
}
}

// Both values must have the same keys (ignoring order)
$firstKeys = array_keys($firstValue);
$secondKeys = array_keys($secondValue);
Expand All @@ -461,37 +484,37 @@ private static function _recursiveCompare($firstValue, $secondValue): bool
}

// Each key must be the same value
foreach ($firstValue as $key => $value) {
foreach ($firstValue as $key => $existingValue) {
$newValue = $secondValue[$key];

// If date value, make sure to cast it as a string to compare
if ($value instanceof DateTime || DateTimeHelper::isIso8601($value)) {
$value = Db::prepareDateForDb($value);
if ($existingValue instanceof DateTime || DateTimeHelper::isIso8601($existingValue)) {
$existingValue = Db::prepareDateForDb($existingValue);
}

// If date value, make sure to cast it as a string to compare
if ($newValue instanceof DateTime || DateTimeHelper::isIso8601($newValue)) {
$newValue = Db::prepareDateForDb($newValue);
}

// If an empty 'date' value, it's the same as null
if (is_array($newValue) && isset($newValue['date']) && $newValue['date'] === '') {
$newValue = null;
}

if (!self::_recursiveCompare($value, $newValue)) {
if (!self::_recursiveCompare($existingValue, $newValue)) {
return false;
}
}

return true;
}

// For now this does not seem relevant
if (is_object($firstValue) && is_object($secondValue)) {
// For now this does not seem relevant
return false;
}

// Perform a loose compare when two values are numeric
if (is_numeric($firstValue) && is_numeric($secondValue)) {
return $firstValue == $secondValue;
}

return $firstValue === $secondValue;
}

Expand Down

0 comments on commit 85df7ff

Please sign in to comment.