1
0
mirror of https://github.com/Chouchen/ShikiryuRSS.git synced 2025-09-05 13:04:32 +02:00

Add specific elements

This commit is contained in:
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
*/
public string $domain;
public ?string $domain = null;
/**
* @string
*/
public string $value;
public ?string $value = null;
public function isValid(): bool
{

View File

@@ -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" />

View File

@@ -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

View File

@@ -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
{

View File

@@ -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
{

View File

@@ -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);
}

View File

@@ -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;
}