You are here

FeedImportUniVectorReader.php in Feed Import 8

File

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

namespace Drupal\feed_import_base;


/**
 * This class is a helper for unidimensional vector reader.
 * This can be used for CSV, SQL results, etc.
 */
abstract class FeedImportUniVectorReader extends FeedImportReader {

  /**
   * {@inheritdoc}
   */
  public function map(&$vector, &$path) {
    if (is_array($vector)) {
      $count = 0;
      $ret = array();
      foreach ($path as $p) {
        if (isset($vector[$p])) {
          $ret[] = $vector[$p];
          $count++;
        }
      }
      return $count == 0 ? NULL : ($count == 1 ? $ret[0] : $ret);
    }
    elseif (is_object($vector)) {
      $count = 0;
      $ret = array();
      foreach ($path as $p) {
        if (isset($vector->{$p})) {
          $ret[] = $vector->{$p};
          $count++;
        }
      }
      return $count == 0 ? NULL : ($count == 1 ? $ret[0] : $ret);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function formatPath($path) {
    return preg_split('/(\\s?\\|\\s?)/', $path, -1, PREG_SPLIT_NO_EMPTY);
  }

}

Classes

Namesort descending Description
FeedImportUniVectorReader This class is a helper for unidimensional vector reader. This can be used for CSV, SQL results, etc.