Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Playlist
- {
- public $category;
- public $channel;
- private $name;
- private $xml;
- public function __construct($title)
- {
- $this->name = $title;
- $this->category = new PlaylistCategory();
- $this->channel = new PlaylistChannel();
- }
- public function __toString()
- {
- $this->xml = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><items/>');
- $this->xml->addChild("playlist_name", $this->name, 1);
- $this->addCategories();
- $this->addChannels();
- // formatting
- $dom = dom_import_simplexml($this->xml)->ownerDocument;
- $dom->formatOutput = true;
- $xml = (string)$dom->saveXML();
- header("Content-Type: application/xml; charset=utf-8");
- return $xml;
- }
- private function addCategories()
- {
- $categories = $this->category->getAll();
- foreach ($categories as $key => $category) {
- if (empty($category)) {
- continue;
- }
- $item = $this->xml->addChild("category");
- $item->addChild("category_id", $key);
- $item->addChild("category_title", $category, 1);
- }
- }
- private function addChannels()
- {
- $channels = $this->channel->getAll();
- foreach ($channels as $key => $channel) {
- $item = $this->xml->addChild("channel");
- foreach ($channel as $name => $value) {
- if ($name == "category_id") {
- $value = $this->category->getCategoryId($value);
- }
- $item->addChild($name, $value, in_array($name, array("category_id", "search_on")) ? 0 : 1);
- }
- }
- }
- }
- class PlaylistCategory
- {
- private $categories = array(0 => "");
- public function add($value)
- {
- $this->categories[] = $value;
- }
- public function getAll()
- {
- return $this->categories;
- }
- public function getCategoryId($title)
- {
- $index = array_search($title, $this->categories) ?: -1;
- return $index;
- }
- }
- class PlaylistChannel
- {
- private $key;
- private $channels = array();
- private $params = array(
- "description" => "description",
- "logo" => "logo_30x30",
- "url" => "playlist_url",
- "category" => "category_id",
- "search" => "search_on"
- );
- public function add($value)
- {
- $this->channels[] = array('title' => $value);
- end($this->channels);
- $this->key = key($this->channels);
- }
- public function param($name, $value = "")
- {
- if (!isset($this->params[$name])) {
- return;
- }
- $name = $this->params[$name];
- $this->channels[$this->key][$name] = $value;
- }
- public function getAll()
- {
- return $this->channels;
- }
- }
- class SimpleXMLExtended extends SimpleXMLElement
- {
- public function addChild($name, $value = null, $cdata = false)
- {
- if (!empty($value)) {
- if (!$cdata) {
- $child = parent::addChild($name, $value);
- } else {
- $child = parent::addChild($name);
- $node = dom_import_simplexml($child);
- $document = $node->ownerDocument;
- $node->appendChild($document->createCDATASection($value));
- }
- } else {
- $child = parent::addChild($name);
- }
- return $child;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement