miniflux-legacy/vendor/fguillot/picofeed/lib/PicoFeed/Serialization/Export.php

123 lines
3.0 KiB
PHP
Raw Normal View History

2013-02-18 03:48:21 +01:00
<?php
namespace PicoFeed\Serialization;
2013-02-18 03:48:21 +01:00
2014-05-20 20:20:27 +02:00
use SimpleXMLElement;
/**
* OPML export class.
2014-05-20 20:20:27 +02:00
*
* @author Frederic Guillot
*/
2013-02-18 03:48:21 +01:00
class Export
{
2014-05-20 20:20:27 +02:00
/**
* List of feeds to exports.
2014-05-20 20:20:27 +02:00
*
* @var array
*/
2013-02-18 03:48:21 +01:00
private $content = array();
2014-05-20 20:20:27 +02:00
/**
* List of required properties for each feed.
2014-05-20 20:20:27 +02:00
*
* @var array
*/
private $required_fields = array(
'title',
'site_url',
2014-05-20 20:20:27 +02:00
'feed_url',
);
2014-05-20 20:20:27 +02:00
/**
* Constructor.
2014-05-20 20:20:27 +02:00
*
* @param array $content List of feeds
2014-05-20 20:20:27 +02:00
*/
2013-02-18 03:48:21 +01:00
public function __construct(array $content)
{
$this->content = $content;
}
2014-05-20 20:20:27 +02:00
/**
* Get the OPML document.
2014-05-20 20:20:27 +02:00
*
* @return string
*/
2013-02-18 03:48:21 +01:00
public function execute()
{
2014-05-20 20:20:27 +02:00
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><opml/>');
2013-02-18 03:48:21 +01:00
$head = $xml->addChild('head');
$head->addChild('title', 'OPML Export');
$body = $xml->addChild('body');
2014-10-19 20:42:31 +02:00
foreach ($this->content as $category => $values) {
if (is_string($category)) {
$this->createCategory($body, $category, $values);
} else {
2014-10-19 20:42:31 +02:00
$this->createEntry($body, $values);
}
}
2014-10-19 20:42:31 +02:00
return $xml->asXML();
}
2014-10-19 20:42:31 +02:00
/**
* Create a feed entry.
2014-10-19 20:42:31 +02:00
*
* @param SimpleXMLElement $parent Parent Element
* @param array $feed Feed properties
2014-10-19 20:42:31 +02:00
*/
public function createEntry(SimpleXMLElement $parent, array $feed)
{
$valid = true;
2014-10-19 20:42:31 +02:00
foreach ($this->required_fields as $field) {
if (!isset($feed[$field])) {
2014-10-19 20:42:31 +02:00
$valid = false;
break;
2014-05-20 20:20:27 +02:00
}
2014-10-19 20:42:31 +02:00
}
2014-10-19 20:42:31 +02:00
if ($valid) {
$outline = $parent->addChild('outline');
2013-02-18 03:48:21 +01:00
$outline->addAttribute('xmlUrl', $feed['feed_url']);
$outline->addAttribute('htmlUrl', $feed['site_url']);
$outline->addAttribute('title', $feed['title']);
$outline->addAttribute('text', $feed['title']);
$outline->addAttribute('description', isset($feed['description']) ? $feed['description'] : $feed['title']);
$outline->addAttribute('type', 'rss');
$outline->addAttribute('version', 'RSS');
}
2014-10-19 20:42:31 +02:00
}
2013-02-18 03:48:21 +01:00
2014-10-19 20:42:31 +02:00
/**
* Create entries for a feed list.
2014-10-19 20:42:31 +02:00
*
* @param SimpleXMLElement $parent Parent Element
* @param array $feeds Feed list
2014-10-19 20:42:31 +02:00
*/
public function createEntries(SimpleXMLElement $parent, array $feeds)
{
foreach ($feeds as $feed) {
$this->createEntry($parent, $feed);
}
}
/**
* Create a category entry.
2014-10-19 20:42:31 +02:00
*
* @param SimpleXMLElement $parent Parent Element
* @param string $category Category
* @param array $feeds Feed properties
2014-10-19 20:42:31 +02:00
*/
public function createCategory(SimpleXMLElement $parent, $category, array $feeds)
{
$outline = $parent->addChild('outline');
$outline->addAttribute('text', $category);
$this->createEntries($outline, $feeds);
2013-02-18 03:48:21 +01:00
}
2014-05-20 20:20:27 +02:00
}