Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Feed to support CSS style sheet specification #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/Suin/RSSWriter/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ class Feed implements FeedInterface
/** @var ChannelInterface[] */
protected $channels = [];

protected $feedStyles = [];

/**
* Add Style sheet
* @param string $style_url
* @param string $media (defaults to 'screen')
* @return $this
*/
public function addStyle($style_url, $media = 'screen'){
if( !empty($style_url) ){
$this->feedStyles[] = [ $style_url, $media ];
}

return $this;
}


/**
* Add channel
* @param ChannelInterface $channel
Expand All @@ -30,15 +47,28 @@ public function addChannel(ChannelInterface $channel)
*/
public function render()
{
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" />', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);

foreach ($this->channels as $channel) {
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:atom=\"http://www.w3.org/2005/Atom\" />",
LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);


foreach ($this->channels as $channel) {
$toDom = dom_import_simplexml($xml);
$fromDom = dom_import_simplexml($channel->asXML());
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}

$dom = new DOMDocument('1.0', 'UTF-8');

if( count($this->feedStyles) > 0 ){
foreach( $this->feedStyles as $info ){
$url = $info[0];
$media = $info[1];
$xslt = $dom->createProcessingInstruction('xml-stylesheet', "type=\"text/css\" media=\"{$media}\" href=\"{$url}\" ");
$dom->appendChild($xslt);
}
}

$dom->appendChild($dom->importNode(dom_import_simplexml($xml), true));
$dom->formatOutput = true;
return $dom->saveXML();
Expand Down
10 changes: 9 additions & 1 deletion src/Suin/RSSWriter/FeedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ interface FeedInterface
/**
* Add channel
* @param ChannelInterface $channel
* @return $thisJ
* @return $this
*/
public function addChannel(ChannelInterface $channel);

/**
* Add Style sheet
* @param string $style_url
* @param string $media (defaults to 'screen')
* @return $this
*/
public function addStyle($style_url, $media);

/**
* Render XML
* @return string
Expand Down