You are here

FeedImportMergeNoDuplicates.php in Feed Import 8

File

feed_import_base/src/FeedImportMergeNoDuplicates.php
View source
<?php

namespace Drupal\feed_import_base;


/**
 * Class which merges field values checking for duplicates.
 * Default merge mode.
 */
class FeedImportMergeNoDuplicates extends FeedImportMergeField {

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Classes

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