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