mirror of
https://github.com/Chouchen/ShikiryuRSS.git
synced 2024-12-22 04:41:37 +01:00
✨ Add specific elements
This commit is contained in:
parent
4194420980
commit
08138992fd
@ -11,11 +11,11 @@ class Category extends HasValidator implements SRSSElement
|
||||
/**
|
||||
* @string
|
||||
*/
|
||||
public string $domain;
|
||||
public ?string $domain = null;
|
||||
/**
|
||||
* @string
|
||||
*/
|
||||
public string $value;
|
||||
public ?string $value = null;
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
|
@ -11,23 +11,23 @@ class Cloud extends HasValidator implements SRSSElement
|
||||
/**
|
||||
* @string
|
||||
*/
|
||||
public string $domain;
|
||||
public ?string $domain = null;
|
||||
/**
|
||||
* @int
|
||||
*/
|
||||
public int $port;
|
||||
public ?int $port = null;
|
||||
/**
|
||||
* @string
|
||||
*/
|
||||
public string $path;
|
||||
public ?string $path = null;
|
||||
/**
|
||||
* @string
|
||||
*/
|
||||
public string $registerProcedure;
|
||||
public ?string $registerProcedure = null;
|
||||
/**
|
||||
* @string
|
||||
*/
|
||||
public string $protocol;
|
||||
public ?string $protocol = null;
|
||||
|
||||
//<cloud domain="rpc.sys.com" port="80" path="/RPC2" registerProcedure="myCloud.rssPleaseNotify" protocol="xml-rpc" />
|
||||
|
||||
|
@ -12,17 +12,17 @@ class Image extends HasValidator implements SRSSElement
|
||||
* @required
|
||||
* @url
|
||||
*/
|
||||
public string $url;
|
||||
public ?string $url = null;
|
||||
/**
|
||||
* @required
|
||||
* @nohtml
|
||||
*/
|
||||
public string $title;
|
||||
public ?string $title = null;
|
||||
/**
|
||||
* @required
|
||||
* @url
|
||||
*/
|
||||
public string $link;
|
||||
public ?string $link = null;
|
||||
/**
|
||||
* @int
|
||||
* @max 144
|
||||
|
@ -11,17 +11,17 @@ class Enclosure extends HasValidator implements SRSSElement
|
||||
/**
|
||||
* @url
|
||||
*/
|
||||
public string $url;
|
||||
public ?string $url = null;
|
||||
|
||||
/**
|
||||
* @int
|
||||
*/
|
||||
public int $length;
|
||||
public ?int $length = null;
|
||||
|
||||
/**
|
||||
* @mediaType
|
||||
*/
|
||||
public string $type;
|
||||
public ?string $type = null;
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
|
@ -11,12 +11,12 @@ class Source extends HasValidator implements SRSSElement
|
||||
/**
|
||||
* @url
|
||||
*/
|
||||
public string $url;
|
||||
public ?string $url = null;
|
||||
|
||||
/**
|
||||
* @nohtml
|
||||
*/
|
||||
public string $source;
|
||||
public ?string $value = null;
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
|
@ -19,22 +19,6 @@ class ItemParser extends DomDocument
|
||||
protected DOMNode $node; // item node
|
||||
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
|
||||
*
|
||||
@ -55,10 +39,30 @@ class ItemParser extends DomDocument
|
||||
{
|
||||
foreach ($nodes->childNodes as $child) {
|
||||
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);
|
||||
} elseif ($child->nodeName === 'media:content') {
|
||||
$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 {
|
||||
$item->{$child->nodeName} = trim($child->nodeValue);
|
||||
}
|
||||
|
@ -49,11 +49,11 @@ class SRSSParser extends DomDocument
|
||||
*/
|
||||
public function parse(string $link): SRSS
|
||||
{
|
||||
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');
|
||||
if($channel->length === 1){ // Good URL and good RSS
|
||||
$this->_loadAttributes(); // loading channel properties
|
||||
$this->parseItems(); // loading all items
|
||||
$this->_parseChannel(); // loading channel properties
|
||||
$this->_parseItems(); // loading all items
|
||||
|
||||
return $this->doc;
|
||||
}
|
||||
@ -68,7 +68,7 @@ class SRSSParser extends DomDocument
|
||||
* @return Item[]
|
||||
* @throws SRSSException
|
||||
*/
|
||||
private function parseItems(): array
|
||||
private function _parseItems(): array
|
||||
{
|
||||
$channel = $this->_getChannel();
|
||||
/** @var DOMNodeList $items */
|
||||
@ -87,7 +87,7 @@ class SRSSParser extends DomDocument
|
||||
* putting all RSS attributes into the object
|
||||
* @throws SRSSException
|
||||
*/
|
||||
private function _loadAttributes(): void
|
||||
private function _parseChannel(): void
|
||||
{
|
||||
$node_channel = $this->_getChannel();
|
||||
$this->doc->channel = new Channel();
|
||||
@ -97,12 +97,25 @@ class SRSSParser extends DomDocument
|
||||
if($child->nodeName === 'image') {
|
||||
$image = new Image();
|
||||
foreach($child->childNodes as $children) {
|
||||
if($children->nodeType == XML_ELEMENT_NODE) {
|
||||
$image->{$child->nodeName} = $children->nodeValue;
|
||||
if($children->nodeType === XML_ELEMENT_NODE) {
|
||||
$image->{$children->nodeName} = $children->nodeValue;
|
||||
}
|
||||
}
|
||||
$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 {
|
||||
$this->doc->channel->{$child->nodeName} = $child->nodeValue;
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shikiryu\SRSS\Exception\SRSSException;
|
||||
use Shikiryu\SRSS\Exception\UnreadableRSSException;
|
||||
use Shikiryu\SRSS\Entity\Channel\Category;
|
||||
use Shikiryu\SRSS\Entity\Channel\Cloud;
|
||||
use Shikiryu\SRSS\Entity\Channel\Image;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\SRSS;
|
||||
|
||||
class BasicReaderTest extends TestCase
|
||||
@ -38,4 +40,60 @@ class BasicReaderTest extends TestCase
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -7,11 +7,13 @@
|
||||
<description>Free web building tutorials</description>
|
||||
<item>
|
||||
<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>
|
||||
<description>New RSS tutorial on test</description>
|
||||
</item>
|
||||
<item>
|
||||
<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>
|
||||
<description>New XML tutorial on test</description>
|
||||
</item>
|
||||
|
Loading…
Reference in New Issue
Block a user