2023-04-05 14:44:57 +02:00
|
|
|
<?php
|
|
|
|
|
2023-04-07 17:57:35 +02:00
|
|
|
namespace Shikiryu\SRSS\Parser;
|
2023-04-05 14:44:57 +02:00
|
|
|
|
|
|
|
use DOMDocument;
|
|
|
|
use DOMElement;
|
2023-04-06 00:38:26 +02:00
|
|
|
use DOMException;
|
|
|
|
use DOMNode;
|
|
|
|
use Shikiryu\SRSS\Entity\Item;
|
2023-04-07 17:57:35 +02:00
|
|
|
use Shikiryu\SRSS\Exception\SRSSException;
|
|
|
|
use Shikiryu\SRSS\SRSSTools;
|
2023-04-05 14:44:57 +02:00
|
|
|
|
2023-04-06 00:38:26 +02:00
|
|
|
/**
|
|
|
|
* @property string|null $description
|
|
|
|
*/
|
2023-04-07 17:57:35 +02:00
|
|
|
class ItemParser extends DomDocument
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
2023-04-06 00:38:26 +02:00
|
|
|
|
|
|
|
protected DOMNode $node; // item node
|
2023-04-05 14:44:57 +02:00
|
|
|
protected $attr; // item's properties
|
|
|
|
|
|
|
|
// possible properties' names
|
2023-04-06 00:38:26 +02:00
|
|
|
protected static $possibilities = [
|
2023-04-05 14:44:57 +02:00
|
|
|
'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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-06 00:38:26 +02:00
|
|
|
* @param Item $item
|
2023-04-05 14:44:57 +02:00
|
|
|
* @param $nodes
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
private static function _loadChildAttributes(Item $item, $nodes): void
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
2023-04-06 00:38:26 +02:00
|
|
|
foreach ($nodes->childNodes as $child) {
|
2023-04-05 14:44:57 +02:00
|
|
|
if ($child->nodeType === XML_ELEMENT_NODE && $child->nodeName !== 'item') {
|
2023-04-06 00:38:26 +02:00
|
|
|
if (array_key_exists($child->nodeName, self::$possibilities) && self::$possibilities[$child->nodeName] === 'folder') {
|
|
|
|
self::_loadChildAttributes($item, $child);
|
|
|
|
} elseif ($child->nodeName === 'media:content') {
|
2023-04-07 17:57:35 +02:00
|
|
|
$item->medias[] = MediaContentParser::read($child);
|
2023-04-05 14:44:57 +02:00
|
|
|
} else {
|
2023-04-07 17:57:35 +02:00
|
|
|
$item->{$child->nodeName} = trim($child->nodeValue);
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 00:38:26 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2023-04-05 14:44:57 +02:00
|
|
|
/**
|
|
|
|
* getter of item DomElement
|
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
public function getItem(): ?\DOMNode
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
|
|
|
$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
|
2023-04-06 00:38:26 +02:00
|
|
|
* @throws DOMException
|
2023-04-05 14:44:57 +02:00
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
public function setEnclosure(string $url, int $length, string $type): void
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
public function isValid(): bool
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
2023-04-06 00:38:26 +02:00
|
|
|
return $this->description != null;
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function __isset($name)
|
|
|
|
{
|
|
|
|
return isset($this->attr[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* main setter for properties
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* @param $val
|
|
|
|
*
|
2023-04-06 00:38:26 +02:00
|
|
|
* @throws SRSSException|DOMException
|
2023-04-05 14:44:57 +02:00
|
|
|
*/
|
|
|
|
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)) {
|
2023-04-06 00:38:26 +02:00
|
|
|
if ($val instanceof DOMElement) {
|
|
|
|
$this->node->appendChild($val);
|
|
|
|
} elseif ($this->$name == null) {
|
2023-04-05 14:44:57 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|