<?php error_reporting(E_ALL);
/**
* This file is part of the Dynamic Networks RSS Framework for PHP
*
* @author Gordon Oheim (hide@address.com)
* @copyright Copyright 2005, Dynamic Networks
* @link http://www.dynamicnetworks.de
* @license http://creativecommons.org/licenses/GPL/2.0/ GNU General Public License
* @filesource
* @version 1.0.0
*/
require_once("parser.php");
/**
* This class is a parser for creating an RSS feed from the framework objects.
*
* The class inherits from parser. Use this, if you want to create the RSS feed
* directly from this class and not via a transformation. Although the code
* is fully functional, the use of this class should be avoided. Use the
* transformer class instead.
*
* @deprecated 1.0.0 - 05.01.2005
* @package rssFramework
* @author Gordon Oheim (hide@address.com)
* @see transformer
* @see parser
*/
class mapper extends parser {
/**
* Transforms a feed object into an RSS SML document.
*
* This function adds a new node called RSS as the root element to the class
* attribute $dom.
* If the param setPubDate is set to true, the function will update the
* $channel::pubDate attribute to the current date.
* If a channel object is attached to the feed, the function will send it to
* $channel::append_object and run through all attributes and objects
* attached to it, creating the entire RSS document.
*
* @param feed $feed
* @param bool $setPubDate
* @return DomDocument
* @see append_object()
* @see $channel::set_pubDate()
* @access public
*/
function create_new_feed($feed, $setPubDate = true) {
if (get_class($feed) != "feed") {
die ("FATAL ERROR: param must be object of type feed.");
} else {
/* create a root element and add version as attribute */
$root = $this->dom->create_element("rss");
$root->set_attribute("version", $feed->version);
if (isset($feed->channel) && is_a($feed->channel, "channel")) {
/* set pubDate to current date and time */
if ($setPubDate == true) {
$feed->channel->set_pubDate(time());
}
/* then start parsing the channel object */
$this->append_object($root, $feed->channel);
}
/* finally append the root node to dom */
$this->dom->append_child($root);
return $this->dom;
}
}
/**
* Creates DomNodes from Object attributes
*
* This function runs through all set object vars and appends them to the
* parent node specified by the param $parent. Depending on the var type,
* the function will decide whether to call itself, an array parsing
* function or simply add the found var.
* If there is a function called make_[classname] available, the object will
* be processed by that function.
*
* @param DomNode $parent the objects parent object
* @param object $object the object to be parsed
* @see make_category()
* @see make_cloud()
* @see make_email()
* @see make_enclosure()
* @see make_guid()
* @see make_url()
* @see append_array()
* @see append_var()
* @access public
*/
function append_object(&$parent, $object) {
/* create a new node with the object class name */
$child = $this->dom->create_element(get_class($object));
/* get the set object vars */
$elements = get_object_vars($object);
/* run through the set object vars */
foreach($elements as $key=>$val) {
/* check element type */
switch(true) {
/* if element is an object, map or recurse */
case is_object($val): {
/* if there is a mapper for this object call it */
if (method_exists($this, "make_".get_class($val))) {
$func = "make_".get_class($val);
$this->$func($child, $key, $val);
/* otherwise shove it into this function again */
} else {
$this->append_object($child, $val);
}
break;
}
/* if element is an array, call append_array */
case is_array($val): {
$this->append_array($child, $key, $val);
break;
}
/* if element is another datatype, append it */
default: {
$this->append_var($child, $key, $val);
}
}
}
/* append the object to the parent node */
$parent->append_child($child);
}
/**
* Appends array valued as DomNodes to the parser's DomDocument
*
* This function runs through an array and appends the array items to an
* array node which is then appended to the calling functions parent node.
* If an array item is an object or another array, the function will either
* call $parser::append_object or $parser::append_array() again.
* The first param specifies the parent node, where the array values are to
* appended. The second param specifies the attribute the array was attached
* to and will function as the DomNode name. The third param is an array
* containing the array values.
*
* @param DomNode $parent
* @param string $child
* @param mixed[] $items
* @see append_var()
* @see append_object()
* @access public
*/
function append_array(&$parent, $child, $items) {
/* quick and dirty solution for skipDays and skipHours */
$element = $child;
/* suppress extra tags for items, categories and enclosures */
if (($child == "items") || ($child == "enclosures") || ($child == "categories")) {
$child =& $parent;
} else {
$child = $this->dom->create_element($child);
}
/* run through the array */
foreach ($items as $key=>$val) {
if (is_object($val)) {
$this->append_object($child, $val);
} else {
if ($element == "skipDays") $key = "day";
if ($element == "skipHours") $key = "hour";
$this->append_var($child, $key, $val);
}
}
if ($child != $parent) {
$parent->append_child($child);
}
}
/**
* Appends a new Text Node to the DomDocument.
*
* This function adds a variable with name and value to the specified parent
* node. A new element and a new text node will be created from the
* params $child and $text and will then appended to the parent object.
*
* @param DomNode $parent
* @param string $child
* @param string $text
* @access public
*/
function append_var(&$parent, $child, $text) {
if (strcasecmp("", $text)) {
$child = $this->dom->create_element($child);
$text = $this->dom->create_text_node(htmlentities($text));
$child->append_child($text);
$parent->append_child($child);
}
}
/**
* Appends a new category object to the parent node.
*
* This function makes sure that the parser transforms any category objects
* it runs into into well-formed category elements. This function is not
* available outside of the class. Use $parser::append_object() instead.
*
* @param DomNode $parent
* @param string $key
* @param category $object
* @see category
* @see append_object()
* @access private
*/
function make_category(&$parent, $key, $object) {
$child = $this->dom->create_element($key);
if (isset($object->domain)) {
$child->set_attribute("domain", $object->get_domain());
}
if (isset($object->title)) {
$text = $this->dom->create_text_node(htmlentitites($object->get_title()));
$child->append_child($text);
}
$parent->append_child($child);
}
/**
* Appends a new cloud object to the parent node.
*
* This function makes sure that the parser transforms any cloud objects
* it runs into into well-formed cloud elements. This function is not
* available outside of the class. Use $parser::append_object() instead.
*
* @param DomNode $parent
* @param string $key
* @param cloud $object
* @see cloud
* @see append_object()
* @access private
*/
function make_cloud(&$parent, $key, $object) {
$child = $this->dom->create_element($key);
$attributes = get_object_vars($object);
foreach ($attributes as $key=>$val) {
if (isset($object->$key)) {
$get_val = "get_$key";
/* in case $key references an url*/
if (is_a($object->$key, "url")) {
$url = $object->$get_val();
$child->set_attribute($key, $url->get_url());
} else {
$child->set_attribute($key, $object->$get_val());
}
}
}
$parent->append_child($child);
}
/**
* Appends a new email object to the parent node.
*
* This function makes sure that the parser transforms any email objects it
* runs into into well-formed email elements. This function is not available
* outside of the class. Use $parser::append_object() instead.
*
* @param DomNode $parent
* @param string $key
* @param email $object
* @see email
* @see append_object()
* @access private
*/
function make_email(&$parent, $key, $object) {
$child = $this->dom->create_element($key);
$text = "";
if (isset($object->address)) {
$text .= $object->get_address();
}
if (isset($object->recipient)) {
$text .= " (" . $object->get_recipient() . ")";
}
$text = $this->dom->create_text_node(htmlentities($text));
$child->append_child($text);
$parent->append_child($child);
}
/**
* Appends a new enclosure object to the parent node.
*
* This function makes sure that the parser transforms any enclosure objects
* it runs into into well-formed enclosure elements. This function is not
* available outside of the class. Use $parser::append_object() instead.
*
* @param DomNode $parent
* @param string $key
* @param enclosure $object
* @see enclosure
* @see append_object()
* @access private
*/
function make_enclosure(&$parent, $key, $object) {
$child = $this->dom->create_element($key);
$attributes = get_object_vars($object);
foreach ($attributes as $key=>$val) {
if (isset($object->$key)) {
$get_val = "get_".$key;
/* in case $key references an url*/
if (is_a($object->key, "url")) {
$url = $object->$get_val();
$child->set_attribute($key, $url->get_url());
} else {
$child->set_attribute($key, $object->$get_val());
}
}
}
$parent->append_child($child);
}
/**
* Appends a new guid object to the parent node.
*
* This function makes sure that the parser transforms any guid objects it
* runs into into well-formed guid elements. This function is not available
* outside of the class. Use $parser::append_object() instead.
*
* @param DomNode $parent
* @param string $key
* @param guid $object
* @see guid
* @see append_object()
* @access private
*/
function make_guid(&$parent, $key, $object) {
$child = $this->dom->create_element($key);
if (isset($object->isPermaLink)) {
$child->set_attribute("ispermalink", $object->get_isPermaLink());
}
if (isset($object->guid)) {
$child->create_text_node(htmlentities($object->get_guid()));
}
$parent->append_child($child);
}
/**
* Creates a new text node containing an email string
*
* This function makes sure that the parser transforms any url objects it
* runs into into a text node. Please note that the RSS Specs do not know
* something as an URL element. The function just creates a string formatted
* url::$address ($url::recipient) and inserts it for calling attribute
* text, e.g. item::$author or channel:$webMaster.
* This function is not available outside of the class. Use $parser::
* append_object() instead.
*
* @param DomNode $parent
* @param string $key
* @param guid $object
* @see url
* @see append_object()
* @access private
*/
function make_url(&$parent, $key, $object) {
$child = $this->dom->create_element($key);
$text = $this->dom->create_text_node(htmlentities($object->get_url()));
$child->append_child($text);
$parent->append_child($child);
}
}
?>