2006-08-22 08:05:44 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* XML Helper class file.
|
|
|
|
*
|
|
|
|
* Simplifies the output of XML documents.
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2006-08-22 08:05:44 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-02-02 10:39:45 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-08-22 08:05:44 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 1.2
|
2006-08-22 08:05:44 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2008-02-11 02:24:10 +00:00
|
|
|
App::import('Core', array('Xml', 'Set'));
|
2006-10-16 20:02:34 +00:00
|
|
|
|
2006-08-22 08:05:44 +00:00
|
|
|
/**
|
|
|
|
* XML Helper class for easy output of XML structures.
|
|
|
|
*
|
|
|
|
* XmlHelper encloses all methods needed while working with XML documents.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
|
|
*/
|
2006-10-27 21:40:34 +00:00
|
|
|
class XmlHelper extends AppHelper {
|
2006-08-22 08:05:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the current model
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $model = null;
|
|
|
|
/**
|
|
|
|
* Name of the current field
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $field = null;
|
|
|
|
/**
|
|
|
|
* Namespaces to be utilized by default when generating documents
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $__namespaces = array();
|
|
|
|
/**
|
|
|
|
* Default document encoding
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $encoding = 'UTF-8';
|
|
|
|
/**
|
|
|
|
* Returns an XML document header
|
|
|
|
*
|
|
|
|
* @param array $attrib Header tag attributes
|
|
|
|
* @return string XML header
|
|
|
|
*/
|
|
|
|
function header($attrib = array()) {
|
2007-12-25 09:08:36 +00:00
|
|
|
if (Configure::read('App.encoding') !== null) {
|
|
|
|
$this->encoding = Configure::read('App.encoding');
|
|
|
|
}
|
2007-12-08 06:08:03 +00:00
|
|
|
$attrib = array_merge(array('version' => '1.0', 'encoding' => $this->encoding), $attrib);
|
2006-08-22 08:05:44 +00:00
|
|
|
return $this->output('<' . '?xml' . $this->__composeAttributes($attrib) . ' ?' . '>');
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Adds a namespace to any documents generated
|
|
|
|
*
|
|
|
|
* @param string $name The namespace name
|
|
|
|
* @param string $url The namespace URI; can be empty if in the default namespace map
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean False if no URL is specified, and the namespace does not exist
|
2006-08-22 08:05:44 +00:00
|
|
|
* default namespace map, otherwise true
|
2008-02-01 04:53:09 +00:00
|
|
|
* @deprecated
|
|
|
|
* @see XML::addNs()
|
2006-08-22 08:05:44 +00:00
|
|
|
*/
|
|
|
|
function addNs($name, $url = null) {
|
2008-02-11 02:24:10 +00:00
|
|
|
return Xml::addNamespace($name, $url);
|
2006-08-22 08:05:44 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Removes a namespace added in addNs()
|
|
|
|
*
|
|
|
|
* @param string $name The namespace name or URI
|
2008-02-01 04:53:09 +00:00
|
|
|
* @deprecated
|
|
|
|
* @see XML::removeNs()
|
2006-08-22 08:05:44 +00:00
|
|
|
*/
|
|
|
|
function removeNs($name) {
|
2008-02-11 02:24:10 +00:00
|
|
|
Xml::removeGlobalNamespace($name);
|
2006-08-22 08:05:44 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Prepares the current set of namespaces for output in elem() / __composeAttributes()
|
|
|
|
*
|
|
|
|
* @return array The contents of $__namespaces, with all keys prefixed with 'xmlns:'
|
|
|
|
*/
|
|
|
|
function __prepareNamespaces() {
|
|
|
|
if (!empty($this->__namespaces)) {
|
|
|
|
$keys = array_keys($this->__namespaces);
|
|
|
|
$count = count($keys);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$keys[$i] = 'xmlns:' . $keys[$i];
|
|
|
|
}
|
|
|
|
return array_combine($keys, array_values($this->__namespaces));
|
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generates an XML element
|
|
|
|
*
|
|
|
|
* @param string $name The name of the XML element
|
|
|
|
* @param array $attrib The attributes of the XML element
|
|
|
|
* @param mixed $content XML element content
|
|
|
|
* @param boolean $endTag Whether the end tag of the element should be printed
|
|
|
|
* @return string XML
|
|
|
|
*/
|
|
|
|
function elem($name, $attrib = array(), $content = null, $endTag = true) {
|
|
|
|
|
|
|
|
$ns = null;
|
|
|
|
if (isset($attrib['namespace'])) {
|
|
|
|
$ns = $attrib['namespace'] . ':';
|
|
|
|
unset($attrib['namespace']);
|
|
|
|
}
|
|
|
|
$out = "<{$ns}{$name}" . $this->__composeAttributes($attrib);
|
|
|
|
|
2007-01-14 05:46:03 +00:00
|
|
|
if (empty($content) && $endTag) {
|
2006-08-22 08:05:44 +00:00
|
|
|
$out .= ' />';
|
|
|
|
} else {
|
|
|
|
$out .= '>' . $this->__composeContent($content);
|
|
|
|
if ($endTag) {
|
|
|
|
$out .= "</{$name}>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->output($out);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generates XML element attributes
|
|
|
|
*
|
|
|
|
* @param mixed $attributes
|
|
|
|
* @return string Formatted XML attributes for inclusion in an XML element
|
|
|
|
*/
|
|
|
|
function __composeAttributes($attributes = array()) {
|
|
|
|
$out = '';
|
|
|
|
if (is_array($attributes) && !empty($attributes)) {
|
|
|
|
$attr = array();
|
|
|
|
$keys = array_keys($attributes);
|
|
|
|
$count = count($keys);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$attr[] = $keys[$i] . '="' . h($attributes[$keys[$i]]) . '"';
|
|
|
|
}
|
|
|
|
$out .= ' ' . join(' ', $attr);
|
|
|
|
} elseif (is_string($attributes) && !empty($attributes)) {
|
|
|
|
$out .= ' ' . $attributes;
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generates XML content based on the type of variable or object passed
|
|
|
|
*
|
|
|
|
* @param mixed $content The content to be converted to XML
|
|
|
|
* @return string XML
|
|
|
|
*/
|
|
|
|
function __composeContent($content) {
|
|
|
|
if (is_string($content)) {
|
2007-01-29 22:04:26 +00:00
|
|
|
return $content;
|
2006-08-22 08:05:44 +00:00
|
|
|
} elseif (is_array($content)) {
|
2007-01-29 22:04:26 +00:00
|
|
|
$out = '';
|
2006-08-22 08:05:44 +00:00
|
|
|
$keys = array_keys($content);
|
|
|
|
$count = count($keys);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
if (is_numeric($keys[$i])) {
|
|
|
|
$out .= $this->__composeContent($content[$keys[$i]]);
|
|
|
|
} elseif (is_array($content[$keys[$i]])) {
|
|
|
|
$attr = $child = array();
|
2006-11-11 21:05:00 +00:00
|
|
|
if (Set::countDim($content[$keys[$i]]) >= 2) {
|
2007-09-24 23:49:54 +00:00
|
|
|
|
2006-08-22 08:05:44 +00:00
|
|
|
} else {
|
2007-09-24 23:49:54 +00:00
|
|
|
|
2006-08-22 08:05:44 +00:00
|
|
|
}
|
|
|
|
//$out .= $this->elem($keys[$i]
|
|
|
|
}
|
|
|
|
}
|
2007-01-29 22:04:26 +00:00
|
|
|
return $out;
|
2008-02-01 04:53:09 +00:00
|
|
|
} elseif (is_object($content) && (is_a($content, 'XmlNode') || is_a($content, 'xmlnode'))) {
|
2007-01-29 22:04:26 +00:00
|
|
|
return $content->toString();
|
|
|
|
} elseif (is_object($content) && method_exists($content, 'toString')) {
|
|
|
|
return $content->toString();
|
2006-08-22 08:05:44 +00:00
|
|
|
} elseif (is_object($content) && method_exists($content, 'toString')) {
|
2007-01-29 22:04:26 +00:00
|
|
|
return $content->toString();
|
|
|
|
} else {
|
|
|
|
return $content;
|
2006-08-22 08:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-16 20:02:34 +00:00
|
|
|
/**
|
|
|
|
* Serializes a model resultset into XML
|
|
|
|
*
|
|
|
|
* @param mixed $data The content to be converted to XML
|
|
|
|
* @param array $options The data formatting options
|
|
|
|
* @return string A copy of $data in XML format
|
|
|
|
*/
|
|
|
|
function serialize($data, $options = array()) {
|
2008-02-01 05:00:44 +00:00
|
|
|
$data = new Xml($data, array_merge(array('attributes' => false, 'format' => 'attributes'), $options));
|
2008-02-11 02:24:10 +00:00
|
|
|
return $data->toString(array_merge(array('header' => false), $options));
|
2006-10-16 20:02:34 +00:00
|
|
|
}
|
2006-08-22 08:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|