From b5817dbfa0ec2238221291710e1cb79bf184bc9a Mon Sep 17 00:00:00 2001 From: Craig Meyer Date: Mon, 24 Jul 2017 12:49:17 -0400 Subject: [PATCH 1/3] Extend Feed to support CSS style sheet specification --- src/Suin/RSSWriter/Feed.php | 42 +++++++++++++++++++++++++--- src/Suin/RSSWriter/FeedInterface.php | 10 ++++++- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Suin/RSSWriter/Feed.php b/src/Suin/RSSWriter/Feed.php index e805049..21b575e 100644 --- a/src/Suin/RSSWriter/Feed.php +++ b/src/Suin/RSSWriter/Feed.php @@ -13,6 +13,24 @@ class Feed implements FeedInterface /** @var ChannelInterface[] */ protected $channels = []; + protected $feed_styles = []; + + /** + * 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) ){ + debug("Adding Style: $style_url"); + $this->feed_styles[] = [ $style_url, $media ]; + } + + return $this; + } + + /** * Add channel * @param ChannelInterface $channel @@ -28,17 +46,33 @@ public function addChannel(ChannelInterface $channel) * Render XML * @return string */ - public function render() - { - $xml = new SimpleXMLElement('', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL); + public function render(){ + + $xml = new SimpleXMLElement("", + LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL); + - foreach ($this->channels as $channel) { + foreach ($this->channels as $channel) { $toDom = dom_import_simplexml($xml); + debug("toDom: " . print_r($toDom, true) ); $fromDom = dom_import_simplexml($channel->asXML()); $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); } $dom = new DOMDocument('1.0', 'UTF-8'); + + if( count($this->feed_styles) > 0 ){ + foreach( $this->feed_styles as $info ){ + $url = $info[0]; + $media = $info[1]; + debug("Feed_Style: " . $url); + $xslt = $dom->createProcessingInstruction('xml-stylesheet', "type=\"text/css\" media=\"{$media}\" href=\"{$url}\" "); + $dom->appendChild($xslt); + } + } + + debug("XML: " . print_r(dom_import_simplexml($xml), true) ); + $dom->appendChild($dom->importNode(dom_import_simplexml($xml), true)); $dom->formatOutput = true; return $dom->saveXML(); diff --git a/src/Suin/RSSWriter/FeedInterface.php b/src/Suin/RSSWriter/FeedInterface.php index afaf518..d5f69ca 100644 --- a/src/Suin/RSSWriter/FeedInterface.php +++ b/src/Suin/RSSWriter/FeedInterface.php @@ -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 From 439cbee273adc132724191af84a929b65f8ebd0a Mon Sep 17 00:00:00 2001 From: Craig Meyer Date: Mon, 24 Jul 2017 22:31:23 -0400 Subject: [PATCH 2/3] Remove debug, and use camelCase naming style --- src/Suin/RSSWriter/Feed.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Suin/RSSWriter/Feed.php b/src/Suin/RSSWriter/Feed.php index 21b575e..0b65b83 100644 --- a/src/Suin/RSSWriter/Feed.php +++ b/src/Suin/RSSWriter/Feed.php @@ -13,7 +13,7 @@ class Feed implements FeedInterface /** @var ChannelInterface[] */ protected $channels = []; - protected $feed_styles = []; + protected $feedStyles = []; /** * Add Style sheet @@ -23,8 +23,7 @@ class Feed implements FeedInterface */ public function addStyle($style_url, $media = 'screen'){ if( !empty($style_url) ){ - debug("Adding Style: $style_url"); - $this->feed_styles[] = [ $style_url, $media ]; + $this->feedStyles[] = [ $style_url, $media ]; } return $this; @@ -54,25 +53,21 @@ public function render(){ foreach ($this->channels as $channel) { $toDom = dom_import_simplexml($xml); - debug("toDom: " . print_r($toDom, true) ); $fromDom = dom_import_simplexml($channel->asXML()); $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); } $dom = new DOMDocument('1.0', 'UTF-8'); - if( count($this->feed_styles) > 0 ){ - foreach( $this->feed_styles as $info ){ + if( count($this->feedStyles) > 0 ){ + foreach( $this->feedStyles as $info ){ $url = $info[0]; $media = $info[1]; - debug("Feed_Style: " . $url); $xslt = $dom->createProcessingInstruction('xml-stylesheet', "type=\"text/css\" media=\"{$media}\" href=\"{$url}\" "); $dom->appendChild($xslt); } } - debug("XML: " . print_r(dom_import_simplexml($xml), true) ); - $dom->appendChild($dom->importNode(dom_import_simplexml($xml), true)); $dom->formatOutput = true; return $dom->saveXML(); From 6fc6fa47d26a57c52a72275efd24c515b217b2f8 Mon Sep 17 00:00:00 2001 From: Craig Meyer Date: Mon, 24 Jul 2017 22:36:07 -0400 Subject: [PATCH 3/3] Fix { spacing on render --- src/Suin/RSSWriter/Feed.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Suin/RSSWriter/Feed.php b/src/Suin/RSSWriter/Feed.php index 0b65b83..649308d 100644 --- a/src/Suin/RSSWriter/Feed.php +++ b/src/Suin/RSSWriter/Feed.php @@ -45,7 +45,8 @@ public function addChannel(ChannelInterface $channel) * Render XML * @return string */ - public function render(){ + public function render() + { $xml = new SimpleXMLElement("", LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);