getDataFetcherPlugin()->getResponseContent($url); // Convert objects to associative arrays. $source= json_decode($response, TRUE); // If json_decode() has returned NULL, it might be that the data isn't // valid utf8 - see http://php.net/manual/en/function.json-decode.php#86997. if (is_null($source)) { $utf8response = utf8_encode($response); $source = json_decode($utf8response, TRUE); } return $source; } /** * Retrieves the JSON data and returns it as an array. * * @param string $url * URL of a JSON feed. * * @return array * The selected data to be iterated. * * @throws \GuzzleHttp\Exception\RequestException */ protected function getSourceData($source) { $data = []; $selectors = explode('/', trim('data/', '/')); foreach ($selectors as $selector) { if (!empty($selector)) { $data = $source[$selector]; } } return $data; } /** * Retrieves the JSON data and returns it as an array. * * @param string $url * URL of a JSON feed. * * @return array * The selected data to be iterated. * * @throws \GuzzleHttp\Exception\RequestException */ protected function getSourceIncluded($source) { if (!isset($source['included'])) { return []; } $included = []; // todo $selectors = explode('/', trim('included/', '/')); foreach ($selectors as $selector) { if (!empty($selector)) { $included = $source[$selector]; } } return $included; } /** * {@inheritdoc} */ protected function openSourceUrl($url) { // extract all relationship definition and add to URL as included $relationships = []; foreach ($this->configuration['fields'] as $field) { if (isset($field['relationship'])) { $relationships[] = $field['relationship']; } } if (!empty($relationships)) { $parts = UrlHelper::parse($url); if (isset($parts['include'])) { $include = explode(',', $parts['include']); $include = array_flip($include); } else { $include = []; } foreach ($relationships as $relationship) { $include[$relationship] = 1; } $path = $parts['path']; $options['query'] = $parts['query']; $options['fragment'] = $parts['fragment']; $options['query']['include'] = implode(',', array_keys($include)); $url = Url::fromUri($path, $options)->toString(); } drush_print($url); // (Re)open the provided URL. $source= $this->getSource($url); $this->dataIterator = new \ArrayIterator($this->getSourceData($source)); $this->includedIterator = new \ArrayIterator($this->getSourceIncluded($source)); if (isset($source['links']['next'])) { $this->urls[] = $source['links']['next']; } return TRUE; } /** * {@inheritdoc} */ protected function fetchNextRow() { $current = $this->dataIterator->current(); if ($current) { foreach ($this->fieldSelectors() as $field_name => $field_info) { if ( isset($field_info['relationship']) && isset($current['relationships'][$field_info['relationship']]) ) { $field_data= []; $relationships = []; $relationship_data = $current['relationships'][$field_info['relationship']]['data']; if (empty($relationship_data)) { $field_data = null; } else { if (isset($relationship_data['type'])) { $relationships[] = $relationship_data; } else { $relationships = $relationship_data; } foreach ($relationships as $relationship) { foreach ($this->includedIterator as $included) { if ($included['type'] == $relationship['type'] && $included['id'] == $relationship['id']) { if (isset($relationship['meta'])) { $included['meta'] = $relationship['meta']; } $field_data[] = $included; } } } if (isset($field_info['multiple']) && $field_info['multiple']) { $field_data = $field_data; } else { $field_data = array_shift($field_data); } } } else { $field_data = $current; } if (!is_null($field_data)) { $selector = $field_info['selector']; $field_selectors = explode('/', trim($selector, '/')); if (isset($field_info['multiple']) && $field_info['multiple']) { foreach ($field_selectors as $field_selector) { array_walk($field_data, function (&$v) use ($field_selector) { if (is_array($v) && array_key_exists($field_selector, $v)) { $v = $v[$field_selector]; } }); } } else { foreach ($field_selectors as $field_selector) { if (is_array($field_data) && array_key_exists($field_selector, $field_data)) { $field_data = $field_data[$field_selector]; } else { $field_data = ''; } } } } $this->currentItem[$field_name] = $field_data; } if (!empty($this->configuration['include_raw_data'])) { $this->currentItem['raw'] = $current; } $this->dataIterator->next(); } } /** * Return the selectors used to populate each configured field. * * @return string[] * Array of selectors, keyed by field name. */ protected function fieldSelectors() { $fields = []; foreach ($this->configuration['fields'] as $field_info) { if (isset($field_info['selector'])) { $fields[$field_info['name']] = $field_info; } } return $fields; } }