1
0
mirror of https://github.com/Chouchen/ShikiryuRSS.git synced 2024-11-25 20:48:51 +01:00

Add specific elements

This commit is contained in:
Clement Desmidt 2023-04-12 14:41:45 +02:00
parent 4194420980
commit 08138992fd
9 changed files with 118 additions and 41 deletions

View File

@ -11,11 +11,11 @@ class Category extends HasValidator implements SRSSElement
/** /**
* @string * @string
*/ */
public string $domain; public ?string $domain = null;
/** /**
* @string * @string
*/ */
public string $value; public ?string $value = null;
public function isValid(): bool public function isValid(): bool
{ {

View File

@ -11,23 +11,23 @@ class Cloud extends HasValidator implements SRSSElement
/** /**
* @string * @string
*/ */
public string $domain; public ?string $domain = null;
/** /**
* @int * @int
*/ */
public int $port; public ?int $port = null;
/** /**
* @string * @string
*/ */
public string $path; public ?string $path = null;
/** /**
* @string * @string
*/ */
public string $registerProcedure; public ?string $registerProcedure = null;
/** /**
* @string * @string
*/ */
public string $protocol; public ?string $protocol = null;
//<cloud domain="rpc.sys.com" port="80" path="/RPC2" registerProcedure="myCloud.rssPleaseNotify" protocol="xml-rpc" /> //<cloud domain="rpc.sys.com" port="80" path="/RPC2" registerProcedure="myCloud.rssPleaseNotify" protocol="xml-rpc" />

View File

@ -12,17 +12,17 @@ class Image extends HasValidator implements SRSSElement
* @required * @required
* @url * @url
*/ */
public string $url; public ?string $url = null;
/** /**
* @required * @required
* @nohtml * @nohtml
*/ */
public string $title; public ?string $title = null;
/** /**
* @required * @required
* @url * @url
*/ */
public string $link; public ?string $link = null;
/** /**
* @int * @int
* @max 144 * @max 144

View File

@ -11,17 +11,17 @@ class Enclosure extends HasValidator implements SRSSElement
/** /**
* @url * @url
*/ */
public string $url; public ?string $url = null;
/** /**
* @int * @int
*/ */
public int $length; public ?int $length = null;
/** /**
* @mediaType * @mediaType
*/ */
public string $type; public ?string $type = null;
public function isValid(): bool public function isValid(): bool
{ {

View File

@ -11,12 +11,12 @@ class Source extends HasValidator implements SRSSElement
/** /**
* @url * @url
*/ */
public string $url; public ?string $url = null;
/** /**
* @nohtml * @nohtml
*/ */
public string $source; public ?string $value = null;
public function isValid(): bool public function isValid(): bool
{ {

View File

@ -19,22 +19,6 @@ class ItemParser extends DomDocument
protected DOMNode $node; // item node protected DOMNode $node; // item node
protected $attr; // item's properties protected $attr; // item's properties
// possible properties' names
protected static $possibilities = [
'title' => 'nohtml',
'link' => 'link',
'description' => 'html',
'author' => 'email',
'category' => 'nohtml',
'comments' => 'link',
'enclosure' => '',
'guid' => 'nohtml',
'pubDate' => 'date',
'source' => 'link',
'media:group' => 'folder',
'media:content' => '',
];
/** /**
* Constructor * Constructor
* *
@ -55,10 +39,30 @@ class ItemParser extends DomDocument
{ {
foreach ($nodes->childNodes as $child) { foreach ($nodes->childNodes as $child) {
if ($child->nodeType === XML_ELEMENT_NODE && $child->nodeName !== 'item') { if ($child->nodeType === XML_ELEMENT_NODE && $child->nodeName !== 'item') {
if (array_key_exists($child->nodeName, self::$possibilities) && self::$possibilities[$child->nodeName] === 'folder') { if ($child->nodeName === 'media:group') {
self::_loadChildAttributes($item, $child); self::_loadChildAttributes($item, $child);
} elseif ($child->nodeName === 'media:content') { } elseif ($child->nodeName === 'media:content') {
$item->medias[] = MediaContentParser::read($child); $item->medias[] = MediaContentParser::read($child);
} elseif ($child->nodeName === 'category') {
$category = new Item\Category();
foreach($child->attributes as $attribute) {
$category->{$attribute->name} = $attribute->value;
}
$category->value = $child->nodeValue;
$item->category[] = $category;
} elseif ($child->nodeName === 'enclosure') {
$enclosure = new Item\Enclosure();
foreach($child->attributes as $attribute) {
$enclosure->{$attribute->name} = $attribute->value;
}
$item->enclosure = $enclosure;
} elseif ($child->nodeName === 'source') {
$source = new Item\Source();
foreach($child->attributes as $attribute) {
$source->{$attribute->name} = $attribute->value;
}
$source->value = $child->nodeValue;
$item->source = $source;
} else { } else {
$item->{$child->nodeName} = trim($child->nodeValue); $item->{$child->nodeName} = trim($child->nodeValue);
} }

View File

@ -52,8 +52,8 @@ class SRSSParser extends DomDocument
if (@$this->load($link)) { // We don't want the warning in case of bad XML. Let's manage it with an exception. if (@$this->load($link)) { // We don't want the warning in case of bad XML. Let's manage it with an exception.
$channel = $this->getElementsByTagName('channel'); $channel = $this->getElementsByTagName('channel');
if($channel->length === 1){ // Good URL and good RSS if($channel->length === 1){ // Good URL and good RSS
$this->_loadAttributes(); // loading channel properties $this->_parseChannel(); // loading channel properties
$this->parseItems(); // loading all items $this->_parseItems(); // loading all items
return $this->doc; return $this->doc;
} }
@ -68,7 +68,7 @@ class SRSSParser extends DomDocument
* @return Item[] * @return Item[]
* @throws SRSSException * @throws SRSSException
*/ */
private function parseItems(): array private function _parseItems(): array
{ {
$channel = $this->_getChannel(); $channel = $this->_getChannel();
/** @var DOMNodeList $items */ /** @var DOMNodeList $items */
@ -87,7 +87,7 @@ class SRSSParser extends DomDocument
* putting all RSS attributes into the object * putting all RSS attributes into the object
* @throws SRSSException * @throws SRSSException
*/ */
private function _loadAttributes(): void private function _parseChannel(): void
{ {
$node_channel = $this->_getChannel(); $node_channel = $this->_getChannel();
$this->doc->channel = new Channel(); $this->doc->channel = new Channel();
@ -97,12 +97,25 @@ class SRSSParser extends DomDocument
if($child->nodeName === 'image') { if($child->nodeName === 'image') {
$image = new Image(); $image = new Image();
foreach($child->childNodes as $children) { foreach($child->childNodes as $children) {
if($children->nodeType == XML_ELEMENT_NODE) { if($children->nodeType === XML_ELEMENT_NODE) {
$image->{$child->nodeName} = $children->nodeValue; $image->{$children->nodeName} = $children->nodeValue;
} }
} }
$this->doc->channel->image = $image; $this->doc->channel->image = $image;
} elseif($child->nodeName === 'cloud') {
$cloud = new Channel\Cloud();
foreach($child->attributes as $attribute) {
$cloud->{$attribute->name} = $attribute->value;
}
$this->doc->channel->cloud = $cloud;
} elseif($child->nodeName === 'category') {
$category = new Channel\Category();
foreach($child->attributes as $attribute) {
$category->{$attribute->name} = $attribute->value;
}
$category->value = $child->nodeValue;
$this->doc->channel->category[] = $category;
} else { } else {
$this->doc->channel->{$child->nodeName} = $child->nodeValue; $this->doc->channel->{$child->nodeName} = $child->nodeValue;
} }

View File

@ -1,8 +1,10 @@
<?php <?php
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Shikiryu\SRSS\Exception\SRSSException; use Shikiryu\SRSS\Entity\Channel\Category;
use Shikiryu\SRSS\Exception\UnreadableRSSException; use Shikiryu\SRSS\Entity\Channel\Cloud;
use Shikiryu\SRSS\Entity\Channel\Image;
use Shikiryu\SRSS\Entity\Item;
use Shikiryu\SRSS\SRSS; use Shikiryu\SRSS\SRSS;
class BasicReaderTest extends TestCase class BasicReaderTest extends TestCase
@ -38,4 +40,60 @@ class BasicReaderTest extends TestCase
self::assertTrue($rss->isValid()); self::assertTrue($rss->isValid());
} }
public function testChannelImage()
{
$rss = SRSS::read(__DIR__.'/resources/media/cnn.xml');
$image = $rss->image;
self::assertInstanceOf(Image::class, $image);
self::assertEquals('http://i2.cdn.turner.com/cnn/2015/images/09/24/cnn.digital.png', $image->url, var_export($image, true));
self::assertEquals('CNN.com - RSS Channel - Entertainment', $image->title, var_export($image, true));
self::assertEquals('https://www.cnn.com/entertainment/index.html', $image->link, var_export($image, true));
}
public function testChannelCategory()
{
$rss = SRSS::read(__DIR__.'/resources/cloud.xml');
$categories = $rss->category;
self::assertCount(1, $categories);
$category = $categories[0];
self::assertInstanceOf(Category::class, $category);
self::assertEquals('http://www.weblogs.com/rssUpdates/changes.xml', $category->domain, var_export($category, true));
self::assertEquals('rssUpdates', $category->value, var_export($category, true));
}
public function testCloud()
{
$rss = SRSS::read(__DIR__.'/resources/cloud.xml');
$cloud = $rss->cloud;
self::assertInstanceOf(Cloud::class, $cloud);
self::assertEquals('radio.xmlstoragesystem.com', $cloud->domain, var_export($cloud, true));
self::assertEquals('80', $cloud->port, var_export($cloud, true));
self::assertEquals('/RPC2', $cloud->path, var_export($cloud, true));
self::assertEquals('xmlStorageSystem.rssPleaseNotify', $cloud->registerProcedure, var_export($cloud, true));
self::assertEquals('xml-rpc', $cloud->protocol, var_export($cloud, true));
}
public function testSource()
{
$rss = SRSS::read(__DIR__.'/resources/basic.xml');
$firstItem = $rss->getFirst();
self::assertInstanceOf(Item::class, $firstItem);
$source = $firstItem->source;
self::assertInstanceOf(Item\Source::class, $source);
self::assertEquals('http://www.tomalak.org/links2.xml', $source->url);
self::assertEquals('Tomalak\'s Realm', $source->value);
}
public function testEnclosure()
{
$rss = SRSS::read(__DIR__.'/resources/basic.xml');
$item = $rss->getItem(2);
self::assertInstanceOf(Item::class, $item);
$enclosure = $item->enclosure;
self::assertInstanceOf(Item\Enclosure::class, $enclosure);
self::assertEquals('http://www.scripting.com/mp3s/touchOfGrey.mp3', $enclosure->url);
self::assertEquals('5588242', $enclosure->length);
self::assertEquals('audio/mpeg', $enclosure->type);
}
} }

View File

@ -7,11 +7,13 @@
<description>Free web building tutorials</description> <description>Free web building tutorials</description>
<item> <item>
<title>RSS Tutorial</title> <title>RSS Tutorial</title>
<source url="http://www.tomalak.org/links2.xml">Tomalak's Realm</source>
<link>https://www.test.com/xml/xml_rss.asp</link> <link>https://www.test.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on test</description> <description>New RSS tutorial on test</description>
</item> </item>
<item> <item>
<title>XML Tutorial</title> <title>XML Tutorial</title>
<enclosure url="http://www.scripting.com/mp3s/touchOfGrey.mp3" length="5588242" type="audio/mpeg"/>
<link>https://www.test.com/xml</link> <link>https://www.test.com/xml</link>
<description>New XML tutorial on test</description> <description>New XML tutorial on test</description>
</item> </item>