2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* XML Helper class file.
|
|
|
|
*
|
|
|
|
* Simplifies the output of XML documents.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
|
|
* @since CakePHP(tm) v 1.2
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
App::import('Core', array('Xml', 'Set'));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XML Helper class for easy output of XML structures.
|
|
|
|
*
|
|
|
|
* XmlHelper encloses all methods needed while working with XML documents.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1473/XML
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class XmlHelper extends AppHelper {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Default document encoding
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $encoding = 'UTF-8';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-21 15:23:58 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
2010-01-25 22:59:05 +00:00
|
|
|
*
|
2008-06-21 15:23:58 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
$this->Xml =& new Xml();
|
|
|
|
$this->Xml->options(array('verifyNs' => false));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an XML document header
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param array $attrib Header tag attributes
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string XML header
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1476/header
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function header($attrib = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (Configure::read('App.encoding') !== null) {
|
|
|
|
$this->encoding = Configure::read('App.encoding');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($attrib)) {
|
2008-06-03 22:35:44 +00:00
|
|
|
$attrib = array_merge(array('encoding' => $this->encoding), $attrib);
|
|
|
|
}
|
|
|
|
if (is_string($attrib) && strpos($attrib, 'xml') !== 0) {
|
|
|
|
$attrib = 'xml ' . $attrib;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-21 21:43:54 +00:00
|
|
|
return $this->Xml->header($attrib);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Adds a namespace to any documents generated
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $name The namespace name
|
|
|
|
* @param string $url The namespace URI; can be empty if in the default namespace map
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean False if no URL is specified, and the namespace does not exist
|
2010-01-25 22:59:05 +00:00
|
|
|
* default namespace map, otherwise true
|
2008-05-30 11:40:08 +00:00
|
|
|
* @deprecated
|
|
|
|
* @see Xml::addNs()
|
|
|
|
*/
|
|
|
|
function addNs($name, $url = null) {
|
2008-06-03 22:35:44 +00:00
|
|
|
return $this->Xml->addNamespace($name, $url);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Removes a namespace added in addNs()
|
|
|
|
*
|
|
|
|
* @param string $name The namespace name or URI
|
|
|
|
* @deprecated
|
|
|
|
* @see Xml::removeNs()
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function removeNs($name) {
|
2008-06-03 22:35:44 +00:00
|
|
|
return $this->Xml->removeGlobalNamespace($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates an XML element
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @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
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string XML
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1475/elem
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function elem($name, $attrib = array(), $content = null, $endTag = true) {
|
2008-06-03 22:35:44 +00:00
|
|
|
$namespace = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($attrib['namespace'])) {
|
2008-06-03 22:35:44 +00:00
|
|
|
$namespace = $attrib['namespace'];
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($attrib['namespace']);
|
|
|
|
}
|
2008-06-03 22:35:44 +00:00
|
|
|
$cdata = false;
|
|
|
|
if (is_array($content) && isset($content['cdata'])) {
|
|
|
|
$cdata = true;
|
|
|
|
unset($content['cdata']);
|
|
|
|
}
|
2009-09-25 02:43:31 +00:00
|
|
|
if (is_array($content) && array_key_exists('value', $content)) {
|
2008-06-03 22:35:44 +00:00
|
|
|
$content = $content['value'];
|
|
|
|
}
|
|
|
|
$children = array();
|
|
|
|
if (is_array($content)) {
|
|
|
|
$children = $content;
|
|
|
|
$content = null;
|
|
|
|
}
|
2008-06-21 15:23:58 +00:00
|
|
|
|
2008-06-03 22:35:44 +00:00
|
|
|
$elem =& $this->Xml->createElement($name, $content, $attrib, $namespace);
|
|
|
|
foreach ($children as $child) {
|
|
|
|
$elem->createElement($child);
|
|
|
|
}
|
|
|
|
$out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-06-03 22:35:44 +00:00
|
|
|
if (!$endTag) {
|
|
|
|
$this->Xml =& $elem;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-21 21:43:54 +00:00
|
|
|
return $out;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-21 15:23:58 +00:00
|
|
|
/**
|
|
|
|
* Create closing tag for current element
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function closeElem() {
|
2008-06-03 22:35:44 +00:00
|
|
|
$name = $this->Xml->name();
|
|
|
|
if ($parent =& $this->Xml->parent()) {
|
|
|
|
$this->Xml =& $parent;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-21 21:43:54 +00:00
|
|
|
return '</' . $name . '>';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Serializes a model resultset into XML
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param mixed $data The content to be converted to XML
|
|
|
|
* @param array $options The data formatting options. For a list of valid options, see
|
2010-02-09 22:58:15 +00:00
|
|
|
* Xml::__construct().
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string A copy of $data in XML format
|
2010-02-09 22:58:15 +00:00
|
|
|
* @see Xml::__construct()
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1474/serialize
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function serialize($data, $options = array()) {
|
2009-03-11 04:09:35 +00:00
|
|
|
$options += array('attributes' => false, 'format' => 'attributes');
|
|
|
|
$data =& new Xml($data, $options);
|
|
|
|
return $data->toString($options + array('header' => false));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|