You are here

abstract class FeedImportVectorReader in Feed Import 8

This class is a helper for vector (n dimensions) reader. Path format is like a/b/c which results in [a, b, c] array.

Hierarchy

Expanded class hierarchy of FeedImportVectorReader

File

feed_import_base/src/FeedImportVectorReader.php, line 8

Namespace

Drupal\feed_import_base
View source
abstract class FeedImportVectorReader extends FeedImportReader {
  const WILDCARD = '*';

  /**
   * {@inheritdoc}
   */
  public function map(&$vector, &$path) {
    $ret = array();
    $count = 0;
    foreach ($path as $p) {
      if (($p = $this
        ->submap($vector, $p)) !== NULL) {
        if ($p instanceof ArrayObject) {
          $ret = array_merge($ret, $p
            ->getArrayCopy());
          $count += count($p);
        }
        else {
          $ret[] = $p;
          $count++;
        }
      }
    }
    return $count == 0 ? NULL : ($count == 1 ? $ret[0] : $ret);
  }

  /**
   * Gets an element by path.
   *
   * @param mixed $vector
   *    The vector to search
   * @param array $path
   *    Path parts
   * @param int $index
   *    Path index
   *
   * @return mixed
   *    Found value or NULL
   */
  public function submap(&$vector, &$path, $index = 0) {
    while (isset($path[$index])) {
      $p =& $path[$index++];
      if ($p == static::WILDCARD) {
        if (is_scalar($vector)) {
          return NULL;
        }
        $result = new ArrayObject();
        foreach ($vector as &$value) {
          if (($res = $this
            ->submap($value, $path, $index)) !== NULL) {
            if ($res instanceof ArrayObject) {
              foreach ($res as &$r) {
                $result[] = $r;
              }
              unset($r);
            }
            else {
              $result[] = $res;
            }
          }
          unset($res);
        }
        return $result ? $result : NULL;
      }
      elseif (is_array($vector)) {
        if (isset($vector[$p])) {
          $vector =& $vector[$p];
          continue;
        }
      }
      elseif (is_object($vector)) {
        if (isset($vector->{$p})) {
          $vector =& $vector->{$p};
          continue;
        }
      }
      return NULL;
    }
    return $vector;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
FeedImportConfigurable::$options protected property
FeedImportConfigurable::cleanLines public static function Helper function to get lines of a string
FeedImportConfigurable::setOptions public function Sets options for this instance 4
FeedImportReader::$items protected property
FeedImportReader::get abstract public function This method returns the next available item or NULL if there are no items left. 6
FeedImportReader::getStreamContext public function Returns a stream context
FeedImportReader::init abstract public function Here you'll init your reader. 6
FeedImportReader::__construct final public function Constructor of reader. Constructor is final but you'll have to implement init() to init your reader.
FeedImportReader::__destruct public function Destructor. 2
FeedImportVectorReader::formatPath public function Override this to preprocess your paths before they are used in map(). Overrides FeedImportReader::formatPath
FeedImportVectorReader::map public function Returns a value mapped from obj by path. Overrides FeedImportReader::map
FeedImportVectorReader::submap public function Gets an element by path.
FeedImportVectorReader::WILDCARD constant