You are here

public function ViewsJsonQuery::apath in Views Json Source 8

Same name and namespace in other branches
  1. 1.x src/Plugin/views/query/ViewsJsonQuery.php \Drupal\views_json_source\Plugin\views\query\ViewsJsonQuery::apath()

Fetch data in array according to apath.

Parameters

string $apath: Something like '1/name/0'.

array $array: The json document content.

Return value

array The json document matching the path.

1 call to ViewsJsonQuery::apath()
ViewsJsonQuery::parse in src/Plugin/views/query/ViewsJsonQuery.php
Parse.

File

src/Plugin/views/query/ViewsJsonQuery.php, line 257

Class

ViewsJsonQuery
Base query handler for views_json_source.

Namespace

Drupal\views_json_source\Plugin\views\query

Code

public function apath($apath, array $array) {
  $r =& $array;
  $paths = explode('/', trim($apath, '//'));
  foreach ($paths as $path) {
    if ($path == '%') {

      // Replace with the contextual filter value.
      $key = $this
        ->getCurrentContextualFilter();
      if (!empty($key) && array_key_exists($key, $r)) {
        $r = $r[$key];
      }
    }
    elseif (stripos($path, '=') !== FALSE) {
      $search_data = explode('=', $path);
      $array_key = $search_data[0];
      $array_value = $search_data[1];
      foreach ($r as $key => $row) {
        if (isset($row[$array_key]) && $row[$array_key] == $array_value) {
          $r = $r[$key];
          break;
        }
      }
    }
    elseif (is_array($r) && isset($r[$path])) {
      $r =& $r[$path];
    }
    elseif (is_object($r)) {
      $r =& $r->{$path};
    }
    else {
      break;
    }
  }
  return $r;
}