From 08138992fd3713ef046d8150f3da9beef299d0da Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Wed, 12 Apr 2023 14:41:45 +0200 Subject: [PATCH] :sparkles: Add specific elements --- src/Entity/Channel/Category.php | 4 +-- src/Entity/Channel/Cloud.php | 10 +++--- src/Entity/Channel/Image.php | 6 ++-- src/Entity/Item/Enclosure.php | 6 ++-- src/Entity/Item/Source.php | 4 +-- src/Parser/ItemParser.php | 38 +++++++++++--------- src/Parser/SRSSParser.php | 27 ++++++++++---- tests/BasicReaderTest.php | 62 +++++++++++++++++++++++++++++++-- tests/resources/basic.xml | 2 ++ 9 files changed, 118 insertions(+), 41 deletions(-) diff --git a/src/Entity/Channel/Category.php b/src/Entity/Channel/Category.php index acfc4f1..7bc4557 100644 --- a/src/Entity/Channel/Category.php +++ b/src/Entity/Channel/Category.php @@ -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 { diff --git a/src/Entity/Channel/Cloud.php b/src/Entity/Channel/Cloud.php index 95f991f..71b6875 100644 --- a/src/Entity/Channel/Cloud.php +++ b/src/Entity/Channel/Cloud.php @@ -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; // diff --git a/src/Entity/Channel/Image.php b/src/Entity/Channel/Image.php index e7f1757..b45b9d0 100644 --- a/src/Entity/Channel/Image.php +++ b/src/Entity/Channel/Image.php @@ -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 diff --git a/src/Entity/Item/Enclosure.php b/src/Entity/Item/Enclosure.php index 4590b23..1ec2169 100644 --- a/src/Entity/Item/Enclosure.php +++ b/src/Entity/Item/Enclosure.php @@ -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 { diff --git a/src/Entity/Item/Source.php b/src/Entity/Item/Source.php index ddbaf55..1d75ebc 100644 --- a/src/Entity/Item/Source.php +++ b/src/Entity/Item/Source.php @@ -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 { diff --git a/src/Parser/ItemParser.php b/src/Parser/ItemParser.php index e40f063..0fe7471 100644 --- a/src/Parser/ItemParser.php +++ b/src/Parser/ItemParser.php @@ -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); } diff --git a/src/Parser/SRSSParser.php b/src/Parser/SRSSParser.php index 10571c3..4d8b066 100644 --- a/src/Parser/SRSSParser.php +++ b/src/Parser/SRSSParser.php @@ -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; } diff --git a/tests/BasicReaderTest.php b/tests/BasicReaderTest.php index e6444f7..3660a01 100644 --- a/tests/BasicReaderTest.php +++ b/tests/BasicReaderTest.php @@ -1,8 +1,10 @@ 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); + } } \ No newline at end of file diff --git a/tests/resources/basic.xml b/tests/resources/basic.xml index 11dd2f7..d2329e1 100644 --- a/tests/resources/basic.xml +++ b/tests/resources/basic.xml @@ -7,11 +7,13 @@ Free web building tutorials RSS Tutorial + Tomalak's Realm https://www.test.com/xml/xml_rss.asp New RSS tutorial on test XML Tutorial + https://www.test.com/xml New XML tutorial on test