1
0
mirror of https://github.com/Chouchen/ShikiryuRSS.git synced 2024-12-23 05:01:36 +01:00
ShikiryuRSS/src/SRSS.php

240 lines
4.7 KiB
PHP
Raw Normal View History

<?php
namespace Shikiryu\SRSS;
use Iterator;
use Shikiryu\SRSS\Entity\Channel;
use Shikiryu\SRSS\Entity\Item;
2023-04-06 11:07:06 +02:00
class SRSS implements Iterator
{
2023-04-06 11:07:06 +02:00
public array $items; // array of SRSSItems
protected $attr; // array of RSS attributes
private $position; // Iterator position
2023-04-06 11:07:06 +02:00
public Channel $channel;
// lists of possible attributes for RSS
protected $possibleAttr = [
2023-04-06 11:07:06 +02:00
'title' => 'nohtml',
'link' => 'link',
'description' => 'html',
'language' => '',
//'language' => 'lang',
2023-04-06 11:07:06 +02:00
'copyright' => 'nohtml',
'pubDate' => 'date',
'lastBuildDate' => 'date',
'category' => 'nohtml',
'docs' => 'link',
'cloud' => '',
'generator' => 'nohtml',
'managingEditor' => 'email',
'webMaster' => 'email',
'ttl' => 'int',
'image' => '',
'rating' => 'nohtml',
//'textInput' => '',
2023-04-06 11:07:06 +02:00
'skipHours' => 'hour',
'skipDays' => 'day',
];
/**
* Constructor
*/
public function __construct()
{
$this->attr = [];
$this->items = [];
$this->position = 0;
}
/**
* @param string $link url of the rss
2023-04-06 11:07:06 +02:00
*
* @return SRSS
2023-04-06 11:07:06 +02:00
* @throws SRSSException
*/
public static function read(string $link): SRSS
{
2023-04-06 11:07:06 +02:00
return (new SRSSParser())->parse($link);
}
/**
* @return SRSS
* @throws \DOMException
*/
public static function create()
{
$doc = new SRSS;
2023-04-06 11:07:06 +02:00
$doc->channel = new Channel();
$doc->items = [];
return $doc;
}
/**
* check if current RSS is a valid one (based on specifications)
* @return bool
* TODO use required
*/
public function isValid()
{
$valid = true;
$items = $this->getItems();
$invalidItems = [];
$i = 1;
2023-04-06 11:07:06 +02:00
foreach ($items as $item) {
if ($item->isValid() === false) {
$invalidItems[] = $i;
$valid = false;
}
$i++;
}
2023-04-06 11:07:06 +02:00
return ($valid && $this->channel->isValid());
}
/**
* @param $name
*
* @return bool
*/
public function __isset($name)
{
2023-04-06 11:07:06 +02:00
return isset($this->channel->{$name});
}
/**
* setter of others attributes
2023-04-06 11:07:06 +02:00
*
* @param $name
* @param $val
2023-04-06 11:07:06 +02:00
*
* @throws SRSSException
*/
public function __set($name, $val)
{
2023-04-06 11:07:06 +02:00
if (!array_key_exists($name, $this->possibleAttr)) {
throw new SRSSException($name . ' is not a possible item');
}
$flag = $this->possibleAttr[$name];
$val = SRSSTools::check($val, $flag);
if (!empty($val)) {
$this->channel->{$name} = $val;
}
}
/**
* getter of others attributes
2023-04-06 11:07:06 +02:00
*
* @param $name
2023-04-06 11:07:06 +02:00
*
* @return null|string
*/
public function __get($name)
{
2023-04-06 11:07:06 +02:00
return $this->channel->{$name} ?? null;
}
/**
* rewind from Iterator
*/
2023-04-06 11:07:06 +02:00
public function rewind(): void
{
$this->position = 0;
}
/**
* current from Iterator
*/
2023-04-06 11:07:06 +02:00
public function current()
{
return $this->items[$this->position];
}
/**
* key from Iterator
*/
2023-04-06 11:07:06 +02:00
public function key(): int
{
return $this->position;
}
/**
* next from Iterator
*/
2023-04-06 11:07:06 +02:00
public function next(): void
{
++$this->position;
}
/**
* valid from Iterator
*/
2023-04-06 11:07:06 +02:00
public function valid(): bool
{
return isset($this->items[$this->position]);
}
/**
* getter of 1st item
* @return Item
*/
public function getFirst(): ?Item
{
return $this->getItem(1);
}
/**
* getter of last item
* @return Item
*/
public function getLast(): Item
{
$items = $this->getItems();
2023-04-06 11:07:06 +02:00
return $items[array_key_last($items)];
}
/**
* getter of an item
2023-04-06 11:07:06 +02:00
*
* @param $i int
*
* @return Item|null
*/
public function getItem(int $i): ?Item
{
$i--;
2023-04-06 11:07:06 +02:00
return $this->items[$i] ?? null;
}
/**
* getter of all items
* @return Item[]
* @throws SRSSException
*/
public function getItems(): array
{
return $this->items;
}
/**
* transform current object into an array
* @return array
* @throws SRSSException
*/
public function toArray(): array
{
$doc = $this->channel->toArray();
2023-04-06 11:07:06 +02:00
foreach ($this->getItems() as $item) {
$doc['items'][] = $item->toArray();
}
return $doc;
}
2023-04-06 11:07:06 +02:00
}