Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
127 lines (77 sloc) 2.58 KB
<?php
abstract class Feed {
public $title;
public $link;
public $description;
public $docs;
public $language;
protected $items;
protected function __construct ($title, $link, $description, $docs, $language) {
$this->writer = new XMLWriter ();
$this->writer->openMemory ();
$this->writer->setIndent (true);
$this->writer->startDocument ('1.0', 'UTF-8');
$this->title = $title;
$this->link = $link;
$this->description = $description;
$this->docs = $docs;
$this->language = $language;
}
public function __toString () {
$this->build ();
return $this->writer->flush ();
}
public function output () {
header ('Content-type: text/xml; charset=utf-8');
print $this->__toString ();
}
public function addStylesheet ($type, $href) {
$this->writer->writePi ('xml-stylesheet', 'type="'.$type.'" href="'.$href.'"');
}
public function addItem (FeedItem $item) {
$this->items[] = $item;
}
abstract protected function build ();
}
class FeedItem {
public $title;
public $link;
public $unique_id;
public $description;
public $publish_date;
public function __construct ($title, $link, $unique_id, $description, $publish_date) {
$this->title = $title;
$this->link = $link;
$this->unique_id = $unique_id;
$this->description = $description;
$this->publish_date = $publish_date;
}
}
class Rss2Feed extends Feed {
public function __construct ($title, $link, $description = '', $docs = '', $language = 'en-us') {
parent::__construct ($title, $link, $description, $docs, $language);
}
protected function build () {
$this->writer->startElement ('rss');
$this->writer->writeAttribute ('version', '2.0');
$this->writer->startElement ('channel');
$this->writer->writeElement ('title', $this->title);
$this->writer->writeElement ('link', $this->link);
$this->writer->writeElement ('description', $this->description);
$this->writer->writeElement ('docs', $this->docs);
$this->writer->writeElement ('language', $this->language);
$this->writer->writeElement ('pubDate', date (DATE_RFC2822));
foreach ($this->items as $item) {
$this->writer->startElement ('item');
$this->writer->writeElement ('title', $item->title);
$this->writer->writeElement ('link', $item->link);
$this->writer->writeElement ('guid', $item->unique_id);
$this->writer->writeElement ('description', $item->description);
$this->writer->writeElement ('pubDate', date (DATE_RFC2822, $item->publish_date));
$this->writer->endElement ();
}
$this->writer->endElement ();
$this->writer->endElement ();
}
}
?>