daily pastebin goal
40%
SHARE
TWEET

XML Playlist Creater

a guest Jan 29th, 2015 1,028 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.     class Playlist
  4.     {
  5.         public $category;
  6.         public $channel;
  7.         private $name;
  8.         private $xml;
  9.  
  10.         public function __construct($title)
  11.         {
  12.             $this->name = $title;
  13.             $this->category = new PlaylistCategory();
  14.             $this->channel = new PlaylistChannel();
  15.         }
  16.  
  17.         public function __toString()
  18.         {
  19.             $this->xml = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><items/>');
  20.             $this->xml->addChild("playlist_name", $this->name, 1);
  21.             $this->addCategories();
  22.             $this->addChannels();
  23.  
  24.             // formatting
  25.             $dom = dom_import_simplexml($this->xml)->ownerDocument;
  26.             $dom->formatOutput = true;
  27.             $xml = (string)$dom->saveXML();
  28.  
  29.             header("Content-Type: application/xml; charset=utf-8");
  30.             return $xml;
  31.         }
  32.  
  33.         private function addCategories()
  34.         {
  35.             $categories = $this->category->getAll();
  36.             foreach ($categories as $key => $category) {
  37.                 if (empty($category)) {
  38.                     continue;
  39.                 }
  40.                 $item = $this->xml->addChild("category");
  41.                 $item->addChild("category_id", $key);
  42.                 $item->addChild("category_title", $category, 1);
  43.             }
  44.         }
  45.  
  46.         private function addChannels()
  47.         {
  48.             $channels = $this->channel->getAll();
  49.             foreach ($channels as $key => $channel) {
  50.                 $item = $this->xml->addChild("channel");
  51.                 foreach ($channel as $name => $value) {
  52.                     if ($name == "category_id") {
  53.                         $value = $this->category->getCategoryId($value);
  54.                     }
  55.                     $item->addChild($name, $value, in_array($name, array("category_id", "search_on")) ? 0 : 1);
  56.                 }
  57.             }
  58.         }
  59.     }
  60.  
  61.     class PlaylistCategory
  62.     {
  63.         private $categories = array(0 => "");
  64.  
  65.         public function add($value)
  66.         {
  67.             $this->categories[] = $value;
  68.         }
  69.  
  70.         public function getAll()
  71.         {
  72.             return $this->categories;
  73.         }
  74.  
  75.         public function getCategoryId($title)
  76.         {
  77.             $index = array_search($title, $this->categories) ?: -1;
  78.             return $index;
  79.         }
  80.     }
  81.  
  82.     class PlaylistChannel
  83.     {
  84.         private $key;
  85.         private $channels = array();
  86.         private $params = array(
  87.             "description" => "description",
  88.             "logo"        => "logo_30x30",
  89.             "url"         => "playlist_url",
  90.             "category"    => "category_id",
  91.             "search"      => "search_on"
  92.         );
  93.  
  94.         public function add($value)
  95.         {
  96.             $this->channels[] = array('title' => $value);
  97.             end($this->channels);
  98.             $this->key = key($this->channels);
  99.         }
  100.  
  101.         public function param($name, $value = "")
  102.         {
  103.             if (!isset($this->params[$name])) {
  104.                 return;
  105.             }
  106.             $name = $this->params[$name];
  107.             $this->channels[$this->key][$name] = $value;
  108.         }
  109.  
  110.         public function getAll()
  111.         {
  112.             return $this->channels;
  113.         }
  114.     }
  115.  
  116.     class SimpleXMLExtended extends SimpleXMLElement
  117.     {
  118.         public function addChild($name, $value = null, $cdata = false)
  119.         {
  120.             if (!empty($value)) {
  121.                 if (!$cdata) {
  122.                     $child = parent::addChild($name, $value);
  123.                 } else {
  124.                     $child = parent::addChild($name);
  125.                     $node = dom_import_simplexml($child);
  126.                     $document = $node->ownerDocument;
  127.                     $node->appendChild($document->createCDATASection($value));
  128.                 }
  129.             } else {
  130.                 $child = parent::addChild($name);
  131.             }
  132.             return $child;
  133.         }
  134.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top