You are here

class InstantArticleRssEncoder in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 src/Encoder/InstantArticleRssEncoder.php \Drupal\fb_instant_articles\Encoder\InstantArticleRssEncoder

Facebook instant articles FBIA RSS encoder.

Hierarchy

  • class \Drupal\serialization\Encoder\XmlEncoder implements \Symfony\Component\Serializer\SerializerAwareInterface, \Symfony\Component\Serializer\Encoder\EncoderInterface, \Symfony\Component\Serializer\Encoder\DecoderInterface uses \Symfony\Component\Serializer\SerializerAwareTrait

Expanded class hierarchy of InstantArticleRssEncoder

1 string reference to 'InstantArticleRssEncoder'
fb_instant_articles.services.yml in ./fb_instant_articles.services.yml
fb_instant_articles.services.yml
1 service uses InstantArticleRssEncoder
serializer.fb_instant_articles.fbia_rss.encoder in ./fb_instant_articles.services.yml
Drupal\fb_instant_articles\Encoder\InstantArticleRssEncoder

File

src/Encoder/InstantArticleRssEncoder.php, line 15

Namespace

Drupal\fb_instant_articles\Encoder
View source
class InstantArticleRssEncoder extends XmlEncoder {
  use StringTranslationTrait;

  /**
   * The format that this encoder supports.
   *
   * @var string
   */
  protected static $format = [
    'fbia_rss',
  ];

  /**
   * The current request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

  /**
   * Instant articles settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Create a Instant Article RSS encoder.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   Request stack.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory interface.
   */
  public function __construct(RequestStack $request_stack, ConfigFactoryInterface $config_factory) {
    $this->currentRequest = $request_stack
      ->getCurrentRequest();
    $this->config = $config_factory
      ->get('fb_instant_articles.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseEncoder() {

    // Overridden to set rss as the type.
    if (!isset($this->baseEncoder)) {
      $this->baseEncoder = new BaseXmlEncoder('rss');
      $this->baseEncoder
        ->setSerializer($this->serializer);
    }
    return $this->baseEncoder;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDecoding($format) {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function encode($data, $format, array $context = []) {

    // Force $data into an array of numeric keys.
    if (!ctype_digit(implode('', array_keys($data)))) {
      $data = [
        $data,
      ];
    }

    // Render all each InstantArticle object.
    foreach ($data as $delta => $item) {
      if (!empty($item['content:encoded']) && $item['content:encoded'] instanceof InstantArticle) {
        $data[$delta]['content:encoded'] = $item['content:encoded']
          ->render();
      }
    }

    // Wrapping tags.
    $feed_title = $this
      ->t('Facebook Instant Articles RSS Feed');
    $feed_description = '';
    if (isset($context['views_style_plugin'])) {

      /** @var \Drupal\rest\Plugin\views\style\Serializer $style */
      $style = $context['views_style_plugin'];
      $feed_title = $style->view
        ->getTitle();
      $feed_description = $style->view->storage
        ->get('description');
    }
    $encoded = [
      '@version' => '2.0',
      '@xmlns:content' => 'http://purl.org/rss/1.0/modules/content/',
      'channel' => [
        'title' => $feed_title,
        'link' => $this
          ->getLink(),
        'lastBuildDate' => date('c', time()),
      ],
    ];
    if (!empty($feed_description)) {
      $encoded['channel']['description'] = $feed_description;
    }
    $encoded['channel']['item'] = $data;
    return parent::encode($encoded, $format, $context);
  }

  /**
   * Helper function to get the URL of the site for the RSS feed <link> tag.
   *
   * @return string
   *   URL of the site.
   */
  protected function getLink() {
    if ($override = $this->config
      ->get('canonical_url_override')) {
      return $override;
    }
    else {
      return $this->currentRequest
        ->getSchemeAndHttpHost();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InstantArticleRssEncoder::$config protected property Instant articles settings.
InstantArticleRssEncoder::$currentRequest protected property The current request object.
InstantArticleRssEncoder::$format protected static property The format that this encoder supports. Overrides XmlEncoder::$format
InstantArticleRssEncoder::encode public function Overrides XmlEncoder::encode
InstantArticleRssEncoder::getBaseEncoder public function Gets the base encoder instance. Overrides XmlEncoder::getBaseEncoder
InstantArticleRssEncoder::getLink protected function Helper function to get the URL of the site for the RSS feed <link> tag.
InstantArticleRssEncoder::supportsDecoding public function Overrides XmlEncoder::supportsDecoding
InstantArticleRssEncoder::__construct public function Create a Instant Article RSS encoder.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
XmlEncoder::$baseEncoder protected property An instance of the Symfony XmlEncoder to perform the actual encoding.
XmlEncoder::decode public function
XmlEncoder::setBaseEncoder public function Sets the base encoder instance.
XmlEncoder::supportsEncoding public function