You are here

public function FeedImportMergeNoDuplicates::merge in Feed Import 8

Merge the new and current field values. The merge should be set in $current ref. variable

Parameters

array $current: Current field values

array $new: New field values

array $field: Field info cardinality - field cardinality compare - compare function for field value

Return value

bool True if $current was changed

Overrides FeedImportMergeField::merge

File

feed_import_base/src/FeedImportMergeNoDuplicates.php, line 13

Class

FeedImportMergeNoDuplicates
Class which merges field values checking for duplicates. Default merge mode.

Namespace

Drupal\feed_import_base

Code

public function merge(array &$current, array &$new, array &$field) {
  $changed = FALSE;
  $cnt = count($new);
  if ($cnt == count($current)) {

    // Same number of items, it is possible that the
    // field is still the same
    // Just check if the items are the same.
    for ($i = 0; $i < $cnt; $i++) {
      if (!$field['compare']($new[$i], $current[$i])) {
        $changed = TRUE;
        break;
      }
    }
  }
  else {

    // Different number of items, the field is 100% changed.
    $changed = TRUE;
  }
  if ($changed) {

    // Get the new field values, but try to copy the existing ones.
    $temp = array();
    for ($i = 0; $i < $cnt; $i++) {
      $not_found = TRUE;
      foreach ($current as $key => &$val) {

        // Compare by function.
        if ($field['compare']($new[$i], $val)) {

          // Value is the same, just get a reference.
          $temp[] =& $val;
          $not_found = FALSE;
          break;
        }
      }
      unset($val);
      if ($not_found) {

        // The value is new.
        $temp[] =& $new[$i];
      }
    }

    // Set current.
    $current = $temp;
  }
  return $changed;
}