class InstantArticleRssEncoder in Facebook Instant Articles 8.2
Same name and namespace in other branches
- 3.x 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\Encoder\EncoderInterface, \Symfony\Component\Serializer\Encoder\DecoderInterface, \Symfony\Component\Serializer\SerializerAwareInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\fb_instant_articles\Encoder\InstantArticleRssEncoder uses StringTranslationTrait
Expanded class hierarchy of InstantArticleRssEncoder
1 string reference to 'InstantArticleRssEncoder'
1 service uses InstantArticleRssEncoder
File
- src/
Encoder/ InstantArticleRssEncoder.php, line 15
Namespace
Drupal\fb_instant_articles\EncoderView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
InstantArticleRssEncoder:: |
protected | property | Instant articles settings. | |
InstantArticleRssEncoder:: |
protected | property | The current request object. | |
InstantArticleRssEncoder:: |
protected static | property |
The format that this encoder supports. Overrides XmlEncoder:: |
|
InstantArticleRssEncoder:: |
public | function |
Encodes data into the given format. Overrides XmlEncoder:: |
|
InstantArticleRssEncoder:: |
public | function |
Gets the base encoder instance. Overrides XmlEncoder:: |
|
InstantArticleRssEncoder:: |
protected | function | Helper function to get the URL of the site for the RSS feed <link> tag. | |
InstantArticleRssEncoder:: |
public | function |
Checks whether the deserializer can decode from given format. Overrides XmlEncoder:: |
|
InstantArticleRssEncoder:: |
public | function | Create a Instant Article RSS encoder. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
XmlEncoder:: |
protected | property | An instance of the Symfony XmlEncoder to perform the actual encoding. | |
XmlEncoder:: |
public | function | Decodes a string into PHP data. | |
XmlEncoder:: |
public | function | Sets the base encoder instance. | |
XmlEncoder:: |
public | function | Checks whether the serializer can encode to given format. |