Skip to content

Commit a7dd2bb

Browse files
committed
Support <content:encoded>
Signed-off-by: Hidehito Nozawa <suinyeze@gmail.com>
1 parent 893b689 commit a7dd2bb

File tree

7 files changed

+67
-12
lines changed

7 files changed

+67
-12
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ $channel
3333
$item = new Item();
3434
$item
3535
->title('Blog Entry Title')
36-
->description('<div>Blog body</div>')
36+
->description('Blog body')
37+
->contentEncoded('<div>Blog body</div>')
3738
->url('http://blog.example.com/2012/08/21/blog-entry/')
3839
->author('Hidehito Nozawa')
3940
->pubDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))
@@ -56,22 +57,23 @@ Output:
5657

5758
```xml
5859
<?xml version="1.0" encoding="UTF-8"?>
59-
<rss version="2.0">
60+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
6061
<channel>
6162
<title>Channel Title</title>
6263
<link>http://blog.example.com</link>
6364
<description>Channel Description</description>
6465
<language>en-US</language>
6566
<copyright>Copyright 2012, Foo Bar</copyright>
66-
<pubDate>Tue, 21 Aug 2012 19:50:37 +0900</pubDate>
67-
<lastBuildDate>Tue, 21 Aug 2012 19:50:37 +0900</lastBuildDate>
67+
<pubDate>Tue, 21 Aug 2012 10:50:37 +0000</pubDate>
68+
<lastBuildDate>Tue, 21 Aug 2012 10:50:37 +0000</lastBuildDate>
6869
<ttl>60</ttl>
69-
<item>
70+
<item xmlns:default="http://purl.org/rss/1.0/modules/content/">
7071
<title>Blog Entry Title</title>
7172
<link>http://blog.example.com/2012/08/21/blog-entry/</link>
72-
<description>&lt;div&gt;Blog body&lt;/div&gt;</description>
73+
<description>Blog body</description>
74+
<content:encoded xmlns="http://purl.org/rss/1.0/modules/content/"><![CDATA[<div>Blog body</div>]]></content:encoded>
7375
<guid>http://blog.example.com/2012/08/21/blog-entry/</guid>
74-
<pubDate>Tue, 21 Aug 2012 19:50:37 +0900</pubDate>
76+
<pubDate>Tue, 21 Aug 2012 10:50:37 +0000</pubDate>
7577
<author>Hidehito Nozawa</author>
7678
</item>
7779
<item>

examples/simple-feed.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
$item = new Item();
2929
$item
3030
->title('Blog Entry Title')
31-
->description('<div>Blog body</div>')
31+
->description('Blog body')
32+
->contentEncoded('<div>Blog body</div>')
3233
->url('http://blog.example.com/2012/08/21/blog-entry/')
3334
->author('Hidehito Nozawa')
3435
->pubDate(strtotime('Tue, 21 Aug 2012 19:50:37 +0900'))

src/Suin/RSSWriter/Feed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function addChannel(ChannelInterface $channel)
3030
*/
3131
public function render()
3232
{
33-
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" />', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
33+
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" />', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
3434

3535
foreach ($this->channels as $channel) {
3636
$toDom = dom_import_simplexml($xml);

src/Suin/RSSWriter/Item.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class Item implements ItemInterface
1717
/** @var string */
1818
protected $description;
1919

20+
/** @var string */
21+
protected $contentEncoded;
22+
2023
/** @var array */
2124
protected $categories = [];
2225

@@ -53,6 +56,12 @@ public function description($description)
5356
return $this;
5457
}
5558

59+
public function contentEncoded($content)
60+
{
61+
$this->contentEncoded = $content;
62+
return $this;
63+
}
64+
5665
public function category($name, $domain = null)
5766
{
5867
$this->categories[] = [$name, $domain];
@@ -97,6 +106,14 @@ public function asXML()
97106
$xml->addChild('link', $this->url);
98107
$xml->addChild('description', $this->description);
99108

109+
if ($this->contentEncoded) {
110+
// SimpleXMLElement does not support CDATA transformation
111+
$element = $xml->addChild('encoded', null, 'http://purl.org/rss/1.0/modules/content/');
112+
$element = dom_import_simplexml($element);
113+
$elementOwner = $element->ownerDocument;
114+
$element->appendChild($elementOwner->createCDATASection($this->contentEncoded));
115+
}
116+
100117
foreach ($this->categories as $category) {
101118
$element = $xml->addChild('category', $category[0]);
102119

src/Suin/RSSWriter/ItemInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public function url($url);
2929
*/
3030
public function description($description);
3131

32+
/**
33+
* Set content:encoded
34+
* @param string $content
35+
* @return $this
36+
*/
37+
public function contentEncoded($content);
38+
3239
/**
3340
* Set item category
3441
* @param string $name Category name

tests/Suin/RSSWriter/FeedTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testRender()
3030
$channel3->expects($this->once())->method('asXML')->will($this->returnValue($xml3));
3131
$this->reveal($feed)->attr('channels', [$channel1, $channel2, $channel3]);
3232
$expect = '<?xml version="1.0" encoding="UTF-8" ?>
33-
<rss version="2.0">
33+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
3434
<channel><title>channel1</title></channel>
3535
<channel><title>channel2</title></channel>
3636
<channel><title>channel3</title></channel>
@@ -54,7 +54,7 @@ public function testRender_with_japanese()
5454
$this->reveal($feed)->attr('channels', [$channel1, $channel2, $channel3]);
5555
$expect = <<< 'XML'
5656
<?xml version="1.0" encoding="UTF-8"?>
57-
<rss version="2.0">
57+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
5858
<channel>
5959
<title>日本語1</title>
6060
</channel>
@@ -85,7 +85,7 @@ public function test__toString()
8585
$channel3->expects($this->once())->method('asXML')->will($this->returnValue($xml3));
8686
$this->reveal($feed)->attr('channels', [$channel1, $channel2, $channel3]);
8787
$expect = '<?xml version="1.0" encoding="UTF-8" ?>
88-
<rss version="2.0">
88+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
8989
<channel><title>channel1</title></channel>
9090
<channel><title>channel2</title></channel>
9191
<channel><title>channel3</title></channel>

tests/Suin/RSSWriter/ItemTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,34 @@ public function testDescription()
3232
$this->assertAttributeSame($description, 'description', $item);
3333
}
3434

35+
public function testContentEncoded()
36+
{
37+
$item = new Item();
38+
$this->assertSame($item, $item->contentEncoded('<div>contents</div>'));
39+
$this->assertAttributeSame('<div>contents</div>', 'contentEncoded', $item);
40+
41+
$feed = new Feed();
42+
$channel = new Channel();
43+
$item->appendTo($channel);
44+
$channel->appendTo($feed);
45+
46+
$expected = '<?xml version="1.0" encoding="UTF-8"?>
47+
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
48+
<channel>
49+
<title/>
50+
<link/>
51+
<description/>
52+
<item xmlns:default="http://purl.org/rss/1.0/modules/content/">
53+
<title/>
54+
<link/>
55+
<description/>
56+
<content:encoded xmlns="http://purl.org/rss/1.0/modules/content/"><![CDATA[<div>contents</div>]]></content:encoded>
57+
</item>
58+
</channel>
59+
</rss>';
60+
$this->assertXmlStringEqualsXmlString($expected, $feed->render());
61+
}
62+
3563
public function testCategory()
3664
{
3765
$category = uniqid();

0 commit comments

Comments
 (0)