mirror of
https://github.com/Chouchen/ShikiryuRSS.git
synced 2025-09-05 13:04:32 +02:00
✅ Add original cases
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
45
src/SRSS.php
45
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user