Skip to content

Commit fd5e5a0

Browse files
authored
Merge pull request #13 from ololower/rss-version
Version attribute for RSS element added
2 parents 789615f + 227aa89 commit fd5e5a0

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

src/Feed.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,27 @@ class Feed
3636
*/
3737
private $items = [];
3838

39+
/**
40+
* Rss version attribute
41+
*
42+
* @var string
43+
*/
44+
private $rssVersion;
45+
3946
/**
4047
* Feed constructor.
4148
*
4249
* @param string $title
4350
* @param string $link
4451
* @param string $description
52+
* @param string $rssVersion
4553
*/
46-
public function __construct($title, $link, $description)
54+
public function __construct($title, $link, $description, $rssVersion = "")
4755
{
4856
$this->title = $title;
4957
$this->link = $link;
5058
$this->description = $description;
59+
$this->rssVersion = $rssVersion;
5160
}
5261

5362
/**
@@ -60,6 +69,15 @@ public function addProduct($product)
6069
$this->items[] = $product;
6170
}
6271

72+
/**
73+
* Set
74+
* @param $rssVersion
75+
*/
76+
public function setRssVersion($rssVersion)
77+
{
78+
$this->rssVersion = $rssVersion;
79+
}
80+
6381
/**
6482
* Generate string representation of this feed.
6583
*
@@ -96,6 +114,6 @@ public function build()
96114
$xmlStructure['channel'][] = $item->getXmlStructure($namespace);
97115
}
98116

99-
return $xmlService->write('rss', $xmlStructure);
117+
return $xmlService->write('rss', new RssElement($xmlStructure, $this->rssVersion));
100118
}
101119
}

src/RssElement.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Vitalybaev\GoogleMerchant;
4+
5+
class RssElement implements \Sabre\Xml\XmlSerializable
6+
{
7+
8+
private $value;
9+
10+
/**
11+
* Rss version attribute
12+
* @var string
13+
*/
14+
private $rssVersion;
15+
16+
/**
17+
* RssElement constructor.
18+
*
19+
* @param $value
20+
* @param string $rssVersion
21+
*/
22+
public function __construct($value, $rssVersion = '')
23+
{
24+
$this->value = $value;
25+
$this->rssVersion = (string)$rssVersion;
26+
}
27+
28+
public function xmlSerialize(\Sabre\Xml\Writer $writer)
29+
{
30+
if ($this->rssVersion) {
31+
$writer->writeAttribute('version', $this->rssVersion);
32+
}
33+
34+
$writer->write($this->value);
35+
}
36+
}

tests/feed/ProductTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,95 @@ public function testProductShipping()
134134
</item>
135135
</channel>
136136
</rss>
137+
';
138+
139+
$this->assertEquals($expectedFeedXml, $feedXml);
140+
}
141+
142+
/**
143+
* Tests setting Id to product.
144+
*/
145+
public function testFeedSetRssVersionFromConstructor()
146+
{
147+
$shipping = new Shipping();
148+
$shipping->setCountry('US');
149+
$shipping->setRegion('CA, NSW, 03');
150+
$shipping->setPostalCode('94043');
151+
$shipping->setLocationId('21137');
152+
$shipping->setService('UPS Express');
153+
$shipping->setPrice('1300 USD');
154+
155+
$product = new Product();
156+
$product->setShipping($shipping);
157+
158+
// Create feed object
159+
$feed = new Feed("My awesome store", "https://example.com", "My awesome description", "2.0");
160+
$feed->addProduct($product);
161+
162+
$feedXml = $feed->build();
163+
$expectedFeedXml = '<?xml version="1.0"?>
164+
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
165+
<channel>
166+
<title>My awesome store</title>
167+
<link>https://example.com</link>
168+
<description>My awesome description</description>
169+
<item>
170+
<g:shipping>
171+
<g:country>US</g:country>
172+
<g:region>CA, NSW, 03</g:region>
173+
<g:postal_code>94043</g:postal_code>
174+
<g:location_id>21137</g:location_id>
175+
<g:service>UPS Express</g:service>
176+
<g:price>1300 USD</g:price>
177+
</g:shipping>
178+
</item>
179+
</channel>
180+
</rss>
181+
';
182+
183+
$this->assertEquals($expectedFeedXml, $feedXml);
184+
}
185+
186+
/**
187+
* Tests setting Id to product.
188+
*/
189+
public function testFeedSetRssVersionViaSetter()
190+
{
191+
$shipping = new Shipping();
192+
$shipping->setCountry('US');
193+
$shipping->setRegion('CA, NSW, 03');
194+
$shipping->setPostalCode('94043');
195+
$shipping->setLocationId('21137');
196+
$shipping->setService('UPS Express');
197+
$shipping->setPrice('1300 USD');
198+
199+
$product = new Product();
200+
$product->setShipping($shipping);
201+
202+
// Create feed object
203+
$feed = new Feed("My awesome store", "https://example.com", "My awesome description");
204+
$feed->setRssVersion("1.0");
205+
$feed->addProduct($product);
206+
207+
$feedXml = $feed->build();
208+
$expectedFeedXml = '<?xml version="1.0"?>
209+
<rss xmlns:g="http://base.google.com/ns/1.0" version="1.0">
210+
<channel>
211+
<title>My awesome store</title>
212+
<link>https://example.com</link>
213+
<description>My awesome description</description>
214+
<item>
215+
<g:shipping>
216+
<g:country>US</g:country>
217+
<g:region>CA, NSW, 03</g:region>
218+
<g:postal_code>94043</g:postal_code>
219+
<g:location_id>21137</g:location_id>
220+
<g:service>UPS Express</g:service>
221+
<g:price>1300 USD</g:price>
222+
</g:shipping>
223+
</item>
224+
</channel>
225+
</rss>
137226
';
138227

139228
$this->assertEquals($expectedFeedXml, $feedXml);

0 commit comments

Comments
 (0)