mirror of
https://github.com/Chouchen/ShikiryuRSS.git
synced 2024-11-22 07:48:51 +01:00
🚨 Clean code for linters
This commit is contained in:
parent
08138992fd
commit
3419e7d46c
@ -18,6 +18,7 @@
|
||||
"phpunit/phpunit": "^9"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"ext-dom": "*",
|
||||
"ext-libxml": "*"
|
||||
}
|
||||
|
@ -6,14 +6,19 @@ use DOMDocument;
|
||||
use DOMElement;
|
||||
use Shikiryu\SRSS\Entity\Channel;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Exception\DOMBuilderException;
|
||||
use Shikiryu\SRSS\SRSS;
|
||||
use Shikiryu\SRSS\SRSSTools;
|
||||
|
||||
class SRSSBuilder extends DomDocument
|
||||
{
|
||||
private function buildRSS(SRSS $srss)
|
||||
/**
|
||||
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
|
||||
*/
|
||||
private function buildRSS(SRSS $srss): SRSSBuilder
|
||||
{
|
||||
try {
|
||||
$root = $this->createElement('rss');
|
||||
|
||||
$root->setAttribute('version', '2.0');
|
||||
$channel = $this->createElement('channel');
|
||||
|
||||
@ -30,141 +35,50 @@ class SRSSBuilder extends DomDocument
|
||||
$this->preserveWhiteSpace = false;
|
||||
// $docs = 'http://www.scriptol.fr/rss/RSS-2.0.html';
|
||||
|
||||
} catch (\DOMException $e) {
|
||||
throw new DOMBuilderException($e);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
public function build(SRSS $srss, string $filepath)
|
||||
{
|
||||
$this->buildRSS($srss);
|
||||
|
||||
$this->save($filepath);
|
||||
/**
|
||||
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
|
||||
*/
|
||||
public function build(SRSS $srss, string $filepath): void
|
||||
{
|
||||
$this
|
||||
->buildRSS($srss)
|
||||
->save($filepath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
|
||||
*/
|
||||
public function show(SRSS $srss)
|
||||
public function show(SRSS $srss): bool|string
|
||||
{
|
||||
$this->buildRSS($srss);
|
||||
|
||||
return $this->saveXml();
|
||||
return $this
|
||||
->buildRSS($srss)
|
||||
->saveXml();
|
||||
}
|
||||
|
||||
/**
|
||||
* setter of "image"'s channel attributes
|
||||
* @param $url string picture's url
|
||||
* @param $title string picture's title
|
||||
* @param $link string link on the picture
|
||||
* @param $width int width
|
||||
* @param $height int height
|
||||
* @param $description string description
|
||||
* TODO
|
||||
*/
|
||||
public function setImage($url, $title, $link, $width = 0, $height = 0, $description = '')
|
||||
private function appendChannelToDom(Channel $channel, DOMElement $node): void
|
||||
{
|
||||
$channel = $this->_getChannel();
|
||||
$array = [];
|
||||
$url = SRSSTools::checkLink($url);
|
||||
$array['url'] = $url;
|
||||
$title = SRSSTools::noHTML($title);
|
||||
$array['title'] = $title;
|
||||
$link = SRSSTools::checkLink($link);
|
||||
$array['link'] = $link;
|
||||
if($width != 0)
|
||||
{
|
||||
$width = SRSSTools::checkInt($width);
|
||||
$array['width'] = $width;
|
||||
}
|
||||
if($height != 0)
|
||||
{
|
||||
$height = SRSSTools::checkInt($height);
|
||||
$array['height'] = $height;
|
||||
}
|
||||
if($description != 0)
|
||||
{
|
||||
$description = SRSSTools::noHTML($description);
|
||||
$array['description'] = $description;
|
||||
}
|
||||
if($this->image == null)
|
||||
{
|
||||
$node = $this->createElement('image');
|
||||
$urlNode = $this->createElement('url', $url);
|
||||
$titleNode = $this->createElement('title', $title);
|
||||
$linkNode = $this->createElement('link', $link);
|
||||
$node->appendChild($urlNode);
|
||||
$node->appendChild($titleNode);
|
||||
$node->appendChild($linkNode);
|
||||
if($width != 0)
|
||||
{
|
||||
$widthNode = $this->createElement('width', $width);
|
||||
$node->appendChild($widthNode);
|
||||
}
|
||||
if($height != 0)
|
||||
{
|
||||
$heightNode = $this->createElement('height', $height);
|
||||
$node->appendChild($heightNode);
|
||||
}
|
||||
if($description != '')
|
||||
{
|
||||
$descNode = $this->createElement('description', $description);
|
||||
$node->appendChild($descNode);
|
||||
}
|
||||
$channel->appendChild($node);
|
||||
}
|
||||
$this->attr['image'] = $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* setter of "cloud"'s channel attributes
|
||||
* @param $domain string domain
|
||||
* @param $port int port
|
||||
* @param $path string path
|
||||
* @param $registerProcedure string register procedure
|
||||
* @param $protocol string protocol
|
||||
* TODO
|
||||
*/
|
||||
public function setCloud($domain, $port, $path, $registerProcedure, $protocol)
|
||||
{
|
||||
$channel = $this->_getChannel();
|
||||
$array = array();
|
||||
$domain = SRSSTools::noHTML($domain);
|
||||
$array['domain'] = $domain;
|
||||
$port = SRSSTools::checkInt($port);
|
||||
$array['port'] = $port;
|
||||
$path = SRSSTools::noHTML($path);
|
||||
$array['path'] = $path;
|
||||
$registerProcedure = SRSSTools::noHTML($registerProcedure);
|
||||
$array['registerProcedure'] = $registerProcedure;
|
||||
$protocol = SRSSTools::noHTML($protocol);
|
||||
$array['protocol'] = $protocol;
|
||||
if($this->cloud == null)
|
||||
{
|
||||
$node = $this->createElement('cloud');
|
||||
$node->setAttribute('domain', $domain);
|
||||
$node->setAttribute('port', $port);
|
||||
$node->setAttribute('path', $path);
|
||||
$node->setAttribute('registerProcedure', $registerProcedure);
|
||||
$node->setAttribute('protocol', $protocol);
|
||||
$channel->appendChild($node);
|
||||
}
|
||||
$this->attr['cloud'] = $array;
|
||||
}
|
||||
|
||||
private function appendChannelToDom(Channel $channel, DOMElement $node)
|
||||
{
|
||||
foreach (array_filter($channel->toArray(), fn($el) => !empty($el)) as $name => $value) {
|
||||
foreach (array_filter($channel->toArray(), static fn($el) => !empty($el)) as $name => $value) {
|
||||
$new_node = $this->createElement($name, $value);
|
||||
$node->appendChild($new_node);
|
||||
}
|
||||
}
|
||||
|
||||
private function appendItemsToDom(array $items, DOMElement $channel)
|
||||
private function appendItemsToDom(array $items, DOMElement $channel): void
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$this->appendItemToDom($item, $channel);
|
||||
}
|
||||
}
|
||||
|
||||
private function appendItemToDom(Item $item, DOMElement $channel)
|
||||
private function appendItemToDom(Item $item, DOMElement $channel): void
|
||||
{
|
||||
$itemNode = $this->createElement('item');
|
||||
foreach (array_filter($item->toArray()) as $name => $value) {
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Shikiryu\SRSS\Entity\Channel;
|
||||
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Entity\SRSSElement;
|
||||
use Shikiryu\SRSS\Validator\HasValidator;
|
||||
use Shikiryu\SRSS\Validator\Validator;
|
||||
@ -21,7 +22,7 @@ class Category extends HasValidator implements SRSSElement
|
||||
{
|
||||
try {
|
||||
return (new Validator())->isObjectValid($this);
|
||||
} catch (\ReflectionException $e) {
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Shikiryu\SRSS\Entity\Channel;
|
||||
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Entity\SRSSElement;
|
||||
use Shikiryu\SRSS\Validator\HasValidator;
|
||||
use Shikiryu\SRSS\Validator\Validator;
|
||||
@ -29,13 +30,11 @@ class Cloud extends HasValidator implements SRSSElement
|
||||
*/
|
||||
public ?string $protocol = null;
|
||||
|
||||
//<cloud domain="rpc.sys.com" port="80" path="/RPC2" registerProcedure="myCloud.rssPleaseNotify" protocol="xml-rpc" />
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
try {
|
||||
return (new Validator())->isObjectValid($this);
|
||||
} catch (\ReflectionException $e) {
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Shikiryu\SRSS\Entity\Channel;
|
||||
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Entity\SRSSElement;
|
||||
use Shikiryu\SRSS\Validator\HasValidator;
|
||||
use Shikiryu\SRSS\Validator\Validator;
|
||||
@ -40,7 +41,7 @@ class Image extends HasValidator implements SRSSElement
|
||||
{
|
||||
try {
|
||||
return (new Validator())->isObjectValid($this);
|
||||
} catch (\ReflectionException $e) {
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Shikiryu\SRSS\Entity;
|
||||
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Entity\Item\Category;
|
||||
use Shikiryu\SRSS\Entity\Item\Enclosure;
|
||||
use Shikiryu\SRSS\Entity\Item\Source;
|
||||
@ -64,7 +65,7 @@ class Item extends HasValidator implements SRSSElement
|
||||
{
|
||||
try {
|
||||
return (new Validator())->isObjectValid($this);
|
||||
} catch (\ReflectionException $e) {
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Shikiryu\SRSS\Entity\Item;
|
||||
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Entity\SRSSElement;
|
||||
use Shikiryu\SRSS\Validator\HasValidator;
|
||||
use Shikiryu\SRSS\Validator\Validator;
|
||||
@ -21,7 +22,7 @@ class Category extends HasValidator implements SRSSElement
|
||||
{
|
||||
try {
|
||||
return (new Validator())->isObjectValid($this);
|
||||
} catch (\ReflectionException $e) {
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Shikiryu\SRSS\Entity\Item;
|
||||
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Entity\SRSSElement;
|
||||
use Shikiryu\SRSS\Validator\HasValidator;
|
||||
use Shikiryu\SRSS\Validator\Validator;
|
||||
@ -27,7 +28,7 @@ class Enclosure extends HasValidator implements SRSSElement
|
||||
{
|
||||
try {
|
||||
return (new Validator())->isObjectValid($this);
|
||||
} catch (\ReflectionException $e) {
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Shikiryu\SRSS\Entity\Item;
|
||||
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Entity\SRSSElement;
|
||||
use Shikiryu\SRSS\Validator\HasValidator;
|
||||
use Shikiryu\SRSS\Validator\Validator;
|
||||
@ -22,7 +23,7 @@ class Source extends HasValidator implements SRSSElement
|
||||
{
|
||||
try {
|
||||
return (new Validator())->isObjectValid($this);
|
||||
} catch (\ReflectionException $e) {
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Shikiryu\SRSS\Entity\Media;
|
||||
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Entity\SRSSElement;
|
||||
use Shikiryu\SRSS\Validator\HasValidator;
|
||||
use Shikiryu\SRSS\Validator\Validator;
|
||||
@ -69,7 +70,7 @@ class Content extends HasValidator implements SRSSElement
|
||||
{
|
||||
try {
|
||||
return (new Validator())->isObjectValid($this);
|
||||
} catch (\ReflectionException $e) {
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
12
src/Exception/DOMBuilderException.php
Normal file
12
src/Exception/DOMBuilderException.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\SRSS\Exception;
|
||||
|
||||
class DOMBuilderException extends SRSSException
|
||||
{
|
||||
public function __construct(\DOMException $e)
|
||||
{
|
||||
parent::__construct(sprintf('Error while building DOM (%s)', $e->getMessage()));
|
||||
}
|
||||
|
||||
}
|
@ -11,7 +11,10 @@ class SRSSException extends Exception
|
||||
parent :: __construct($msg);
|
||||
}
|
||||
|
||||
public function getError()
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getError(): string
|
||||
{
|
||||
return 'Une exception a été générée : <strong>Message : ' . $this->getMessage() . '</strong> à la ligne : ' . $this->getLine();
|
||||
}
|
||||
|
@ -95,33 +95,6 @@ class ItemParser extends DomDocument
|
||||
return $this->getElementsByTagName('item')->item(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* setter for enclosure's properties
|
||||
*
|
||||
* @param $url string url
|
||||
* @param $length int length
|
||||
* @param $type string type
|
||||
* @throws DOMException
|
||||
*/
|
||||
public function setEnclosure(string $url, int $length, string $type): void
|
||||
{
|
||||
$array = [];
|
||||
$url = SRSSTools::checkLink($url);
|
||||
$array['url'] = $url;
|
||||
$length = SRSSTools::checkInt($length);
|
||||
$array['length'] = $length;
|
||||
$type = SRSSTools::noHTML($type);
|
||||
$array['type'] = $type;
|
||||
if ($this->enclosure == null) {
|
||||
$node = $this->createElement('enclosure');
|
||||
$node->setAttribute('url', $url);
|
||||
$node->setAttribute('length', $length);
|
||||
$node->setAttribute('type', $type);
|
||||
$this->node->appendChild($node);
|
||||
}
|
||||
$this->attr['enclosure'] = $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if current item is valid (following specifications)
|
||||
* @return bool
|
||||
@ -156,12 +129,13 @@ class ItemParser extends DomDocument
|
||||
}
|
||||
|
||||
$flag = $this->possibilities[$name];
|
||||
if ($flag !== '')
|
||||
if ($flag !== '') {
|
||||
$val = SRSSTools::check($val, $flag);
|
||||
}
|
||||
if (!empty($val)) {
|
||||
if ($val instanceof DOMElement) {
|
||||
$this->node->appendChild($val);
|
||||
} elseif ($this->$name == null) {
|
||||
} elseif ($this->$name === null) {
|
||||
$this->node->appendChild(new DomElement($name, $val));
|
||||
}
|
||||
$this->attr[$name] = $val;
|
||||
@ -178,11 +152,17 @@ class ItemParser extends DomDocument
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
if (isset($this->attr[$name]))
|
||||
if (isset($this->attr[$name])) {
|
||||
return $this->attr[$name];
|
||||
}
|
||||
|
||||
if (array_key_exists($name, $this->possibilities)) {
|
||||
|
||||
$tmp = $this->node->getElementsByTagName($name);
|
||||
if ($tmp->length != 1) return null;
|
||||
|
||||
if ($tmp->length !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $tmp->item(0)->nodeValue;
|
||||
}
|
||||
@ -194,7 +174,7 @@ class ItemParser extends DomDocument
|
||||
* transform current item's object into an array
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
public function toArray(): array
|
||||
{
|
||||
$infos = [];
|
||||
foreach ($this->attr as $attrName => $attrVal) {
|
||||
|
@ -5,10 +5,8 @@ namespace Shikiryu\SRSS\Parser;
|
||||
use DOMDocument;
|
||||
use DOMNode;
|
||||
use DOMNodeList;
|
||||
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;
|
||||
@ -17,13 +15,11 @@ use Shikiryu\SRSS\SRSS;
|
||||
class SRSSParser extends DomDocument
|
||||
{
|
||||
private SRSS $doc;
|
||||
private DOMXPath $xpath;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
libxml_use_internal_errors(true);
|
||||
parent::__construct();
|
||||
$this->xpath = new DOMXpath($this);
|
||||
$this->doc = new SRSS();
|
||||
}
|
||||
|
||||
@ -65,10 +61,9 @@ class SRSSParser extends DomDocument
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item[]
|
||||
* @throws SRSSException
|
||||
*/
|
||||
private function _parseItems(): array
|
||||
private function _parseItems(): void
|
||||
{
|
||||
$channel = $this->_getChannel();
|
||||
/** @var DOMNodeList $items */
|
||||
@ -80,9 +75,8 @@ class SRSSParser extends DomDocument
|
||||
$this->doc->items[$i] = ItemParser::read($items->item($i));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->doc->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* putting all RSS attributes into the object
|
||||
* @throws SRSSException
|
||||
@ -132,36 +126,10 @@ class SRSSParser extends DomDocument
|
||||
private function _getChannel(): DOMNode
|
||||
{
|
||||
$channel = $this->getElementsByTagName('channel');
|
||||
if($channel->length != 1) {
|
||||
if($channel->length !== 1) {
|
||||
throw new ChannelNotFoundInRSSException('channel node not created, or too many channel nodes');
|
||||
}
|
||||
|
||||
return $channel->item(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* getter of "image"'s channel attributes
|
||||
* @return string|array
|
||||
* TODO
|
||||
*/
|
||||
public function image()
|
||||
{
|
||||
$args = func_get_args();
|
||||
if (func_num_args() == 0) {
|
||||
$args[0] = 'url';
|
||||
}
|
||||
$img = $this->xpath->query('//channel/image');
|
||||
if ($img->length != 1) { // <image> is not in channel
|
||||
return null;
|
||||
}
|
||||
$img = $img->item(0);
|
||||
$r = [];
|
||||
foreach ($img->childNodes as $child) {
|
||||
if ($child->nodeType == XML_ELEMENT_NODE && in_array($child->nodeName, $args)) {
|
||||
$r[$child->nodeName] = $child->nodeValue;
|
||||
}
|
||||
}
|
||||
|
||||
return (func_num_args() > 1) ? $r : $r[$args[0]];
|
||||
}
|
||||
}
|
30
src/SRSS.php
30
src/SRSS.php
@ -6,6 +6,9 @@ use Iterator;
|
||||
use ReflectionException;
|
||||
use Shikiryu\SRSS\Builder\SRSSBuilder;
|
||||
use Shikiryu\SRSS\Entity\Channel;
|
||||
use Shikiryu\SRSS\Entity\Channel\Category;
|
||||
use Shikiryu\SRSS\Entity\Channel\Cloud;
|
||||
use Shikiryu\SRSS\Entity\Channel\Image;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException;
|
||||
use Shikiryu\SRSS\Exception\PropertyNotFoundException;
|
||||
@ -13,6 +16,27 @@ use Shikiryu\SRSS\Exception\SRSSException;
|
||||
use Shikiryu\SRSS\Exception\UnreadableRSSException;
|
||||
use Shikiryu\SRSS\Parser\SRSSParser;
|
||||
|
||||
/**
|
||||
* @property null|string $title
|
||||
* @property null|string $link
|
||||
* @property null|string $description
|
||||
* @property null|string $language
|
||||
* @property null|string $copyright
|
||||
* @property null|string $managingEditor
|
||||
* @property null|string $webMaster
|
||||
* @property null|string $pubDate
|
||||
* @property null|string $lastBuildDate
|
||||
* @property null|Category[] $category
|
||||
* @property null|string $generator
|
||||
* @property null|string $docs
|
||||
* @property null|Cloud $cloud
|
||||
* @property null|string $ttl
|
||||
* @property null|Image $image
|
||||
* @property null|string $rating
|
||||
* @property null|string $textInput
|
||||
* @property null|string $skipHours
|
||||
* @property null|string $skipDays
|
||||
*/
|
||||
class SRSS implements Iterator
|
||||
{
|
||||
public Channel $channel;
|
||||
@ -72,7 +96,7 @@ class SRSS implements Iterator
|
||||
}
|
||||
|
||||
return ($valid && $this->channel->isValid());
|
||||
} catch (ReflectionException $e) {
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -244,6 +268,7 @@ class SRSS implements Iterator
|
||||
* @param string $path
|
||||
*
|
||||
* @return void
|
||||
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
|
||||
*/
|
||||
public function save(string $path): void
|
||||
{
|
||||
@ -252,8 +277,9 @@ class SRSS implements Iterator
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
|
||||
*/
|
||||
public function show()
|
||||
public function show(): bool|string
|
||||
{
|
||||
return (new SRSSBuilder('1.0', 'UTF-8'))->show($this);
|
||||
}
|
||||
|
@ -28,36 +28,37 @@ class SRSSTools
|
||||
|
||||
/**
|
||||
* format the RSS to the wanted format
|
||||
*
|
||||
* @param $format string wanted format
|
||||
* @param $date string RSS date
|
||||
*
|
||||
* @return string date
|
||||
*/
|
||||
public static function formatDate($format, $date)
|
||||
public static function formatDate(string $format, string $date): string
|
||||
{
|
||||
return date($format, strtotime($date));
|
||||
}
|
||||
|
||||
/**
|
||||
* format a date for RSS format
|
||||
*
|
||||
* @param string $date date to format
|
||||
* @param string $format
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getRSSDate($date, $format='')
|
||||
public static function getRSSDate(string $date, string $format = ''): string
|
||||
{
|
||||
$datepos = 'dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU';
|
||||
if($format != '' && preg_match('~^(['.$datepos.']{1})(-|/)(['.$datepos.']{1})(-|/)(['.$datepos.']{1})$~', $format, $match)){
|
||||
$sep = $match[2];
|
||||
$format = '%'.$match[1].$sep.'%'.$match[3].$sep.'%'.$match[5];
|
||||
if($dateArray = strptime($date, $format)){
|
||||
$mois = (int)$dateArray['tm_mon'] + 1;
|
||||
$annee = strlen($dateArray['tm_year']) > 2 ? '20'.substr($dateArray['tm_year'], -2) : '19'.$dateArray['tm_year'];
|
||||
$date = $annee.'-'.$mois.'-'.$dateArray['tm_mday'];
|
||||
return date("D, d M Y H:i:s T", strtotime($date));
|
||||
}
|
||||
$date_position = 'dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU';
|
||||
if($format !== '' && preg_match('~^(['.$date_position.']{1})([-/])(['.$date_position.']{1})([-/])(['.$date_position.']{1})$~', $format)){
|
||||
$datetime = DateTime::createFromFormat($format, $date);
|
||||
if ($datetime === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $datetime->format(DATE_RSS);
|
||||
}
|
||||
|
||||
if (strtotime($date) !==false ) {
|
||||
return date("D, d M Y H:i:s T", strtotime($date));
|
||||
}
|
||||
@ -73,41 +74,49 @@ class SRSSTools
|
||||
|
||||
/**
|
||||
* check if it's an url
|
||||
*
|
||||
* @param $check string to check
|
||||
*
|
||||
* @return string|boolean the filtered data, or FALSE if the filter fails.
|
||||
*/
|
||||
public static function checkLink($check)
|
||||
public static function checkLink(string $check): bool|string
|
||||
{
|
||||
return filter_var($check, FILTER_VALIDATE_URL);
|
||||
}
|
||||
|
||||
/**
|
||||
* make a string XML-compatible
|
||||
*
|
||||
* @param $check string to format
|
||||
*
|
||||
* @return string formatted string
|
||||
* TODO CDATA ?
|
||||
*/
|
||||
public static function HTML4XML($check)
|
||||
public static function HTML4XML(string $check): string
|
||||
{
|
||||
return htmlspecialchars($check);
|
||||
}
|
||||
|
||||
/**
|
||||
* delete html tags
|
||||
*
|
||||
* @param $check string to format
|
||||
*
|
||||
* @return string formatted string
|
||||
*/
|
||||
public static function noHTML($check)
|
||||
public static function noHTML(string $check): string
|
||||
{
|
||||
return strip_tags($check);
|
||||
}
|
||||
|
||||
/**
|
||||
* check if it's a day (in RSS terms)
|
||||
*
|
||||
* @param $check string to check
|
||||
*
|
||||
* @return string the day, or empty string
|
||||
*/
|
||||
public static function checkDay($check)
|
||||
public static function checkDay(string $check): string
|
||||
{
|
||||
$possibleDay = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
|
||||
return in_array(strtolower($check), $possibleDay) ? $check : '';
|
||||
@ -115,20 +124,24 @@ class SRSSTools
|
||||
|
||||
/**
|
||||
* check if it's an email
|
||||
*
|
||||
* @param $check string to check
|
||||
*
|
||||
* @return string|boolean the filtered data, or FALSE if the filter fails.
|
||||
*/
|
||||
public static function checkEmail($check)
|
||||
public static function checkEmail(string $check): bool|string
|
||||
{
|
||||
return filter_var($check, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
|
||||
/**
|
||||
* check if it's an hour (in RSS terms)
|
||||
*
|
||||
* @param $check string to check
|
||||
*
|
||||
* @return string|boolean the filtered data, or FALSE if the filter fails.
|
||||
*/
|
||||
public static function checkHour($check)
|
||||
public static function checkHour(string $check): bool|string
|
||||
{
|
||||
$options = [
|
||||
'options' => [
|
||||
@ -142,10 +155,12 @@ class SRSSTools
|
||||
|
||||
/**
|
||||
* check if it's an int
|
||||
*
|
||||
* @param $check int to check
|
||||
*
|
||||
* @return int|boolean the filtered data, or FALSE if the filter fails.
|
||||
*/
|
||||
public static function checkInt($check)
|
||||
public static function checkInt(int $check): bool|int
|
||||
{
|
||||
return filter_var($check, FILTER_VALIDATE_INT);
|
||||
}
|
||||
@ -155,7 +170,7 @@ class SRSSTools
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private static function checkMediaType($check)
|
||||
public static function checkMediaType($check): mixed
|
||||
{
|
||||
return $check;
|
||||
}
|
||||
@ -165,7 +180,7 @@ class SRSSTools
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
private static function checkMediaMedium($check)
|
||||
public static function checkMediaMedium($check): ?string
|
||||
{
|
||||
return in_array($check, ['image', 'audio', 'video', 'document', 'executable']) ? $check : null;
|
||||
}
|
||||
@ -175,7 +190,7 @@ class SRSSTools
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
private static function checkBool($check)
|
||||
public static function checkBool($check): ?string
|
||||
{
|
||||
return in_array($check, ['true', 'false']) ? $check : null;
|
||||
}
|
||||
@ -185,7 +200,7 @@ class SRSSTools
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
private static function checkMediumExpression($check)
|
||||
public static function checkMediumExpression($check): ?string
|
||||
{
|
||||
return in_array($check, ['sample', 'full', 'nonstop']) ? $check : null;
|
||||
}
|
||||
|
@ -15,9 +15,9 @@ class Validator
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function isPropertyValid($object, $property)
|
||||
public function isPropertyValid($object, $property): bool
|
||||
{
|
||||
$properties = array_filter($this->_getClassProperties(get_class($object)), fn($p) => $p->getName() === $property);
|
||||
$properties = array_filter($this->_getClassProperties(get_class($object)), static fn($p) => $p->getName() === $property);
|
||||
if (count($properties) !== 1) {
|
||||
return false;
|
||||
}
|
||||
@ -26,7 +26,7 @@ class Validator
|
||||
$propertyValue = $object->{$properties->name};
|
||||
$propertyAnnotations = $this->_getPropertyAnnotations($properties);
|
||||
|
||||
if (!in_array('required', $propertyAnnotations, true) && empty($propertyValue)) {
|
||||
if (empty($propertyValue) && !in_array('required', $propertyAnnotations, true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ class Validator
|
||||
$propertyValue = $object->{$property['name']};
|
||||
// $propertyAnnotations = $this->_getPropertyAnnotations($property, get_class($object));
|
||||
|
||||
if (!in_array('required', $property['rules'], true) && empty($propertyValue)) {
|
||||
if (empty($propertyValue) && !in_array('required', $property['rules'], true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -109,7 +109,7 @@ class Validator
|
||||
{
|
||||
preg_match_all('#@(.*?)\n#s', $property->getDocComment(), $annotations);
|
||||
|
||||
return array_map(fn($annotation) => trim($annotation), $annotations[1]);
|
||||
return array_map(static fn($annotation) => trim($annotation), $annotations[1]);
|
||||
}
|
||||
|
||||
private function _validateString($value): bool
|
||||
@ -199,7 +199,7 @@ class Validator
|
||||
);
|
||||
}
|
||||
|
||||
private function _validateContentMedia($value)
|
||||
private function _validateContentMedia($value): bool
|
||||
{
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $content) {
|
||||
|
@ -16,7 +16,7 @@ class BasicBuilderTest extends TestCase
|
||||
unlink($this->saved);
|
||||
}
|
||||
}
|
||||
public function testCreateBasicRSS()
|
||||
public function testCreateBasicRSS(): void
|
||||
{
|
||||
$srss = SRSS::create();
|
||||
$srss->title = 'My Blog';
|
||||
|
@ -9,7 +9,7 @@ use Shikiryu\SRSS\SRSS;
|
||||
|
||||
class BasicReaderTest extends TestCase
|
||||
{
|
||||
public function testReadBasicRSS()
|
||||
public function testReadBasicRSS(): void
|
||||
{
|
||||
$rss = SRSS::read(__DIR__.'/resources/basic.xml');
|
||||
self::assertEquals('test Home Page', $rss->title);
|
||||
@ -20,7 +20,7 @@ class BasicReaderTest extends TestCase
|
||||
self::assertTrue($rss->isValid());
|
||||
}
|
||||
|
||||
public function testSpecificationExampleRSS()
|
||||
public function testSpecificationExampleRSS(): void
|
||||
{
|
||||
$rss = SRSS::read(__DIR__.'/resources/harvard.xml');
|
||||
self::assertEquals('Liftoff News', $rss->title);
|
||||
@ -41,7 +41,7 @@ class BasicReaderTest extends TestCase
|
||||
self::assertTrue($rss->isValid());
|
||||
}
|
||||
|
||||
public function testChannelImage()
|
||||
public function testChannelImage(): void
|
||||
{
|
||||
$rss = SRSS::read(__DIR__.'/resources/media/cnn.xml');
|
||||
$image = $rss->image;
|
||||
@ -51,7 +51,7 @@ class BasicReaderTest extends TestCase
|
||||
self::assertEquals('https://www.cnn.com/entertainment/index.html', $image->link, var_export($image, true));
|
||||
}
|
||||
|
||||
public function testChannelCategory()
|
||||
public function testChannelCategory(): void
|
||||
{
|
||||
$rss = SRSS::read(__DIR__.'/resources/cloud.xml');
|
||||
$categories = $rss->category;
|
||||
@ -62,7 +62,7 @@ class BasicReaderTest extends TestCase
|
||||
self::assertEquals('rssUpdates', $category->value, var_export($category, true));
|
||||
}
|
||||
|
||||
public function testCloud()
|
||||
public function testCloud(): void
|
||||
{
|
||||
$rss = SRSS::read(__DIR__.'/resources/cloud.xml');
|
||||
$cloud = $rss->cloud;
|
||||
@ -74,7 +74,7 @@ class BasicReaderTest extends TestCase
|
||||
self::assertEquals('xml-rpc', $cloud->protocol, var_export($cloud, true));
|
||||
}
|
||||
|
||||
public function testSource()
|
||||
public function testSource(): void
|
||||
{
|
||||
$rss = SRSS::read(__DIR__.'/resources/basic.xml');
|
||||
$firstItem = $rss->getFirst();
|
||||
@ -85,7 +85,7 @@ class BasicReaderTest extends TestCase
|
||||
self::assertEquals('Tomalak\'s Realm', $source->value);
|
||||
}
|
||||
|
||||
public function testEnclosure()
|
||||
public function testEnclosure(): void
|
||||
{
|
||||
$rss = SRSS::read(__DIR__.'/resources/basic.xml');
|
||||
$item = $rss->getItem(2);
|
||||
|
@ -8,22 +8,22 @@ use Shikiryu\SRSS\SRSS;
|
||||
|
||||
class ExceptionTest extends TestCase
|
||||
{
|
||||
public function testPropertyNotFound()
|
||||
public function testPropertyNotFound(): void
|
||||
{
|
||||
$srss = new SRSS();
|
||||
$this->expectException(PropertyNotFoundException::class);
|
||||
$srss->notfound = 'true';
|
||||
}
|
||||
|
||||
public function testRssNotFound()
|
||||
public function testRssNotFound(): void
|
||||
{
|
||||
$this->expectException(UnreadableRSSException::class);
|
||||
$rss = SRSS::read('not_found.xml');
|
||||
SRSS::read('not_found.xml');
|
||||
}
|
||||
|
||||
public function testMissingChannel()
|
||||
public function testMissingChannel(): void
|
||||
{
|
||||
$this->expectException(ChannelNotFoundInRSSException::class);
|
||||
$rss = SRSS::read(__DIR__ . '/resources/invalid-no-channel.xml');
|
||||
SRSS::read(__DIR__ . '/resources/invalid-no-channel.xml');
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ use Shikiryu\SRSS\SRSS;
|
||||
|
||||
class MediaTest extends TestCase
|
||||
{
|
||||
public function testImages()
|
||||
public function testImages(): void
|
||||
{
|
||||
$rss = SRSS::read(__DIR__.'/resources/media/cnn.xml');
|
||||
self::assertEquals('CNN.com - RSS Channel - Entertainment', $rss->title);
|
||||
@ -17,7 +17,7 @@ class MediaTest extends TestCase
|
||||
self::assertTrue($rss->isValid(), var_export($rss->channel->validated, true));
|
||||
}
|
||||
|
||||
public function testMusicVideo()
|
||||
public function testMusicVideo(): void
|
||||
{
|
||||
$rss = SRSS::read(__DIR__.'/resources/media/music-video.xml');
|
||||
self::assertEquals('Music Videos 101', $rss->title);
|
||||
|
@ -16,7 +16,7 @@ class OriginalReaderSRSSTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testOriginalReader()
|
||||
public function testOriginalReader(): void
|
||||
{
|
||||
$rss = SRSS::read($this->original);
|
||||
self::assertEquals('Liftoff News', $rss->title);
|
||||
|
@ -7,7 +7,7 @@ use Shikiryu\SRSS\SRSSTools;
|
||||
|
||||
class OriginalWriterSRSSTest extends TestCase
|
||||
{
|
||||
public function testOriginalWriter()
|
||||
public function testOriginalWriter(): void
|
||||
{
|
||||
$rss = SRSS::create();
|
||||
$rss->title = 'My Awesome Blog';
|
||||
@ -22,12 +22,12 @@ class OriginalWriterSRSSTest extends TestCase
|
||||
];
|
||||
|
||||
foreach($items as $item){
|
||||
$rssitem = new Item();
|
||||
$rssitem->title = $item["title"];
|
||||
$rssitem->link = $item['link'];
|
||||
$rssitem->pubDate = $item["pubDate"];
|
||||
$rssitem->description = $item["description"];
|
||||
$rss->addItem($rssitem);
|
||||
$rss_item = new Item();
|
||||
$rss_item->title = $item["title"];
|
||||
$rss_item->link = $item['link'];
|
||||
$rss_item->pubDate = $item["pubDate"];
|
||||
$rss_item->description = $item["description"];
|
||||
$rss->addItem($rss_item);
|
||||
}
|
||||
|
||||
$firstItem = new Item();
|
||||
|
Loading…
Reference in New Issue
Block a user