From f1816dec0c0ffed4766932e776b6fc042b55cb73 Mon Sep 17 00:00:00 2001 From: Shikiryu Date: Tue, 11 Apr 2023 23:49:03 +0200 Subject: [PATCH] :construction: Add some validations and corrections --- README.md | 6 +++--- src/Builder/SRSSBuilder.php | 3 ++- src/Entity/Channel/Image.php | 6 +++++- src/Entity/Item.php | 9 +++++++-- src/Entity/Media/Content.php | 6 +++++- src/Parser/ItemParser.php | 2 +- src/Parser/SRSSParser.php | 15 ++++++++------- src/SRSS.php | 17 ++++++++++------- src/SRSSTools.php | 3 ++- src/Validator/Validator.php | 7 +++++-- 10 files changed, 48 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 58cd595..eb53ea0 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ First, we need to load the RSS : $rss = SRSS::read('http://exemple.com/rss.xml'); -Easy, right? Then you can extract general informations : +Easy, right? Then you can extract general information : echo $rss->title; // will display blog title @@ -43,7 +43,7 @@ First, we need to initialize the RSS : $rss = SRSS::create(); -Easy, right? Then you can add general informations : +Easy, right? Then you can add general information : $rss->title = 'My Awesome Blog'; $rss->link = 'http://shikiryu.com/devblog/'; @@ -73,4 +73,4 @@ The other one does the opposite and add the next item in top of your RSS ---------------------------------- Contact : -http://shikiryu.com/contact +https://shikiryu.com/contact diff --git a/src/Builder/SRSSBuilder.php b/src/Builder/SRSSBuilder.php index 19ac73d..e1a1b6c 100644 --- a/src/Builder/SRSSBuilder.php +++ b/src/Builder/SRSSBuilder.php @@ -17,6 +17,8 @@ class SRSSBuilder extends DomDocument $root->setAttribute('version', '2.0'); $channel = $this->createElement('channel'); + $srss->channel->generator = 'Shikiryu RSS'; + $this->appendChannelToDom($srss->channel, $channel); $this->appendItemsToDom($srss->items, $channel); @@ -24,7 +26,6 @@ class SRSSBuilder extends DomDocument $root->appendChild($channel); $this->appendChild($root); $this->encoding = 'UTF-8'; - $srss->generator = 'Shikiryu RSS'; $this->formatOutput = true; $this->preserveWhiteSpace = false; // $docs = 'http://www.scriptol.fr/rss/RSS-2.0.html'; diff --git a/src/Entity/Channel/Image.php b/src/Entity/Channel/Image.php index f4fac3a..e7f1757 100644 --- a/src/Entity/Channel/Image.php +++ b/src/Entity/Channel/Image.php @@ -38,7 +38,11 @@ class Image extends HasValidator implements SRSSElement public function isValid(): bool { - return (new Validator())->isObjectValid($this); + try { + return (new Validator())->isObjectValid($this); + } catch (\ReflectionException $e) { + return false; + } } public function toArray(): array diff --git a/src/Entity/Item.php b/src/Entity/Item.php index 2831f2f..6b795b6 100644 --- a/src/Entity/Item.php +++ b/src/Entity/Item.php @@ -2,6 +2,7 @@ namespace Shikiryu\SRSS\Entity; +use Shikiryu\SRSS\Entity\Media\Content; use Shikiryu\SRSS\Validator\HasValidator; use Shikiryu\SRSS\Validator\Validator; @@ -50,14 +51,18 @@ class Item extends HasValidator implements SRSSElement public ?string $source = null; /** - * @var \Shikiryu\SRSS\Entity\Media\Content[] + * @var Content[] * @contentMedia */ public array $medias = []; public function isValid(): bool { - return (new Validator())->isObjectValid($this); + try { + return (new Validator())->isObjectValid($this); + } catch (\ReflectionException $e) { + return false; + } } public function toArray(): array diff --git a/src/Entity/Media/Content.php b/src/Entity/Media/Content.php index bfbd14a..a776957 100644 --- a/src/Entity/Media/Content.php +++ b/src/Entity/Media/Content.php @@ -67,7 +67,11 @@ class Content extends HasValidator implements SRSSElement public function isValid(): bool { - return (new Validator())->isObjectValid($this); + try { + return (new Validator())->isObjectValid($this); + } catch (\ReflectionException $e) { + return false; + } } public function toArray(): array diff --git a/src/Parser/ItemParser.php b/src/Parser/ItemParser.php index f65649c..e40f063 100644 --- a/src/Parser/ItemParser.php +++ b/src/Parser/ItemParser.php @@ -84,7 +84,7 @@ class ItemParser extends DomDocument /** * getter of item DomElement */ - public function getItem(): ?\DOMNode + public function getItem(): ?DOMNode { $this->appendChild($this->node); diff --git a/src/Parser/SRSSParser.php b/src/Parser/SRSSParser.php index a7f9076..2936ad8 100644 --- a/src/Parser/SRSSParser.php +++ b/src/Parser/SRSSParser.php @@ -3,6 +3,7 @@ namespace Shikiryu\SRSS\Parser; use DOMDocument; +use DOMNode; use DOMNodeList; use DOMXPath; use Shikiryu\SRSS\Entity\Channel; @@ -39,8 +40,8 @@ class SRSSParser extends DomDocument /** * @param string $link * - * @return \Shikiryu\SRSS\SRSS - * @throws \Shikiryu\SRSS\Exception\SRSSException + * @return SRSS + * @throws SRSSException */ public function parse(string $link) { @@ -48,7 +49,7 @@ class SRSSParser extends DomDocument $channel = $this->getElementsByTagName('channel'); if($channel->length === 1){ // Good URL and good RSS $this->_loadAttributes(); // loading channel properties - $this->getItems(); // loading all items + $this->parseItems(); // loading all items return $this->doc; } @@ -61,9 +62,9 @@ class SRSSParser extends DomDocument /** * @return Item[] - * @throws \Shikiryu\SRSS\Exception\SRSSException + * @throws SRSSException */ - private function getItems(): array + private function parseItems(): array { $channel = $this->_getChannel(); /** @var DOMNodeList $items */ @@ -108,10 +109,10 @@ class SRSSParser extends DomDocument /** * getter of current RSS channel - * @return \DOMNode + * @return DOMNode * @throws SRSSException */ - private function _getChannel(): \DOMNode + private function _getChannel(): DOMNode { $channel = $this->getElementsByTagName('channel'); if($channel->length != 1) { diff --git a/src/SRSS.php b/src/SRSS.php index 3e047ca..18da1d8 100644 --- a/src/SRSS.php +++ b/src/SRSS.php @@ -39,7 +39,6 @@ class SRSS implements Iterator /** * @return SRSS - * @throws \DOMException */ public static function create() { @@ -57,14 +56,18 @@ class SRSS implements Iterator */ public function isValid(): bool { - $valid = true; - foreach ($this->getItems() as $item) { - if ($item->isValid() === false) { - $valid = false; + try { + $valid = true; + foreach ($this->getItems() as $item) { + if ($item->isValid() === false) { + $valid = false; + } } - } - return ($valid && $this->channel->isValid()); + return ($valid && $this->channel->isValid()); + } catch (\ReflectionException $e) { + return false; + } } /** diff --git a/src/SRSSTools.php b/src/SRSSTools.php index 9d7ee3e..f677047 100644 --- a/src/SRSSTools.php +++ b/src/SRSSTools.php @@ -2,6 +2,7 @@ namespace Shikiryu\SRSS; +use DateTime; use DateTimeInterface; class SRSSTools @@ -62,7 +63,7 @@ class SRSSTools } if (count(explode(' ', $date)) === 2) { - return \DateTime::createFromFormat('Y-m-d H:i:s', $date)->format(DateTimeInterface::RSS); + return DateTime::createFromFormat('Y-m-d H:i:s', $date)->format(DateTimeInterface::RSS); } [$j, $m, $a] = explode('/', $date); diff --git a/src/Validator/Validator.php b/src/Validator/Validator.php index c16f1de..4652d28 100644 --- a/src/Validator/Validator.php +++ b/src/Validator/Validator.php @@ -2,6 +2,7 @@ namespace Shikiryu\SRSS\Validator; +use DateTime; use DateTimeInterface; use ReflectionClass; use ReflectionException; @@ -12,7 +13,7 @@ class Validator { protected ?object $object = null; /** - * @throws \ReflectionException + * @throws ReflectionException */ public function isPropertyValid($object, $property) { @@ -34,6 +35,8 @@ class Validator $object->validated[$properties->name] = $this->_validateProperty($annotation, $propertyValue); } + + return false; } /** @@ -173,7 +176,7 @@ class Validator private function _validateDate($value): bool { - return \DateTime::createFromFormat(DateTimeInterface::RSS, $value) !== false; + return DateTime::createFromFormat(DateTimeInterface::RSS, $value) !== false; } private function _validateHour($value): bool