You are here

public function FeedImportVectorReader::submap in Feed Import 8

Gets an element by path.

Parameters

mixed $vector: The vector to search

array $path: Path parts

int $index: Path index

Return value

mixed Found value or NULL

1 call to FeedImportVectorReader::submap()
FeedImportVectorReader::map in feed_import_base/src/FeedImportVectorReader.php
Returns a value mapped from obj by path.

File

feed_import_base/src/FeedImportVectorReader.php, line 46

Class

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

Namespace

Drupal\feed_import_base

Code

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;
}