2023-04-05 14:44:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shikiryu\SRSS;
|
|
|
|
|
|
|
|
use Iterator;
|
2023-04-11 14:07:13 +02:00
|
|
|
use Shikiryu\SRSS\Builder\SRSSBuilder;
|
2023-04-06 00:38:26 +02:00
|
|
|
use Shikiryu\SRSS\Entity\Channel;
|
|
|
|
use Shikiryu\SRSS\Entity\Item;
|
2023-04-07 17:57:35 +02:00
|
|
|
use Shikiryu\SRSS\Exception\SRSSException;
|
|
|
|
use Shikiryu\SRSS\Parser\SRSSParser;
|
2023-04-05 14:44:57 +02:00
|
|
|
|
2023-04-06 11:07:06 +02:00
|
|
|
class SRSS implements Iterator
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
2023-04-07 00:35:20 +02:00
|
|
|
public Channel $channel;
|
2023-04-07 17:57:35 +02:00
|
|
|
/** @var Item[] */
|
2023-04-06 11:07:06 +02:00
|
|
|
public array $items; // array of SRSSItems
|
2023-04-05 14:44:57 +02:00
|
|
|
|
2023-04-07 00:35:20 +02:00
|
|
|
private int $position; // Iterator position
|
2023-04-06 00:38:26 +02:00
|
|
|
|
2023-04-05 14:44:57 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2023-04-06 00:38:26 +02:00
|
|
|
$this->items = [];
|
2023-04-05 14:44:57 +02:00
|
|
|
$this->position = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-06 00:38:26 +02:00
|
|
|
* @param string $link url of the rss
|
2023-04-06 11:07:06 +02:00
|
|
|
*
|
2023-04-05 14:44:57 +02:00
|
|
|
* @return SRSS
|
2023-04-06 11:07:06 +02:00
|
|
|
* @throws SRSSException
|
2023-04-05 14:44:57 +02:00
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
public static function read(string $link): SRSS
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
2023-04-06 11:07:06 +02:00
|
|
|
return (new SRSSParser())->parse($link);
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return SRSS
|
2023-04-06 00:38:26 +02:00
|
|
|
* @throws \DOMException
|
2023-04-05 14:44:57 +02:00
|
|
|
*/
|
|
|
|
public static function create()
|
|
|
|
{
|
|
|
|
$doc = new SRSS;
|
2023-04-06 11:07:06 +02:00
|
|
|
|
2023-04-06 00:38:26 +02:00
|
|
|
$doc->channel = new Channel();
|
|
|
|
$doc->items = [];
|
|
|
|
|
2023-04-05 14:44:57 +02:00
|
|
|
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
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
|
|
|
$valid = true;
|
2023-04-07 17:57:35 +02:00
|
|
|
foreach ($this->getItems() as $item) {
|
2023-04-06 11:07:06 +02:00
|
|
|
if ($item->isValid() === false) {
|
2023-04-05 14:44:57 +02:00
|
|
|
$valid = false;
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 00:38:26 +02:00
|
|
|
|
2023-04-06 11:07:06 +02:00
|
|
|
return ($valid && $this->channel->isValid());
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function __isset($name)
|
|
|
|
{
|
2023-04-06 11:07:06 +02:00
|
|
|
return isset($this->channel->{$name});
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setter of others attributes
|
2023-04-06 11:07:06 +02:00
|
|
|
*
|
2023-04-05 14:44:57 +02:00
|
|
|
* @param $name
|
|
|
|
* @param $val
|
2023-04-06 11:07:06 +02:00
|
|
|
*
|
2023-04-05 14:44:57 +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-06 11:07:06 +02:00
|
|
|
throw new SRSSException($name . ' is not a possible item');
|
|
|
|
}
|
2023-04-07 17:57:35 +02:00
|
|
|
// TODO add validator ?
|
|
|
|
// if ((new Validator())->isPropertyValid($this->channel, $name)) {
|
2023-04-06 11:07:06 +02:00
|
|
|
$this->channel->{$name} = $val;
|
2023-04-07 17:57:35 +02:00
|
|
|
// }
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getter of others attributes
|
2023-04-06 11:07:06 +02:00
|
|
|
*
|
2023-04-05 14:44:57 +02:00
|
|
|
* @param $name
|
2023-04-06 11:07:06 +02:00
|
|
|
*
|
2023-04-05 14:44:57 +02:00
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function __get($name)
|
|
|
|
{
|
2023-04-06 11:07:06 +02:00
|
|
|
return $this->channel->{$name} ?? null;
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rewind from Iterator
|
|
|
|
*/
|
2023-04-06 11:07:06 +02:00
|
|
|
public function rewind(): void
|
|
|
|
{
|
2023-04-05 14:44:57 +02:00
|
|
|
$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
|
|
|
{
|
2023-04-05 14:44:57 +02:00
|
|
|
return $this->items[$this->position];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* key from Iterator
|
|
|
|
*/
|
2023-04-06 11:07:06 +02:00
|
|
|
public function key(): int
|
2023-04-06 00:38:26 +02:00
|
|
|
{
|
2023-04-05 14:44:57 +02:00
|
|
|
return $this->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* next from Iterator
|
|
|
|
*/
|
2023-04-06 11:07:06 +02:00
|
|
|
public function next(): void
|
2023-04-06 00:38:26 +02:00
|
|
|
{
|
2023-04-05 14:44:57 +02:00
|
|
|
++$this->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* valid from Iterator
|
|
|
|
*/
|
2023-04-06 11:07:06 +02:00
|
|
|
public function valid(): bool
|
2023-04-06 00:38:26 +02:00
|
|
|
{
|
2023-04-05 14:44:57 +02:00
|
|
|
return isset($this->items[$this->position]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getter of 1st item
|
2023-04-07 00:35:20 +02:00
|
|
|
* @return Item|null
|
2023-04-05 14:44:57 +02:00
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
public function getFirst(): ?Item
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
|
|
|
return $this->getItem(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getter of last item
|
2023-04-06 00:38:26 +02:00
|
|
|
* @return Item
|
2023-04-05 14:44:57 +02:00
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
public function getLast(): Item
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
|
|
|
$items = $this->getItems();
|
2023-04-06 11:07:06 +02:00
|
|
|
|
2023-04-06 00:38:26 +02:00
|
|
|
return $items[array_key_last($items)];
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getter of an item
|
2023-04-06 11:07:06 +02:00
|
|
|
*
|
2023-04-05 14:44:57 +02:00
|
|
|
* @param $i int
|
2023-04-06 00:38:26 +02:00
|
|
|
*
|
|
|
|
* @return Item|null
|
2023-04-05 14:44:57 +02:00
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
public function getItem(int $i): ?Item
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
|
|
|
$i--;
|
2023-04-06 11:07:06 +02:00
|
|
|
|
2023-04-06 00:38:26 +02:00
|
|
|
return $this->items[$i] ?? null;
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getter of all items
|
2023-04-06 00:38:26 +02:00
|
|
|
* @return Item[]
|
2023-04-05 14:44:57 +02:00
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
public function getItems(): array
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* transform current object into an array
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-04-06 00:38:26 +02:00
|
|
|
public function toArray(): array
|
2023-04-05 14:44:57 +02:00
|
|
|
{
|
2023-04-06 00:38:26 +02:00
|
|
|
$doc = $this->channel->toArray();
|
|
|
|
|
2023-04-06 11:07:06 +02:00
|
|
|
foreach ($this->getItems() as $item) {
|
2023-04-05 14:44:57 +02:00
|
|
|
$doc['items'][] = $item->toArray();
|
|
|
|
}
|
2023-04-06 00:38:26 +02:00
|
|
|
|
2023-04-11 14:07:13 +02:00
|
|
|
return array_filter($doc);
|
2023-04-05 14:44:57 +02:00
|
|
|
}
|
2023-04-07 17:57:35 +02:00
|
|
|
|
2023-04-11 14:07:13 +02:00
|
|
|
/**
|
|
|
|
* @param \Shikiryu\SRSS\Entity\Item $rssItem
|
|
|
|
*
|
|
|
|
* @return array|\Shikiryu\SRSS\Entity\Item[]
|
|
|
|
*/
|
|
|
|
public function addItem(Item $rssItem): array
|
2023-04-07 17:57:35 +02:00
|
|
|
{
|
|
|
|
$this->items[] = $rssItem;
|
2023-04-11 14:07:13 +02:00
|
|
|
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Shikiryu\SRSS\Entity\Item $firstItem
|
|
|
|
*
|
|
|
|
* @return array|\Shikiryu\SRSS\Entity\Item[]
|
|
|
|
*/
|
|
|
|
public function addItemBefore(Item $firstItem): array
|
|
|
|
{
|
|
|
|
array_unshift($this->items, $firstItem);
|
|
|
|
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function save(string $path): void
|
|
|
|
{
|
|
|
|
(new SRSSBuilder('1.0', 'UTF-8'))->build($this, $path);
|
2023-04-07 17:57:35 +02:00
|
|
|
}
|
2023-04-11 14:07:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
public function show()
|
|
|
|
{
|
|
|
|
return (new SRSSBuilder('1.0', 'UTF-8'))->show($this);
|
|
|
|
}
|
|
|
|
|
2023-04-06 11:07:06 +02:00
|
|
|
}
|