1
0
mirror of https://github.com/Chouchen/ShikiryuRSS.git synced 2024-05-07 14:41:31 +02: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');
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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