mirror of
https://github.com/Chouchen/ShikiryuRSS.git
synced 2025-09-05 13:04:32 +02:00
✅ Add new exceptions
This commit is contained in:
12
src/Exception/ChannelNotFoundInRSSException.php
Normal file
12
src/Exception/ChannelNotFoundInRSSException.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\SRSS\Exception;
|
||||
|
||||
class ChannelNotFoundInRSSException extends SRSSException
|
||||
{
|
||||
public function __construct($file)
|
||||
{
|
||||
parent::__construct(sprintf('Invalid file `%s`: <channel> not found', $file));
|
||||
}
|
||||
|
||||
}
|
15
src/Exception/PropertyNotFoundException.php
Normal file
15
src/Exception/PropertyNotFoundException.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\SRSS\Exception;
|
||||
|
||||
class PropertyNotFoundException extends SRSSException
|
||||
{
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct($class, $name)
|
||||
{
|
||||
parent::__construct(sprintf('Property `%s` not found in `%s`', $name, $class));
|
||||
}
|
||||
}
|
12
src/Exception/UnreadableRSSException.php
Normal file
12
src/Exception/UnreadableRSSException.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\SRSS\Exception;
|
||||
|
||||
class UnreadableRSSException extends SRSSException
|
||||
{
|
||||
public function __construct($file)
|
||||
{
|
||||
parent::__construct(sprintf('File `%s` is unreadable.', $file));
|
||||
}
|
||||
|
||||
}
|
@@ -9,7 +9,9 @@ use DOMXPath;
|
||||
use Shikiryu\SRSS\Entity\Channel;
|
||||
use Shikiryu\SRSS\Entity\Channel\Image;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException;
|
||||
use Shikiryu\SRSS\Exception\SRSSException;
|
||||
use Shikiryu\SRSS\Exception\UnreadableRSSException;
|
||||
use Shikiryu\SRSS\SRSS;
|
||||
|
||||
class SRSSParser extends DomDocument
|
||||
@@ -41,9 +43,11 @@ class SRSSParser extends DomDocument
|
||||
* @param string $link
|
||||
*
|
||||
* @return SRSS
|
||||
* @throws ChannelNotFoundInRSSException
|
||||
* @throws SRSSException
|
||||
* @throws UnreadableRSSException
|
||||
*/
|
||||
public function parse(string $link)
|
||||
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.
|
||||
$channel = $this->getElementsByTagName('channel');
|
||||
@@ -54,10 +58,10 @@ class SRSSParser extends DomDocument
|
||||
return $this->doc;
|
||||
}
|
||||
|
||||
throw new SRSSException('invalid file '.$link);
|
||||
throw new ChannelNotFoundInRSSException($link);
|
||||
}
|
||||
|
||||
throw new SRSSException('Can not open file '.$link);
|
||||
throw new UnreadableRSSException($link);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,7 +120,7 @@ class SRSSParser extends DomDocument
|
||||
{
|
||||
$channel = $this->getElementsByTagName('channel');
|
||||
if($channel->length != 1) {
|
||||
throw new SRSSException('channel node not created, or too many channel nodes');
|
||||
throw new ChannelNotFoundInRSSException('channel node not created, or too many channel nodes');
|
||||
}
|
||||
|
||||
return $channel->item(0);
|
||||
|
21
src/SRSS.php
21
src/SRSS.php
@@ -3,15 +3,20 @@
|
||||
namespace Shikiryu\SRSS;
|
||||
|
||||
use Iterator;
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Builder\SRSSBuilder;
|
||||
use Shikiryu\SRSS\Entity\Channel;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException;
|
||||
use Shikiryu\SRSS\Exception\PropertyNotFoundException;
|
||||
use Shikiryu\SRSS\Exception\SRSSException;
|
||||
use Shikiryu\SRSS\Exception\UnreadableRSSException;
|
||||
use Shikiryu\SRSS\Parser\SRSSParser;
|
||||
|
||||
class SRSS implements Iterator
|
||||
{
|
||||
public Channel $channel;
|
||||
|
||||
/** @var Item[] */
|
||||
public array $items; // array of SRSSItems
|
||||
|
||||
@@ -30,6 +35,8 @@ class SRSS implements Iterator
|
||||
* @param string $link url of the rss
|
||||
*
|
||||
* @return SRSS
|
||||
* @throws ChannelNotFoundInRSSException
|
||||
* @throws UnreadableRSSException
|
||||
* @throws SRSSException
|
||||
*/
|
||||
public static function read(string $link): SRSS
|
||||
@@ -40,7 +47,7 @@ class SRSS implements Iterator
|
||||
/**
|
||||
* @return SRSS
|
||||
*/
|
||||
public static function create()
|
||||
public static function create(): SRSS
|
||||
{
|
||||
$doc = new SRSS;
|
||||
|
||||
@@ -65,7 +72,7 @@ class SRSS implements Iterator
|
||||
}
|
||||
|
||||
return ($valid && $this->channel->isValid());
|
||||
} catch (\ReflectionException $e) {
|
||||
} catch (ReflectionException $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -91,7 +98,7 @@ class SRSS implements Iterator
|
||||
public function __set($name, $val)
|
||||
{
|
||||
if (!property_exists(Channel::class, $name)) {
|
||||
throw new SRSSException($name . ' is not a possible item');
|
||||
throw new PropertyNotFoundException(Channel::class, $name);
|
||||
}
|
||||
// TODO add validator ?
|
||||
// if ((new Validator())->isPropertyValid($this->channel, $name)) {
|
||||
@@ -210,9 +217,9 @@ class SRSS implements Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Shikiryu\SRSS\Entity\Item $rssItem
|
||||
* @param Item $rssItem
|
||||
*
|
||||
* @return array|\Shikiryu\SRSS\Entity\Item[]
|
||||
* @return array|Item[]
|
||||
*/
|
||||
public function addItem(Item $rssItem): array
|
||||
{
|
||||
@@ -222,9 +229,9 @@ class SRSS implements Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Shikiryu\SRSS\Entity\Item $firstItem
|
||||
* @param Item $firstItem
|
||||
*
|
||||
* @return array|\Shikiryu\SRSS\Entity\Item[]
|
||||
* @return array|Item[]
|
||||
*/
|
||||
public function addItemBefore(Item $firstItem): array
|
||||
{
|
||||
|
Reference in New Issue
Block a user