Skip to content

Instantly share code, notes, and snippets.

@kAlvaro
Created January 5, 2019 12:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kAlvaro/531d4e2456f66a8231ebde34d73af959 to your computer and use it in GitHub Desktop.
Save kAlvaro/531d4e2456f66a8231ebde34d73af959 to your computer and use it in GitHub Desktop.
Quick and dirty workaround for https://github.com/suin/php-rss-writer/issues/28
<?php
namespace BorrameCom\RSSWriter;
class Channel extends \Suin\RSSWriter\Channel
{
/**
* @var string
*/
protected $imageUrl;
/**
* @var string
*/
protected $imageTitle;
/**
* @var string
*/
protected $imageLink;
/**
* @param string $url URL of a GIF, JPEG or PNG image that represents the channel.
* @param string $title Describes the image, it's used in the ALT attribute of the HTML <img> tag when the channel is rendered in HTML.
* @param string $link URL of the site, when the channel is rendered, the image is a link to the site.
* @return $this
*
* @link https://cyber.harvard.edu/rss/rss.html#ltimagegtSubelementOfLtchannelgt
*/
public function image(string $url, string $title, string $link): Channel
{
$this->imageUrl = $url;
$this->imageTitle = $title;
$this->imageLink = $link;
return $this;
}
public function asXML(): \SimpleXMLElement
{
$image = new \SimpleXMLElement('<image />');
if ($this->imageUrl !== null) {
$image->addChild('url', $this->imageUrl);
}
if ($this->imageTitle !== null) {
$image->addChild('title', $this->imageTitle);
}
if ($this->imageLink !== null) {
$image->addChild('link', $this->imageLink);
}
$xml = parent::asXML();
$this->insertBefore($image, $xml->xpath('/channel/item')[0]);
return $xml;
}
private function insertBefore(\SimpleXMLElement $insert, \SimpleXMLElement $target): void
{
$target_dom = dom_import_simplexml($target);
$insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
$target_dom->parentNode->insertBefore($insert_dom, $target_dom);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment