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

🚧 Add some validations and corrections

This commit is contained in:
Shikiryu 2023-04-11 23:49:03 +02:00
parent 5de5993e2b
commit f1816dec0c
10 changed files with 48 additions and 26 deletions

View File

@ -12,7 +12,7 @@ First, we need to load the RSS :
$rss = SRSS::read('http://exemple.com/rss.xml'); $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 echo $rss->title; // will display blog title
@ -43,7 +43,7 @@ First, we need to initialize the RSS :
$rss = SRSS::create(); $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->title = 'My Awesome Blog';
$rss->link = 'http://shikiryu.com/devblog/'; $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 : Contact :
http://shikiryu.com/contact https://shikiryu.com/contact

View File

@ -17,6 +17,8 @@ class SRSSBuilder extends DomDocument
$root->setAttribute('version', '2.0'); $root->setAttribute('version', '2.0');
$channel = $this->createElement('channel'); $channel = $this->createElement('channel');
$srss->channel->generator = 'Shikiryu RSS';
$this->appendChannelToDom($srss->channel, $channel); $this->appendChannelToDom($srss->channel, $channel);
$this->appendItemsToDom($srss->items, $channel); $this->appendItemsToDom($srss->items, $channel);
@ -24,7 +26,6 @@ class SRSSBuilder extends DomDocument
$root->appendChild($channel); $root->appendChild($channel);
$this->appendChild($root); $this->appendChild($root);
$this->encoding = 'UTF-8'; $this->encoding = 'UTF-8';
$srss->generator = 'Shikiryu RSS';
$this->formatOutput = true; $this->formatOutput = true;
$this->preserveWhiteSpace = false; $this->preserveWhiteSpace = false;
// $docs = 'http://www.scriptol.fr/rss/RSS-2.0.html'; // $docs = 'http://www.scriptol.fr/rss/RSS-2.0.html';

View File

@ -38,7 +38,11 @@ class Image extends HasValidator implements SRSSElement
public function isValid(): bool public function isValid(): bool
{ {
try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) {
return false;
}
} }
public function toArray(): array public function toArray(): array

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Entity; namespace Shikiryu\SRSS\Entity;
use Shikiryu\SRSS\Entity\Media\Content;
use Shikiryu\SRSS\Validator\HasValidator; use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator; use Shikiryu\SRSS\Validator\Validator;
@ -50,14 +51,18 @@ class Item extends HasValidator implements SRSSElement
public ?string $source = null; public ?string $source = null;
/** /**
* @var \Shikiryu\SRSS\Entity\Media\Content[] * @var Content[]
* @contentMedia * @contentMedia
*/ */
public array $medias = []; public array $medias = [];
public function isValid(): bool public function isValid(): bool
{ {
try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) {
return false;
}
} }
public function toArray(): array public function toArray(): array

View File

@ -67,7 +67,11 @@ class Content extends HasValidator implements SRSSElement
public function isValid(): bool public function isValid(): bool
{ {
try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) {
return false;
}
} }
public function toArray(): array public function toArray(): array

View File

@ -84,7 +84,7 @@ class ItemParser extends DomDocument
/** /**
* getter of item DomElement * getter of item DomElement
*/ */
public function getItem(): ?\DOMNode public function getItem(): ?DOMNode
{ {
$this->appendChild($this->node); $this->appendChild($this->node);

View File

@ -3,6 +3,7 @@
namespace Shikiryu\SRSS\Parser; namespace Shikiryu\SRSS\Parser;
use DOMDocument; use DOMDocument;
use DOMNode;
use DOMNodeList; use DOMNodeList;
use DOMXPath; use DOMXPath;
use Shikiryu\SRSS\Entity\Channel; use Shikiryu\SRSS\Entity\Channel;
@ -39,8 +40,8 @@ class SRSSParser extends DomDocument
/** /**
* @param string $link * @param string $link
* *
* @return \Shikiryu\SRSS\SRSS * @return SRSS
* @throws \Shikiryu\SRSS\Exception\SRSSException * @throws SRSSException
*/ */
public function parse(string $link) public function parse(string $link)
{ {
@ -48,7 +49,7 @@ class SRSSParser extends DomDocument
$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->_loadAttributes(); // loading channel properties
$this->getItems(); // loading all items $this->parseItems(); // loading all items
return $this->doc; return $this->doc;
} }
@ -61,9 +62,9 @@ class SRSSParser extends DomDocument
/** /**
* @return Item[] * @return Item[]
* @throws \Shikiryu\SRSS\Exception\SRSSException * @throws SRSSException
*/ */
private function getItems(): array private function parseItems(): array
{ {
$channel = $this->_getChannel(); $channel = $this->_getChannel();
/** @var DOMNodeList $items */ /** @var DOMNodeList $items */
@ -108,10 +109,10 @@ class SRSSParser extends DomDocument
/** /**
* getter of current RSS channel * getter of current RSS channel
* @return \DOMNode * @return DOMNode
* @throws SRSSException * @throws SRSSException
*/ */
private function _getChannel(): \DOMNode private function _getChannel(): DOMNode
{ {
$channel = $this->getElementsByTagName('channel'); $channel = $this->getElementsByTagName('channel');
if($channel->length != 1) { if($channel->length != 1) {

View File

@ -39,7 +39,6 @@ class SRSS implements Iterator
/** /**
* @return SRSS * @return SRSS
* @throws \DOMException
*/ */
public static function create() public static function create()
{ {
@ -57,6 +56,7 @@ class SRSS implements Iterator
*/ */
public function isValid(): bool public function isValid(): bool
{ {
try {
$valid = true; $valid = true;
foreach ($this->getItems() as $item) { foreach ($this->getItems() as $item) {
if ($item->isValid() === false) { if ($item->isValid() === false) {
@ -65,6 +65,9 @@ class SRSS implements Iterator
} }
return ($valid && $this->channel->isValid()); return ($valid && $this->channel->isValid());
} catch (\ReflectionException $e) {
return false;
}
} }
/** /**

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS; namespace Shikiryu\SRSS;
use DateTime;
use DateTimeInterface; use DateTimeInterface;
class SRSSTools class SRSSTools
@ -62,7 +63,7 @@ class SRSSTools
} }
if (count(explode(' ', $date)) === 2) { 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); [$j, $m, $a] = explode('/', $date);

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Validator; namespace Shikiryu\SRSS\Validator;
use DateTime;
use DateTimeInterface; use DateTimeInterface;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
@ -12,7 +13,7 @@ class Validator
{ {
protected ?object $object = null; protected ?object $object = null;
/** /**
* @throws \ReflectionException * @throws ReflectionException
*/ */
public function isPropertyValid($object, $property) public function isPropertyValid($object, $property)
{ {
@ -34,6 +35,8 @@ class Validator
$object->validated[$properties->name] = $this->_validateProperty($annotation, $propertyValue); $object->validated[$properties->name] = $this->_validateProperty($annotation, $propertyValue);
} }
return false;
} }
/** /**
@ -173,7 +176,7 @@ class Validator
private function _validateDate($value): bool private function _validateDate($value): bool
{ {
return \DateTime::createFromFormat(DateTimeInterface::RSS, $value) !== false; return DateTime::createFromFormat(DateTimeInterface::RSS, $value) !== false;
} }
private function _validateHour($value): bool private function _validateHour($value): bool