You are here

public function JSONFIReader::init in Feed Import 7.3

Here you'll init your reader.

Overrides FeedImportReader::init

File

feed_import_base/inc/feed_import_readers.inc, line 637
This file contains implementations of feed import readers.

Class

JSONFIReader
JSON Reader class, used to read data from json files.

Code

public function init() {

  // Set default params
  $this->options += array(
    'stream' => NULL,
    'parent' => NULL,
    'raw' => NULL,
    'url' => NULL,
  );
  if ($this->options['url']) {

    // Check for stream options.
    if ($ctx = $this
      ->getStreamContext($this->options['stream'])) {

      // Open the file using stream options.
      $this->items = json_decode(file_get_contents($this->options['url'], FALSE, $ctx));
    }
    else {

      // Open the file.
      $this->items = json_decode(file_get_contents($this->options['url']));
    }
  }
  elseif ($this->options['raw']) {
    $this->items = json_decode($this->options['raw']);
  }
  if (!$this->items) {
    return FALSE;
  }
  unset($this->options['raw']);

  // Check for parent
  if ($this->options['parent']) {
    $this->options['parent'] = $this
      ->formatPath($this->options['parent']);
    $this->items = $this
      ->map($this->items, $this->options['parent']);
  }
  return TRUE;
}