mirror of
https://github.com/Chouchen/ShikiryuRSS.git
synced 2026-11-24 01:01:18 +01:00
1 parent
fec8c122e3
commit
63d0b03b50
18 files changed
+494
-234
No files matched your search
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\SRSS\Parser;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMException;
|
||||
use DOMNode;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Exception\SRSSException;
|
||||
use Shikiryu\SRSS\SRSSTools;
|
||||
|
||||
/**
|
||||
* @property string|null $description
|
||||
*/
|
||||
class ItemParser extends DomDocument
|
||||
{
|
||||
|
||||
protected DOMNode $node; // item node
|
||||
protected $attr; // item's properties
|
||||
|
||||
// possible properties' names
|
||||
protected static $possibilities = [
|
||||
'title' => 'nohtml',
|
||||
'link' => 'link',
|
||||
'description' => 'html',
|
||||
'author' => 'email',
|
||||
'category' => 'nohtml',
|
||||
'comments' => 'link',
|
||||
'enclosure' => '',
|
||||
'guid' => 'nohtml',
|
||||
'pubDate' => 'date',
|
||||
'source' => 'link',
|
||||
'media:group' => 'folder',
|
||||
'media:content' => '',
|
||||
];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param DomNode $node
|
||||
*/
|
||||
public function __construct($node = null)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
* @param $nodes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private static function _loadChildAttributes(Item $item, $nodes): void
|
||||
{
|
||||
foreach ($nodes->childNodes as $child) {
|
||||
if ($child->nodeType === XML_ELEMENT_NODE && $child->nodeName !== 'item') {
|
||||
if (array_key_exists($child->nodeName, self::$possibilities) && self::$possibilities[$child->nodeName] === 'folder') {
|
||||
self::_loadChildAttributes($item, $child);
|
||||
} elseif ($child->nodeName === 'media:content') {
|
||||
$item->medias[] = MediaContentParser::read($child);
|
||||
} else {
|
||||
$item->{$child->nodeName} = trim($child->nodeValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DOMNode|null $node
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public static function read(?DOMNode $node = null): Item
|
||||
{
|
||||
$item = new Item();
|
||||
if ($node instanceof DOMNode) {
|
||||
self::_loadChildAttributes($item, $node);
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter of item DomElement
|
||||
*/
|
||||
public function getItem(): ?\DOMNode
|
||||
{
|
||||
$this->appendChild($this->node);
|
||||
|
||||
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
|
||||
*/
|
||||
public function isValid(): bool
|
||||
{
|
||||
return $this->description != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset($name)
|
||||
{
|
||||
return isset($this->attr[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* main setter for properties
|
||||
*
|
||||
* @param $name
|
||||
* @param $val
|
||||
*
|
||||
* @throws SRSSException|DOMException
|
||||
*/
|
||||
public function __set($name, $val)
|
||||
{
|
||||
if (!array_key_exists($name, $this->possibilities)) {
|
||||
throw new SRSSException(sprintf('%s is not a possible item (%s)', $name, implode(', ', array_keys($this->possibilities))));
|
||||
}
|
||||
|
||||
$flag = $this->possibilities[$name];
|
||||
if ($flag !== '')
|
||||
$val = SRSSTools::check($val, $flag);
|
||||
if (!empty($val)) {
|
||||
if ($val instanceof DOMElement) {
|
||||
$this->node->appendChild($val);
|
||||
} elseif ($this->$name == null) {
|
||||
$this->node->appendChild(new DomElement($name, $val));
|
||||
}
|
||||
$this->attr[$name] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* main getter for properties
|
||||
*
|
||||
* @param $name
|
||||
*
|
||||
* @return null|string
|
||||
* @throws SRSSException
|
||||
*/
|
||||
public function __get($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;
|
||||
|
||||
return $tmp->item(0)->nodeValue;
|
||||
}
|
||||
|
||||
throw new SRSSException(sprintf('%s is not a possible item (%s)', $name, implode(', ', array_keys($this->possibilities))));
|
||||
}
|
||||
|
||||
/**
|
||||
* transform current item's object into an array
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$infos = [];
|
||||
foreach ($this->attr as $attrName => $attrVal) {
|
||||
$infos[$attrName] = $attrVal;
|
||||
}
|
||||
|
||||
return $infos;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\SRSS\Parser;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMException;
|
||||
use DOMNode;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Entity\Media\Content;
|
||||
use Shikiryu\SRSS\Exception\SRSSException;
|
||||
use Shikiryu\SRSS\SRSSTools;
|
||||
|
||||
/**
|
||||
* @property string|null $description
|
||||
*/
|
||||
class MediaContentParser extends DomDocument
|
||||
{
|
||||
protected DOMNode $node; // item node
|
||||
protected $attr; // item's properties
|
||||
|
||||
// possible properties' names
|
||||
protected static $possibilities = [
|
||||
'title' => 'nohtml',
|
||||
'link' => 'link',
|
||||
'description' => 'html',
|
||||
'author' => 'email',
|
||||
'category' => 'nohtml',
|
||||
'comments' => 'link',
|
||||
'enclosure' => '',
|
||||
'guid' => 'nohtml',
|
||||
'pubDate' => 'date',
|
||||
'source' => 'link',
|
||||
'media:group' => 'folder',
|
||||
'media:content' => '',
|
||||
];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param DomNode $node
|
||||
*/
|
||||
public function __construct($node = null)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Content $item
|
||||
* @param $nodes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private static function _loadChildAttributes(Content $item, $nodes): void
|
||||
{
|
||||
foreach ($nodes->attributes as $attribute) {
|
||||
|
||||
if (property_exists(Content::class, $attribute->name)) {
|
||||
$item->{$attribute->name} = $attribute->value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DOMNode|null $node
|
||||
*
|
||||
* @return Content
|
||||
*/
|
||||
public static function read(?DOMNode $node = null): Content
|
||||
{
|
||||
$content = new Content();
|
||||
if ($node instanceof DOMNode) {
|
||||
self::_loadChildAttributes($content, $node);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\SRSS\Parser;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMNodeList;
|
||||
use DOMXPath;
|
||||
use Shikiryu\SRSS\Entity\Channel;
|
||||
use Shikiryu\SRSS\Entity\Channel\Image;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Exception\SRSSException;
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
* manage of libxml errors
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
foreach (libxml_get_errors() as $error) {
|
||||
error_log($error->message, 3, 'error.log');
|
||||
}
|
||||
libxml_clear_errors();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $link
|
||||
*
|
||||
* @return \Shikiryu\SRSS\SRSS
|
||||
* @throws \Shikiryu\SRSS\Exception\SRSSException
|
||||
*/
|
||||
public function parse(string $link)
|
||||
{
|
||||
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');
|
||||
if($channel->length === 1){ // Good URL and good RSS
|
||||
$this->_loadAttributes(); // loading channel properties
|
||||
$this->getItems(); // loading all items
|
||||
|
||||
return $this->doc;
|
||||
}
|
||||
|
||||
throw new SRSSException('invalid file '.$link);
|
||||
}
|
||||
|
||||
throw new SRSSException('Can not open file '.$link);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item[]
|
||||
* @throws \Shikiryu\SRSS\Exception\SRSSException
|
||||
*/
|
||||
private function getItems(): array
|
||||
{
|
||||
$channel = $this->_getChannel();
|
||||
/** @var DOMNodeList $items */
|
||||
$items = $channel->getElementsByTagName('item');
|
||||
$length = $items->length;
|
||||
$this->doc->items = [];
|
||||
if ($length > 0) {
|
||||
for($i = 0; $i < $length; $i++) {
|
||||
$this->doc->items[$i] = ItemParser::read($items->item($i));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->doc->items;
|
||||
}
|
||||
/**
|
||||
* putting all RSS attributes into the object
|
||||
* @throws SRSSException
|
||||
*/
|
||||
private function _loadAttributes(): void
|
||||
{
|
||||
$node_channel = $this->_getChannel();
|
||||
$this->doc->channel = new Channel();
|
||||
|
||||
foreach($node_channel->childNodes as $child) {
|
||||
if ($child->nodeType === XML_ELEMENT_NODE && $child->nodeName !== 'item') {
|
||||
if($child->nodeName === 'image') {
|
||||
$image = new Image();
|
||||
foreach($child->childNodes as $children) {
|
||||
if($children->nodeType == XML_ELEMENT_NODE) {
|
||||
$image->{$child->nodeName} = $children->nodeValue;
|
||||
}
|
||||
}
|
||||
$this->doc->channel->image = $image;
|
||||
|
||||
} else {
|
||||
$this->doc->channel->{$child->nodeName} = $child->nodeValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getter of current RSS channel
|
||||
* @return \DOMNode
|
||||
* @throws SRSSException
|
||||
*/
|
||||
private function _getChannel(): \DOMNode
|
||||
{
|
||||
$channel = $this->getElementsByTagName('channel');
|
||||
if($channel->length != 1) {
|
||||
throw new SRSSException('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]];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user