1
0
mirror of https://github.com/Chouchen/ShikiryuRSS.git synced 2024-11-22 19:58:51 +01:00
ShikiryuRSS/src/SRSS.php

288 lines
5.9 KiB
PHP
Raw Normal View History

<?php
namespace Shikiryu\SRSS;
use Iterator;
2023-04-12 00:27:35 +02:00
use ReflectionException;
2023-04-11 14:07:13 +02:00
use Shikiryu\SRSS\Builder\SRSSBuilder;
use Shikiryu\SRSS\Entity\Channel;
2023-04-12 15:28:38 +02:00
use Shikiryu\SRSS\Entity\Channel\Category;
use Shikiryu\SRSS\Entity\Channel\Cloud;
use Shikiryu\SRSS\Entity\Channel\Image;
use Shikiryu\SRSS\Entity\Item;
2023-04-12 00:27:35 +02:00
use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException;
use Shikiryu\SRSS\Exception\PropertyNotFoundException;
use Shikiryu\SRSS\Exception\SRSSException;
2023-04-12 00:27:35 +02:00
use Shikiryu\SRSS\Exception\UnreadableRSSException;
use Shikiryu\SRSS\Parser\SRSSParser;
use Shikiryu\SRSS\Validator\Validator;
2023-04-12 15:28:38 +02:00
/**
* @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
*/
2023-04-06 11:07:06 +02:00
class SRSS implements Iterator
{
2023-04-07 00:35:20 +02:00
public Channel $channel;
2023-04-12 00:27:35 +02:00
/** @var Item[] */
2023-04-06 11:07:06 +02:00
public array $items; // array of SRSSItems
2023-04-07 00:35:20 +02:00
private int $position; // Iterator position
/**
* Constructor
*/
public function __construct()
{
$this->items = [];
$this->position = 0;
}
/**
* @param string $link url of the rss
2023-04-06 11:07:06 +02:00
*
* @return SRSS
2023-04-12 00:27:35 +02:00
* @throws ChannelNotFoundInRSSException
* @throws UnreadableRSSException
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
*/
2023-04-12 00:27:35 +02:00
public static function create(): SRSS
{
$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
*/
2023-04-07 00:35:20 +02:00
public function isValid(): bool
{
try {
$valid = true;
foreach ($this->getItems() as $item) {
if ($item->isValid() === false) {
$valid = false;
}
}
return ($valid && $this->channel->isValid());
2023-04-12 15:28:38 +02:00
} catch (ReflectionException) {
return false;
}
}
/**
* @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-07 00:35:20 +02:00
if (!property_exists(Channel::class, $name)) {
2023-04-12 00:27:35 +02:00
throw new PropertyNotFoundException(Channel::class, $name);
2023-04-06 11:07:06 +02:00
}
if ((new Validator())->isValidValueForObjectProperty($this->channel, $name, $val)) {
2023-04-06 11:07:06 +02:00
$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-11 14:07:13 +02:00
public function current(): Item
2023-04-06 11:07:06 +02:00
{
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
2023-04-07 00:35:20 +02:00
* @return Item|null
*/
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[]
*/
public function getItems(): array
{
return $this->items;
}
/**
* transform current object into an array
* @return array
*/
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();
}
2023-04-11 14:07:13 +02:00
return array_filter($doc);
}
2023-04-11 14:07:13 +02:00
/**
2023-04-12 00:27:35 +02:00
* @param Item $rssItem
2023-04-11 14:07:13 +02:00
*
2023-04-12 00:27:35 +02:00
* @return array|Item[]
2023-04-11 14:07:13 +02:00
*/
public function addItem(Item $rssItem): array
{
$this->items[] = $rssItem;
2023-04-11 14:07:13 +02:00
return $this->items;
}
/**
2023-04-12 00:27:35 +02:00
* @param Item $firstItem
2023-04-11 14:07:13 +02:00
*
2023-04-12 00:27:35 +02:00
* @return array|Item[]
2023-04-11 14:07:13 +02:00
*/
public function addItemBefore(Item $firstItem): array
{
array_unshift($this->items, $firstItem);
return $this->items;
}
/**
* @param string $path
*
* @return void
2023-04-12 15:28:38 +02:00
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
2023-04-11 14:07:13 +02:00
*/
public function save(string $path): void
{
(new SRSSBuilder('1.0', 'UTF-8'))->build($this, $path);
}
2023-04-11 14:07:13 +02:00
/**
* @return false|string
2023-04-12 15:28:38 +02:00
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
2023-04-11 14:07:13 +02:00
*/
2023-04-12 15:28:38 +02:00
public function show(): bool|string
2023-04-11 14:07:13 +02:00
{
return (new SRSSBuilder('1.0', 'UTF-8'))->show($this);
}
2023-04-06 11:07:06 +02:00
}