From e6b50f331dc51681af1c3e13878e08595eb29d69 Mon Sep 17 00:00:00 2001 From: Anatolii Koziura Date: Sun, 28 Feb 2021 15:02:52 +0200 Subject: [PATCH 1/2] Version attribute for RSS element added --- src/Feed.php | 2 +- src/RssElement.php | 22 ++++++++++++++++++++++ tests/feed/ProductTest.php | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/RssElement.php diff --git a/src/Feed.php b/src/Feed.php index c436bcb..9e92652 100644 --- a/src/Feed.php +++ b/src/Feed.php @@ -96,6 +96,6 @@ public function build() $xmlStructure['channel'][] = $item->getXmlStructure($namespace); } - return $xmlService->write('rss', $xmlStructure); + return $xmlService->write('rss', new RssElement($xmlStructure)); } } diff --git a/src/RssElement.php b/src/RssElement.php new file mode 100644 index 0000000..2a310b5 --- /dev/null +++ b/src/RssElement.php @@ -0,0 +1,22 @@ +value = $value; + $this->rss_version = (string)$rss_version; + } + + public function xmlSerialize(\Sabre\Xml\Writer $writer) + { + $writer->writeAttribute('version', $this->rss_version); + $writer->write($this->value); + } +} \ No newline at end of file diff --git a/tests/feed/ProductTest.php b/tests/feed/ProductTest.php index 061bbcc..f4bada8 100644 --- a/tests/feed/ProductTest.php +++ b/tests/feed/ProductTest.php @@ -117,7 +117,7 @@ public function testProductShipping() $feedXml = $feed->build(); $expectedFeedXml = ' - + My awesome store https://example.com From 227aa89252b8f3d3ed23be004361efbd4e3539c0 Mon Sep 17 00:00:00 2001 From: Anatolii Koziura Date: Mon, 1 Mar 2021 16:45:19 +0200 Subject: [PATCH 2/2] RSS version compatibility fixed --- src/Feed.php | 22 ++++++++- src/RssElement.php | 22 +++++++-- tests/feed/ProductTest.php | 91 +++++++++++++++++++++++++++++++++++++- 3 files changed, 128 insertions(+), 7 deletions(-) diff --git a/src/Feed.php b/src/Feed.php index 9e92652..ee00300 100644 --- a/src/Feed.php +++ b/src/Feed.php @@ -36,18 +36,27 @@ class Feed */ private $items = []; + /** + * Rss version attribute + * + * @var string + */ + private $rssVersion; + /** * Feed constructor. * * @param string $title * @param string $link * @param string $description + * @param string $rssVersion */ - public function __construct($title, $link, $description) + public function __construct($title, $link, $description, $rssVersion = "") { $this->title = $title; $this->link = $link; $this->description = $description; + $this->rssVersion = $rssVersion; } /** @@ -60,6 +69,15 @@ public function addProduct($product) $this->items[] = $product; } + /** + * Set + * @param $rssVersion + */ + public function setRssVersion($rssVersion) + { + $this->rssVersion = $rssVersion; + } + /** * Generate string representation of this feed. * @@ -96,6 +114,6 @@ public function build() $xmlStructure['channel'][] = $item->getXmlStructure($namespace); } - return $xmlService->write('rss', new RssElement($xmlStructure)); + return $xmlService->write('rss', new RssElement($xmlStructure, $this->rssVersion)); } } diff --git a/src/RssElement.php b/src/RssElement.php index 2a310b5..1a932bc 100644 --- a/src/RssElement.php +++ b/src/RssElement.php @@ -6,17 +6,31 @@ class RssElement implements \Sabre\Xml\XmlSerializable { private $value; - private $rss_version; - public function __construct($value, $rss_version = 2) + /** + * Rss version attribute + * @var string + */ + private $rssVersion; + + /** + * RssElement constructor. + * + * @param $value + * @param string $rssVersion + */ + public function __construct($value, $rssVersion = '') { $this->value = $value; - $this->rss_version = (string)$rss_version; + $this->rssVersion = (string)$rssVersion; } public function xmlSerialize(\Sabre\Xml\Writer $writer) { - $writer->writeAttribute('version', $this->rss_version); + if ($this->rssVersion) { + $writer->writeAttribute('version', $this->rssVersion); + } + $writer->write($this->value); } } \ No newline at end of file diff --git a/tests/feed/ProductTest.php b/tests/feed/ProductTest.php index f4bada8..0380320 100644 --- a/tests/feed/ProductTest.php +++ b/tests/feed/ProductTest.php @@ -117,7 +117,96 @@ public function testProductShipping() $feedXml = $feed->build(); $expectedFeedXml = ' - + + + My awesome store + https://example.com + My awesome description + + + US + CA, NSW, 03 + 94043 + 21137 + UPS Express + 1300 USD + + + + +'; + + $this->assertEquals($expectedFeedXml, $feedXml); + } + + /** + * Tests setting Id to product. + */ + public function testFeedSetRssVersionFromConstructor() + { + $shipping = new Shipping(); + $shipping->setCountry('US'); + $shipping->setRegion('CA, NSW, 03'); + $shipping->setPostalCode('94043'); + $shipping->setLocationId('21137'); + $shipping->setService('UPS Express'); + $shipping->setPrice('1300 USD'); + + $product = new Product(); + $product->setShipping($shipping); + + // Create feed object + $feed = new Feed("My awesome store", "https://example.com", "My awesome description", "2.0"); + $feed->addProduct($product); + + $feedXml = $feed->build(); + $expectedFeedXml = ' + + + My awesome store + https://example.com + My awesome description + + + US + CA, NSW, 03 + 94043 + 21137 + UPS Express + 1300 USD + + + + +'; + + $this->assertEquals($expectedFeedXml, $feedXml); + } + + /** + * Tests setting Id to product. + */ + public function testFeedSetRssVersionViaSetter() + { + $shipping = new Shipping(); + $shipping->setCountry('US'); + $shipping->setRegion('CA, NSW, 03'); + $shipping->setPostalCode('94043'); + $shipping->setLocationId('21137'); + $shipping->setService('UPS Express'); + $shipping->setPrice('1300 USD'); + + $product = new Product(); + $product->setShipping($shipping); + + // Create feed object + $feed = new Feed("My awesome store", "https://example.com", "My awesome description"); + $feed->setRssVersion("1.0"); + $feed->addProduct($product); + + $feedXml = $feed->build(); + $expectedFeedXml = ' + My awesome store https://example.com