feat: abstract generating rss feed using RSSFeed class

This commit is contained in:
Yassine Doghri 2024-05-07 16:03:07 +00:00
parent bb03798aff
commit 17865dab2b
5 changed files with 93 additions and 12 deletions

View File

@ -40,6 +40,7 @@ class Autoload extends AutoloadConfig
*/
public $psr4 = [
APP_NAMESPACE => APPPATH,
'Themes' => ROOTPATH . 'themes',
'Modules' => ROOTPATH . 'modules/',
'Modules\Admin' => ROOTPATH . 'modules/Admin/',
'Modules\Analytics' => ROOTPATH . 'modules/Analytics/',
@ -55,7 +56,7 @@ class Autoload extends AutoloadConfig
'Modules\PremiumPodcasts' => ROOTPATH . 'modules/PremiumPodcasts/',
'Modules\Update' => ROOTPATH . 'modules/Update/',
'Modules\WebSub' => ROOTPATH . 'modules/WebSub/',
'Themes' => ROOTPATH . 'themes',
'RSSFeed' => APPPATH . 'Libraries/RSSFeed',
'ViewComponents' => APPPATH . 'Libraries/ViewComponents/',
'ViewThemes' => APPPATH . 'Libraries/ViewThemes/',
'Vite' => APPPATH . 'Libraries/Vite/',

View File

@ -37,16 +37,6 @@ if (! function_exists('get_rss_feed')) {
$episodes = $podcast->episodes;
$itunesNamespace = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
$podcastNamespace = 'https://podcastindex.org/namespace/1.0';
$atomNamespace = 'http://www.w3.org/2005/Atom';
$rss = new SimpleRSSElement(
"<?xml version='1.0' encoding='utf-8'?><rss version='2.0' xmlns:itunes='{$itunesNamespace}' xmlns:podcast='{$podcastNamespace}' xmlns:atom='{$atomNamespace}' xmlns:content='http://purl.org/rss/1.0/modules/content/'></rss>"
);
$channel = $rss->addChild('channel');
$atomLink = $channel->addChild('link', null, $atomNamespace);

View File

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/**
* @copyright 2024 Ad Aures
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Libraries\RSSFeed;
class RSSFeed
{
public const NAMESPACES = [
'itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd',
'podcast' => 'https://podcastindex.org/namespace/1.0',
'atom' => 'http://www.w3.org/2005/Atom',
];
/**
* @var Tag[]
*/
private array $tags = [];
private SimpleRSSElement $root;
private SimpleRSSElement $channel;
public function findTag(string $name): Tag
{
}
public function render(): string
{
$this->root = new SimpleRSSElement(
"<rss version='2.0' xmlns:content='http://purl.org/rss/1.0/modules/content/' />"
);
foreach ($this::NAMESPACES as $key => $namespace) {
$this->root->addAttribute('xmlns:' . $key, $namespace);
}
$this->channel = $this->root->addChild('channel');
/** @var string|false $xml */
$xml = $this->root->asXML();
if (! $xml) {
return ''; // TODO: show exception?
}
return $xml;
}
/**
* @param array{string,string}[] $attributes
* @param key-of<self::NAMESPACES> $namespace
*/
private function addTag(string $name, string $value, $attributes, $namespace)
{
$this->tags[] = new Tag($name, $value, $attributes, $namespace);
}
}

View File

@ -8,7 +8,7 @@ declare(strict_types=1);
* @link https://castopod.org/
*/
namespace App\Libraries;
namespace App\Libraries\RSSFeed;
use DOMDocument;
use SimpleXMLElement;

View File

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
/**
* @copyright 2024 Ad Aures
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Libraries\RSSFeed;
class Tag
{
/**
* @param array{string,string}[] $attributes
* @param key-of<RSSFeed::NAMESPACES> $namespace
*/
public function __construct(
private string $name,
private string $value,
private array $attributes,
private string $namespace
) {
}
}