Skip to content

Commit c534755

Browse files
committed
Add support for "enclosure" element for Items, allowing the module to be used for Podcasts.
1 parent 325d231 commit c534755

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
`\Suin\RSSWriter` is yet another simple RSS writer library for PHP 5.3 or later. This component is Licensed under MIT license.
44

5+
This library can also be used to publish Podcasts.
6+
57
The build status of the current master branch is tracked by Travis CI: [![Build Status](https://secure.travis-ci.org/suin/php-rss-writer.png?branch=master)](http://travis-ci.org/suin/php-rss-writer)
68

79

@@ -18,13 +20,23 @@ $channel
1820
->url('http://blog.example.com')
1921
->appendTo($feed);
2022

23+
// RSS item
2124
$item = new Item();
2225
$item
2326
->title("Blog Entry Title")
2427
->description("<div>Blog body</div>")
2528
->url('http://blog.example.com/2012/08/21/blog-entry/')
2629
->appendTo($channel);
2730

31+
// Podcast item
32+
$item = new Item();
33+
$item
34+
->title("Some Podcast Entry")
35+
->description("<div>Podcast body</div>")
36+
->url('http://podcast.example.com/2012/08/21/podcast-entry/')
37+
->enclosure('http://link-to-audio-file.com/2013/08/21/podcast.mp3', 4889, 'audio/mpeg')
38+
->appendTo($channel);
39+
2840

2941
echo $feed;
3042
```
@@ -81,4 +93,4 @@ If you want to know APIs, please see `FeedInterface`, `ChannelInterface` and `It
8193

8294
## License
8395

84-
MIT license
96+
MIT license

Source/Suin/RSSWriter/Item.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Item implements \Suin\RSSWriter\ItemInterface
2020
protected $isPermalink;
2121
/** @var int */
2222
protected $pubDate;
23+
/** @var array */
24+
protected $enclosure;
2325

2426
/**
2527
* Set item title
@@ -90,6 +92,19 @@ public function pubDate($pubDate)
9092
return $this;
9193
}
9294

95+
/**
96+
* Set enclosure
97+
* @param var $url Url to media file
98+
* @param int $length Length in bytes of the media file
99+
* @param var $type Media type, default is audio/mpeg
100+
* @return $this
101+
*/
102+
public function enclosure($url, $length = 0, $type = 'audio/mpeg')
103+
{
104+
$this->enclosure = array('url' => $url, 'length' => $length, 'type' => $type);
105+
return $this;
106+
}
107+
93108
/**
94109
* Append item to the channel
95110
* @param \Suin\RSSWriter\ChannelInterface $channel
@@ -137,6 +152,18 @@ public function asXML()
137152
$xml->addChild('pubDate', date(DATE_RSS, $this->pubDate));
138153
}
139154

155+
156+
if (is_array($this->enclosure) && (count($this->enclosure) == 3))
157+
{
158+
$element = $xml->addChild('enclosure');
159+
$element->addAttribute('url', $this->enclosure['url']);
160+
$element->addAttribute('type', $this->enclosure['type']);
161+
162+
if ($this->enclosure['length'])
163+
{
164+
$element->addAttribute('length', $this->enclosure['length']);
165+
}
166+
}
140167
return $xml;
141168
}
142169
}

Source/Suin/RSSWriter/ItemInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public function guid($guid, $isPermalink = false);
5151
*/
5252
public function pubDate($pubDate);
5353

54+
/**
55+
* Set enclosure
56+
* @param var $url Url to media file
57+
* @param int $length Length in bytes of the media file
58+
* @param var $type Media type, default is audio/mpeg
59+
* @return $this
60+
*/
61+
public function enclosure($url, $length = 0, $type = 'audio/mpeg');
62+
5463
/**
5564
* Append item to the channel
5665
* @param \Suin\RSSWriter\ChannelInterface $channel

Tests/Suin/RSSWriter/ItemTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ public function testAppendTo()
8888
$this->assertSame($item, $item->appendTo($channel));
8989
}
9090

91+
public function testEnclosure()
92+
{
93+
$url = uniqid();
94+
$enclosure = array('url' => $url, 'length' => 0, 'type' => 'audio/mpeg');
95+
$item = new Item();
96+
$this->assertSame($item, $item->enclosure($url));
97+
$this->assertAttributeSame($enclosure, 'enclosure', $item);
98+
}
99+
91100
public function testAsXML()
92101
{
93102
$now = time();
@@ -104,6 +113,10 @@ public function testAsXML()
104113
'guid' => "http://inessential.com/2002/09/01.php#a2",
105114
'isPermalink' => true,
106115
'pubDate' => $now,
116+
'enclosure' => array(
117+
'url' => 'http://link-to-audio-file.com/test.mp3',
118+
'length' => 4992,
119+
'type' => 'audio/mpeg')
107120
);
108121

109122
$item = new Item();
@@ -122,6 +135,7 @@ public function testAsXML()
122135
<category domain=\"{$data['categories'][1][1]}\">{$data['categories'][1][0]}</category>
123136
<guid isPermaLink=\"true\">{$data['guid']}</guid>
124137
<pubDate>{$nowString}</pubDate>
138+
<enclosure url=\"{$data['enclosure']['url']}\" length=\"{$data['enclosure']['length']}\" type=\"{$data['enclosure']['type']}\"/>
125139
</item>
126140
";
127141
$this->assertXmlStringEqualsXmlString($expect, $item->asXML()->asXML());
@@ -133,7 +147,7 @@ public function testAsXML_test_Japanese()
133147
$nowString = date(DATE_RSS, $now);
134148

135149
$data = array(
136-
'title' => "日本語",
150+
'title' => "Venice Film Festival",
137151
'url' => 'http://nytimes.com/2004/12/07FEST.html',
138152
'description' => "Some of the most heated chatter at the Venice Film Festival this week was about the way that the arrival of the stars at the Palazzo del Cinema was being staged.",
139153
);

0 commit comments

Comments
 (0)