You are here

public function SyndicationParser::parse in Feeds 8.3

Parses content returned by fetcher.

@todo This needs more documentation.

Parameters

\Drupal\feeds\FeedInterface $feed: The feed we are parsing for.

\Drupal\feeds\Result\FetcherResultInterface $fetcher_result: The result returned by the fetcher.

\Drupal\feeds\StateInterface $state: The state object.

Return value

\Drupal\feeds\Result\ParserResultInterface The parser result object.

Overrides ParserInterface::parse

File

src/Feeds/Parser/SyndicationParser.php, line 30

Class

SyndicationParser
Defines an RSS and Atom feed parser.

Namespace

Drupal\feeds\Feeds\Parser

Code

public function parse(FeedInterface $feed, FetcherResultInterface $fetcher_result, StateInterface $state) {
  $result = new ParserResult();
  Reader::setExtensionManager(\Drupal::service('feed.bridge.reader'));
  Reader::registerExtension('GeoRSS');
  $raw = $fetcher_result
    ->getRaw();
  if (!strlen(trim($raw))) {
    throw new EmptyFeedException();
  }
  try {
    $channel = Reader::importString($raw);
  } catch (ExceptionInterface $e) {
    $args = [
      '%site' => $feed
        ->label(),
      '%error' => trim($e
        ->getMessage()),
    ];
    throw new \RuntimeException($this
      ->t('The feed from %site seems to be broken because of error "%error".', $args));
  }
  foreach ($channel as $delta => $entry) {
    $item = new SyndicationItem();

    // Move the values to an array as expected by processors.
    $item
      ->set('title', $entry
      ->getTitle())
      ->set('guid', $entry
      ->getId())
      ->set('url', $entry
      ->getLink())
      ->set('guid', $entry
      ->getId())
      ->set('url', $entry
      ->getLink())
      ->set('description', $entry
      ->getDescription())
      ->set('content', $entry
      ->getContent())
      ->set('tags', $entry
      ->getCategories()
      ->getValues())
      ->set('feed_title', $channel
      ->getTitle())
      ->set('feed_description', $channel
      ->getDescription())
      ->set('feed_url', $channel
      ->getLink());
    if ($image = $channel
      ->getImage()) {
      $item
        ->set('feed_image_uri', $image['uri']);
    }
    if ($enclosure = $entry
      ->getEnclosure()) {
      $item
        ->set('enclosures', [
        rawurldecode($enclosure->url),
      ]);
    }
    if ($author = $entry
      ->getAuthor()) {
      $author += [
        'name' => '',
        'email' => '',
      ];
      $item
        ->set('author_name', $author['name'])
        ->set('author_email', $author['email']);
    }
    if ($date = $entry
      ->getDateCreated()) {
      $item
        ->set('timestamp', $date
        ->getTimestamp());
    }
    if ($date = $entry
      ->getDateModified()) {
      $item
        ->set('updated', $date
        ->getTimestamp());
    }
    if ($point = $entry
      ->getGeoPoint()) {
      $item
        ->set('georss_lat', $point['lat'])
        ->set('georss_lon', $point['lon']);
    }
    $result
      ->addItem($item);
  }
  return $result;
}