diff --git a/phpunit.xml b/phpunit.xml
index e340815..3edcd2e 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -8,8 +8,7 @@
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
- stopOnFailure = "false"
- syntaxCheck = "false">
+ stopOnFailure = "false">
@@ -17,12 +16,6 @@
-
-
- src/
-
-
-
diff --git a/src/Builder/SRSSBuilder.php b/src/Builder/SRSSBuilder.php
index 5a6f1ae..19ac73d 100644
--- a/src/Builder/SRSSBuilder.php
+++ b/src/Builder/SRSSBuilder.php
@@ -6,13 +6,12 @@ use DOMDocument;
use DOMElement;
use Shikiryu\SRSS\Entity\Channel;
use Shikiryu\SRSS\Entity\Item;
-use Shikiryu\SRSS\Parser\ItemParser;
use Shikiryu\SRSS\SRSS;
use Shikiryu\SRSS\SRSSTools;
class SRSSBuilder extends DomDocument
{
- public function build(SRSS $srss, string $filepath)
+ private function buildRSS(SRSS $srss)
{
$root = $this->createElement('rss');
$root->setAttribute('version', '2.0');
@@ -25,53 +24,27 @@ class SRSSBuilder extends DomDocument
$root->appendChild($channel);
$this->appendChild($root);
$this->encoding = 'UTF-8';
- $this->generator = 'Shikiryu RSS';
+ $srss->generator = 'Shikiryu RSS';
$this->formatOutput = true;
$this->preserveWhiteSpace = false;
// $docs = 'http://www.scriptol.fr/rss/RSS-2.0.html';
+ return $this;
+ }
+ public function build(SRSS $srss, string $filepath)
+ {
+ $this->buildRSS($srss);
+
$this->save($filepath);
}
/**
- * add a SRSS Item as an item into current RSS as first item
- *
- * @param ItemParser $item
+ * @return false|string
*/
- public function addItemBefore(ItemParser $item)
+ public function show(SRSS $srss)
{
- $node = $this->importNode($item->getItem(), true);
- $items = $this->getElementsByTagName('item');
- if($items->length != 0){
- $firstNode = $items->item(0);
- if($firstNode != null)
- $firstNode->parentNode->insertBefore($node, $firstNode);
- else
- $this->addItem($item);
- }
- else
- $this->addItem($item);
- }
+ $this->buildRSS($srss);
- /**
- * add a SRSS Item as an item into current RSS
- *
- * @param ItemParser $item
- */
- public function addItem(ItemParser $item)
- {
- $node = $this->importNode($item->getItem(), true);
- $channel = $this->_getChannel();
- $channel->appendChild($node);
- }
-
- /**
- * display XML
- * see DomDocument's docs
- */
- public function show()
- {
- // TODO build
return $this->saveXml();
}
diff --git a/src/SRSS.php b/src/SRSS.php
index 4eb353d..3e047ca 100644
--- a/src/SRSS.php
+++ b/src/SRSS.php
@@ -3,6 +3,7 @@
namespace Shikiryu\SRSS;
use Iterator;
+use Shikiryu\SRSS\Builder\SRSSBuilder;
use Shikiryu\SRSS\Entity\Channel;
use Shikiryu\SRSS\Entity\Item;
use Shikiryu\SRSS\Exception\SRSSException;
@@ -118,7 +119,7 @@ class SRSS implements Iterator
/**
* current from Iterator
*/
- public function current(): mixed
+ public function current(): Item
{
return $this->items[$this->position];
}
@@ -202,11 +203,49 @@ class SRSS implements Iterator
$doc['items'][] = $item->toArray();
}
- return $doc;
+ return array_filter($doc);
}
- public function addItem(Item $rssItem)
+ /**
+ * @param \Shikiryu\SRSS\Entity\Item $rssItem
+ *
+ * @return array|\Shikiryu\SRSS\Entity\Item[]
+ */
+ public function addItem(Item $rssItem): array
{
$this->items[] = $rssItem;
+
+ return $this->items;
}
+
+ /**
+ * @param \Shikiryu\SRSS\Entity\Item $firstItem
+ *
+ * @return array|\Shikiryu\SRSS\Entity\Item[]
+ */
+ public function addItemBefore(Item $firstItem): array
+ {
+ array_unshift($this->items, $firstItem);
+
+ return $this->items;
+ }
+
+ /**
+ * @param string $path
+ *
+ * @return void
+ */
+ public function save(string $path): void
+ {
+ (new SRSSBuilder('1.0', 'UTF-8'))->build($this, $path);
+ }
+
+ /**
+ * @return false|string
+ */
+ public function show()
+ {
+ return (new SRSSBuilder('1.0', 'UTF-8'))->show($this);
+ }
+
}
diff --git a/tests/BasicBuilderTest.php b/tests/BasicBuilderTest.php
index d3be8ed..b3a433c 100644
--- a/tests/BasicBuilderTest.php
+++ b/tests/BasicBuilderTest.php
@@ -8,6 +8,14 @@ use Shikiryu\SRSS\SRSSTools;
class BasicBuilderTest extends TestCase
{
+ private string $saved = __DIR__.'/resources/tmp/build/testCreateBasicRSS.rss';
+ protected function tearDown(): void
+ {
+ parent::tearDown();
+ if (file_exists($this->saved)) {
+ unlink($this->saved);
+ }
+ }
public function testCreateBasicRSS()
{
$srss = SRSS::create();
@@ -31,10 +39,11 @@ class BasicBuilderTest extends TestCase
self::assertTrue($srss->isValid());
- $filepath = __DIR__.'/resources/tmp/build/testCreateBasicRSS.rss';
$builder = new SRSSBuilder();
- $builder->build($srss, $filepath);
+ $builder->build($srss, $this->saved);
- self::assertFileExists($filepath);
+ self::assertFileExists($this->saved);
+
+ self::assertIsString($srss->show());
}
}
\ No newline at end of file
diff --git a/tests/OriginalReaderSRSSTest.php b/tests/OriginalReaderSRSSTest.php
new file mode 100644
index 0000000..d702121
--- /dev/null
+++ b/tests/OriginalReaderSRSSTest.php
@@ -0,0 +1,41 @@
+saved)) {
+ unlink($this->saved);
+ }
+ }
+
+ public function testOriginalReader()
+ {
+ $rss = SRSS::read($this->original);
+ self::assertEquals('Liftoff News', $rss->title);
+
+ $article1 = $rss->getItem(1);
+ $articleFirst = $rss->getFirst();
+ self::assertEquals($article1, $articleFirst);
+
+ $links = [];
+ foreach($rss as $article) {
+ $links[] = sprintf('%s %s', $article->link, SRSSTools::formatDate('d/m/y', $article->pubDate), $article->title);
+ }
+ self::assertCount(4, $links, var_export($links, true));
+
+ $rssArray = $rss->toArray();
+ self::assertCount(11, $rssArray, var_export($rssArray, true)); // 11 elements in RSS
+
+ $rss->save($this->saved);
+
+ self::assertFileExists($this->saved);
+ }
+}
\ No newline at end of file
diff --git a/tests/OriginalWriterSRSSTest.php b/tests/OriginalWriterSRSSTest.php
new file mode 100644
index 0000000..40b1f36
--- /dev/null
+++ b/tests/OriginalWriterSRSSTest.php
@@ -0,0 +1,45 @@
+title = 'My Awesome Blog';
+ $rss->link = 'http://shikiryu.com/devblog/';
+ $rss->description = 'is awesome';
+
+ $items = [
+ ['title' => 'title 1', 'link' => 'http://shikiryu.com/devblog/article-1', 'pubDate' => SRSSTools::getRSSDate('2012-03-05 12:02:01'), 'description' => 'description 1'],
+ ['title' => 'title 2', 'link' => 'http://shikiryu.com/devblog/article-2', 'pubDate' => SRSSTools::getRSSDate('2022-03-05 22:02:02'), 'description' => 'description 2'],
+ ['title' => 'title 3', 'link' => 'http://shikiryu.com/devblog/article-3', 'pubDate' => SRSSTools::getRSSDate('2032-03-05 32:02:03'), 'description' => 'description 3'],
+ ['title' => 'title 4', 'link' => 'http://shikiryu.com/devblog/article-4', 'pubDate' => SRSSTools::getRSSDate('2042-03-05 42:02:04'), 'description' => 'description 4'],
+ ];
+
+ foreach($items as $item){
+ $rssitem = new Item();
+ $rssitem->title = $item["title"];
+ $rssitem->link = $item['link'];
+ $rssitem->pubDate = $item["pubDate"];
+ $rssitem->description = $item["description"];
+ $rss->addItem($rssitem);
+ }
+
+ $firstItem = new Item();
+ $firstItem->title = 'title 0';
+ $firstItem->link = 'http://shikiryu.com/devblog/article-0';
+ $firstItem->pubDate = SRSSTools::getRSSDate('2011-03-05 12:02:01');
+ $firstItem->description = 'description 0';
+ $rss->addItemBefore($firstItem);
+
+ self::assertCount(5, $rss->items, var_export($rss->items, true));
+ self::assertEquals('title 0', $rss->getFirst()->title, var_export($rss->items, true));
+ self::assertEquals('title 1', $rss->getItem(2)->title, var_export($rss->items, true));
+
+ }
+}
\ No newline at end of file