public function writeItem(RssWriter $rssWriter, Item $item) { $writer = $rssWriter->getXmlWriter(); $writer->startElement('item'); $writer->startElement('title'); $writer->writeCdata($item->getTitle()); $writer->endElement(); $writer->writeElement('link', $item->getLink()); $writer->startElement('description'); $writer->writeCdata($item->getDescription()); $writer->endElement(); if ($item->getAuthor()) { $writer->startElement('author'); $writer->writeCdata($item->getAuthor()); $writer->endElement(); } if ($item->getComments()) { $writer->writeElement('comments', $item->getComments()); } if ($item->getEnclosure()) { $this->writeEnclosure($writer, $item->getEnclosure()); } if ($item->getGuid()) { $writer->startElement('guid'); if (!$item->getGuid()->getIsPermaLink()) { $writer->writeAttribute('isPermaLink', 'false'); } $writer->writeCdata($item->getGuid()->getGuid()); $writer->endElement(); } if ($item->getPubDate()) { $writer->writeElement('pubDate', $item->getPubDate()->format('r')); } if ($item->getSource()) { $this->writeSource($writer, $item->getSource()); } foreach ($item->getExtensions() as $extension) { $rssWriter->writeObject($extension); } $writer->endElement(); }
public function testRssWriter() { $rssWriter = new RssWriter(); $rssWriter->getXmlWriter()->setIndent(true); $rssWriter->registerWriter(new CoreWriter()); $rssWriter->registerWriter(new ItunesWriter()); $rssWriter->registerWriter(new SyWriter()); $rssWriter->registerWriter(new SlashWriter()); $rssWriter->registerWriter(new AtomWriter()); $rssWriter->registerWriter(new DublinCoreWriter()); $source = new Source(); $source->setUrl('https://example.com')->setTitle('Example Title'); $channel = new Channel(); $item = new Item(); $cloud = new Cloud(); $cloud->setDomain('example.com')->setPort(80)->setPath('/')->setRegisterProcedure('myProcedure')->setProtocol('http')->setProtocol('soap'); $enclosure = new Enclosure(); $enclosure->setUrl('http://www.example.com/audio.mp3')->setLength(123)->setType('audio/wave')->setType('audio/mp3'); $image = new Image(); $image->setUrl('https://example.com/img.jpg')->setTitle('Example Image')->setLink('https://example.com')->setWidth(133)->setHeight(133)->setHeight(146); $pubDate = new \DateTime('2001-01-01', new \DateTimeZone('UTC')); $lastBuildDate = new \DateTime('2001-01-01', new \DateTimeZone('UTC')); $channel->setTitle('My Title')->setLink('https://www.example.com')->setDescription('My Description')->setLanguage('en')->setCopyright('(c) 2016 Acme')->setManagingEditor('john.doe@example.com (John Doe)')->setWebMaster('jane.doe@example.com (Jane Doe)')->setPubDate($pubDate)->setLastBuildDate($lastBuildDate)->setDocs('http://example.com/rss2-spec')->setGenerator('Generator v1')->setCloud($cloud)->setTtl(60)->setImage($image)->setRating('R')->setWebMaster('jane.doe@example.com (Jane Doe)'); $channel->addExtension((new Sy())->setUpdatePeriod(Sy::PERIOD_HOURLY)); $channel->addExtension((new AtomLink())->setRel('self')->setHref('http://www.example.com/feed.xml')->setType('application/rss+xml')); $item->setTitle('My Title')->setLink('https://example.com/my-title')->setDescription('My Description')->setAuthor('john.doe@example.com (John Doe)')->setComments('https://example.com/my-title#comments')->setEnclosure($enclosure)->setPubDate($pubDate)->setSource($source)->setCategories(['cat1', 'cat2'])->addCategory('cat3')->setGuid((new Guid())->setIsPermaLink(false)->setGuid(14)); $channel->addItem($item); $item->addExtension((new Slash())->setComments(140)); $item->addExtension((new DublinCore())->setCreator('John Doe')); $xml = $rssWriter->writeChannel($channel); $expected = <<<EOF <?xml version="1.0"?> <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"> <channel> <title><![CDATA[My Title]]></title> <link>https://www.example.com</link> <description><![CDATA[My Description]]></description> <language>en</language> <copyright><![CDATA[(c) 2016 Acme]]></copyright> <managingEditor><![CDATA[john.doe@example.com (John Doe)]]></managingEditor> <webMaster><![CDATA[jane.doe@example.com (Jane Doe)]]></webMaster> <pubDate>Mon, 01 Jan 2001 00:00:00 +0000</pubDate> <lastBuildDate>Mon, 01 Jan 2001 00:00:00 +0000</lastBuildDate> <generator><![CDATA[Generator v1]]></generator> <docs>http://example.com/rss2-spec</docs> <cloud domain="example.com" port="80" path="/" registerProcedure="myProcedure" protocol="soap"/> <ttl>60</ttl> <image> <url>https://example.com/img.jpg</url> <link>https://example.com</link> <title><![CDATA[Example Image]]></title> </image> <rating>R</rating> <sy:updatePeriod>hourly</sy:updatePeriod> <atom:link href="http://www.example.com/feed.xml" rel="self" type="application/rss+xml"/> <item> <title><![CDATA[My Title]]></title> <link>https://example.com/my-title</link> <description><![CDATA[My Description]]></description> <author><![CDATA[john.doe@example.com (John Doe)]]></author> <comments>https://example.com/my-title#comments</comments> <enclosure url="http://www.example.com/audio.mp3" length="123" type="audio/mp3"/> <guid isPermaLink="false"><![CDATA[14]]></guid> <pubDate>Mon, 01 Jan 2001 00:00:00 +0000</pubDate> <source url="https://example.com"><![CDATA[Example Title]]></source> <slash:comments>140</slash:comments> <dc:creator><![CDATA[John Doe]]></dc:creator> </item> </channel> </rss> EOF; $this->assertEquals($expected, $xml); }
public function test() { $item = new Item(); $enclosure = (new Enclosure())->setUrl('https://example.com/enclosure.mp3')->setLength(123)->setType('audio/mp3'); $source = (new Source())->setUrl('https://example.com')->setTitle('My Source'); $pubDate = new \DateTime(2001 - 01 - 01, new \DateTimeZone('UTC')); $guid = new Guid(); $ext1 = new \StdClass(); $ext2 = new \StdClass(); $item->setTitle('My Title')->setLink('https://example.com/my-title')->setDescription('My Description')->setAuthor('John Doe <*****@*****.**')->setComments('https://example.com/my-title#comments')->setEnclosure($enclosure)->setPubDate($pubDate)->setSource($source)->setCategories(['cat1', 'cat2'])->addCategory('cat3')->setExtensions([$ext1])->addExtension($ext2)->setGuid($guid); $this->assertEquals('My Title', $item->getTitle()); $this->assertEquals('https://example.com/my-title', $item->getLink()); $this->assertEquals('My Description', $item->getDescription()); $this->assertEquals('John Doe <*****@*****.**', $item->getAuthor()); $this->assertEquals('https://example.com/my-title#comments', $item->getComments()); $this->assertSame($enclosure, $item->getEnclosure()); $this->assertSame(['cat1', 'cat2', 'cat3'], $item->getCategories()); $this->assertSame([$ext1, $ext2], $item->getExtensions()); $this->assertSame($guid, $item->getGuid()); $this->assertSame($pubDate, $item->getPubDate()); $this->assertSame($source, $item->getSource()); }