Skip to content

Commit

Permalink
Merge pull request #13 from ololower/rss-version
Browse files Browse the repository at this point in the history
Version attribute for RSS element added
  • Loading branch information
vitalybaev authored Mar 1, 2021
2 parents 789615f + 227aa89 commit fd5e5a0
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -96,6 +114,6 @@ public function build()
$xmlStructure['channel'][] = $item->getXmlStructure($namespace);
}

return $xmlService->write('rss', $xmlStructure);
return $xmlService->write('rss', new RssElement($xmlStructure, $this->rssVersion));
}
}
36 changes: 36 additions & 0 deletions src/RssElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Vitalybaev\GoogleMerchant;

class RssElement implements \Sabre\Xml\XmlSerializable
{

private $value;

/**
* Rss version attribute
* @var string
*/
private $rssVersion;

/**
* RssElement constructor.
*
* @param $value
* @param string $rssVersion
*/
public function __construct($value, $rssVersion = '')
{
$this->value = $value;
$this->rssVersion = (string)$rssVersion;
}

public function xmlSerialize(\Sabre\Xml\Writer $writer)
{
if ($this->rssVersion) {
$writer->writeAttribute('version', $this->rssVersion);
}

$writer->write($this->value);
}
}
89 changes: 89 additions & 0 deletions tests/feed/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,95 @@ public function testProductShipping()
</item>
</channel>
</rss>
';

$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 = '<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>My awesome store</title>
<link>https://example.com</link>
<description>My awesome description</description>
<item>
<g:shipping>
<g:country>US</g:country>
<g:region>CA, NSW, 03</g:region>
<g:postal_code>94043</g:postal_code>
<g:location_id>21137</g:location_id>
<g:service>UPS Express</g:service>
<g:price>1300 USD</g:price>
</g:shipping>
</item>
</channel>
</rss>
';

$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 = '<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="1.0">
<channel>
<title>My awesome store</title>
<link>https://example.com</link>
<description>My awesome description</description>
<item>
<g:shipping>
<g:country>US</g:country>
<g:region>CA, NSW, 03</g:region>
<g:postal_code>94043</g:postal_code>
<g:location_id>21137</g:location_id>
<g:service>UPS Express</g:service>
<g:price>1300 USD</g:price>
</g:shipping>
</item>
</channel>
</rss>
';

$this->assertEquals($expectedFeedXml, $feedXml);
Expand Down

0 comments on commit fd5e5a0

Please sign in to comment.