1: <?php
2: /* SVN FILE: $Id$ */
3: /**
4: * XML Helper class file.
5: *
6: * Simplifies the output of XML documents.
7: *
8: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
9: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10: *
11: * Licensed under The MIT License
12: * Redistributions of files must retain the above copyright notice.
13: *
14: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
15: * @link http://cakephp.org CakePHP(tm) Project
16: * @package cake
17: * @subpackage cake.cake.libs.view.helpers
18: * @since CakePHP(tm) v 1.2
19: * @version $Revision$
20: * @modifiedby $LastChangedBy$
21: * @lastmodified $Date$
22: * @license http://www.opensource.org/licenses/mit-license.php The MIT License
23: */
24: App::import('Core', array('Xml', 'Set'));
25:
26: /**
27: * XML Helper class for easy output of XML structures.
28: *
29: * XmlHelper encloses all methods needed while working with XML documents.
30: *
31: * @package cake
32: * @subpackage cake.cake.libs.view.helpers
33: */
34: class XmlHelper extends AppHelper {
35: /**
36: * Default document encoding
37: *
38: * @access public
39: * @var string
40: */
41: var $encoding = 'UTF-8';
42:
43: var $Xml;
44: var $XmlElement;
45: /**
46: * Constructor
47: * @return void
48: */
49: function __construct() {
50: parent::__construct();
51: $this->Xml =& new Xml();
52: $this->Xml->options(array('verifyNs' => false));
53: }
54: /**
55: * Returns an XML document header
56: *
57: * @param array $attrib Header tag attributes
58: * @return string XML header
59: */
60: function header($attrib = array()) {
61: if (Configure::read('App.encoding') !== null) {
62: $this->encoding = Configure::read('App.encoding');
63: }
64:
65: if (is_array($attrib)) {
66: $attrib = array_merge(array('encoding' => $this->encoding), $attrib);
67: }
68: if (is_string($attrib) && strpos($attrib, 'xml') !== 0) {
69: $attrib = 'xml ' . $attrib;
70: }
71:
72: return $this->output($this->Xml->header($attrib));
73: }
74: /**
75: * Adds a namespace to any documents generated
76: *
77: * @param string $name The namespace name
78: * @param string $url The namespace URI; can be empty if in the default namespace map
79: * @return boolean False if no URL is specified, and the namespace does not exist
80: * default namespace map, otherwise true
81: * @deprecated
82: * @see Xml::addNs()
83: */
84: function addNs($name, $url = null) {
85: return $this->Xml->addNamespace($name, $url);
86: }
87: /**
88: * Removes a namespace added in addNs()
89: *
90: * @param string $name The namespace name or URI
91: * @deprecated
92: * @see Xml::removeNs()
93: */
94: function removeNs($name) {
95: return $this->Xml->removeGlobalNamespace($name);
96: }
97: /**
98: * Generates an XML element
99: *
100: * @param string $name The name of the XML element
101: * @param array $attrib The attributes of the XML element
102: * @param mixed $content XML element content
103: * @param boolean $endTag Whether the end tag of the element should be printed
104: * @return string XML
105: */
106: function elem($name, $attrib = array(), $content = null, $endTag = true) {
107: $namespace = null;
108: if (isset($attrib['namespace'])) {
109: $namespace = $attrib['namespace'];
110: unset($attrib['namespace']);
111: }
112: $cdata = false;
113: if (is_array($content) && isset($content['cdata'])) {
114: $cdata = true;
115: unset($content['cdata']);
116: }
117: if (is_array($content) && array_key_exists('value', $content)) {
118: $content = $content['value'];
119: }
120: $children = array();
121: if (is_array($content)) {
122: $children = $content;
123: $content = null;
124: }
125:
126: $elem =& $this->Xml->createElement($name, $content, $attrib, $namespace);
127: foreach ($children as $child) {
128: $elem->createElement($child);
129: }
130: $out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));
131:
132: if (!$endTag) {
133: $this->XmlElement =& $elem;
134: }
135: return $this->output($out);
136: }
137: /**
138: * Create closing tag for current element
139: *
140: * @return string
141: */
142: function closeElem() {
143: $elem = (empty($this->XmlElement)) ? $this->Xml : $this->XmlElement;
144: $name = $elem->name();
145: if ($parent =& $elem->parent()) {
146: $this->XmlElement =& $parent;
147: }
148: return $this->output('</' . $name . '>');
149: }
150: /**
151: * Serializes a model resultset into XML
152: *
153: * @param mixed $data The content to be converted to XML
154: * @param array $options The data formatting options. For a list of valid options, see
155: * XmlNode::__construct().
156: * @return string A copy of $data in XML format
157: * @see XmlNode
158: */
159: function serialize($data, $options = array()) {
160: $options += array('attributes' => false, 'format' => 'attributes');
161: $data =& new Xml($data, $options);
162: return $data->toString($options + array('header' => false));
163: }
164: }
165:
166: ?>
167: