mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
updated XmlHelper to take advantage of Xml class to support namespaces
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7110 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
f1921c15ef
commit
546d804b10
1 changed files with 39 additions and 108 deletions
|
@ -36,27 +36,6 @@ App::import('Core', array('Xml', 'Set'));
|
|||
*/
|
||||
class XmlHelper extends AppHelper {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
@ -76,10 +55,13 @@ class XmlHelper extends AppHelper {
|
|||
}
|
||||
|
||||
if (is_array($attrib)) {
|
||||
$attrib = array_merge(array('version' => '1.0', 'encoding' => $this->encoding), $attrib);
|
||||
$attrib = array_merge(array('encoding' => $this->encoding), $attrib);
|
||||
}
|
||||
if (is_string($attrib) && strpos($attrib, 'xml') !== 0) {
|
||||
$attrib = 'xml ' . $attrib;
|
||||
}
|
||||
|
||||
return $this->output('<' . '?xml' . $this->__composeAttributes($attrib) . ' ?' . '>');
|
||||
return $this->output($this->Xml->header($attrib));
|
||||
}
|
||||
/**
|
||||
* Adds a namespace to any documents generated
|
||||
|
@ -92,7 +74,7 @@ class XmlHelper extends AppHelper {
|
|||
* @see Xml::addNs()
|
||||
*/
|
||||
function addNs($name, $url = null) {
|
||||
return Xml::addNamespace($name, $url);
|
||||
return $this->Xml->addNamespace($name, $url);
|
||||
}
|
||||
/**
|
||||
* Removes a namespace added in addNs()
|
||||
|
@ -102,23 +84,7 @@ class XmlHelper extends AppHelper {
|
|||
* @see Xml::removeNs()
|
||||
*/
|
||||
function removeNs($name) {
|
||||
Xml::removeGlobalNamespace($name);
|
||||
}
|
||||
/**
|
||||
* 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();
|
||||
return $this->Xml->removeGlobalNamespace($name);
|
||||
}
|
||||
/**
|
||||
* Generates an XML element
|
||||
|
@ -130,81 +96,41 @@ class XmlHelper extends AppHelper {
|
|||
* @return string XML
|
||||
*/
|
||||
function elem($name, $attrib = array(), $content = null, $endTag = true) {
|
||||
$ns = null;
|
||||
|
||||
$namespace = null;
|
||||
if (isset($attrib['namespace'])) {
|
||||
$ns = $attrib['namespace'] . ':';
|
||||
$namespace = $attrib['namespace'];
|
||||
unset($attrib['namespace']);
|
||||
}
|
||||
$out = "<{$ns}{$name}" . $this->__composeAttributes($attrib);
|
||||
$cdata = false;
|
||||
if (is_array($content) && isset($content['cdata'])) {
|
||||
$cdata = true;
|
||||
unset($content['cdata']);
|
||||
}
|
||||
if (is_array($content) && isset($content['value'])) {
|
||||
$content = $content['value'];
|
||||
}
|
||||
$children = array();
|
||||
if (is_array($content)) {
|
||||
$children = $content;
|
||||
$content = null;
|
||||
}
|
||||
$elem =& $this->Xml->createElement($name, $content, $attrib, $namespace);
|
||||
foreach ($children as $child) {
|
||||
$elem->createElement($child);
|
||||
}
|
||||
$out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));
|
||||
|
||||
if ((empty($content) && $content !== 0) && $endTag) {
|
||||
$out .= ' />';
|
||||
} else {
|
||||
$out .= '>' . $this->__composeContent($content);
|
||||
if ($endTag) {
|
||||
$out .= "</{$name}>";
|
||||
}
|
||||
if (!$endTag) {
|
||||
$this->Xml =& $elem;
|
||||
}
|
||||
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)) {
|
||||
return $content;
|
||||
} elseif (is_array($content)) {
|
||||
$out = '';
|
||||
$keys = array_keys($content);
|
||||
$count = count($keys);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
if (is_numeric($content[$keys[$i]])) {
|
||||
$out .= $this->__composeContent($content[$keys[$i]]);
|
||||
} elseif (is_array($content[$keys[$i]])) {
|
||||
$attr = $child = array();
|
||||
if (Set::countDim($content[$keys[$i]]) >= 2) {
|
||||
trigger_error(__('Dimension for XmlHelper::__composeContent is too high (>= 2). Please use an array with less dimension.', true), E_USER_WARNING);
|
||||
} else {
|
||||
$out .= $this->__composeContent($content[$keys[$i]]);
|
||||
}
|
||||
} elseif (is_string($content[$keys[$i]])) {
|
||||
$out .= $this->elem($content[$keys[$i]]);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
} elseif (is_object($content) && (is_a($content, 'XmlNode') || is_a($content, 'xmlnode'))) {
|
||||
return $content->toString();
|
||||
} elseif (is_object($content) && method_exists($content, 'toString')) {
|
||||
return $content->toString();
|
||||
} else {
|
||||
return $content;
|
||||
function closeElem() {
|
||||
$name = $this->Xml->name();
|
||||
if ($parent =& $this->Xml->parent()) {
|
||||
$this->Xml =& $parent;
|
||||
}
|
||||
return $this->output('</' . $name . '>');
|
||||
}
|
||||
/**
|
||||
* Serializes a model resultset into XML
|
||||
|
@ -217,6 +143,11 @@ class XmlHelper extends AppHelper {
|
|||
$data = new Xml($data, array_merge(array('attributes' => false, 'format' => 'attributes'), $options));
|
||||
return $data->toString(array_merge(array('header' => false), $options));
|
||||
}
|
||||
|
||||
function beforeRender() {
|
||||
$this->Xml =& new Xml();
|
||||
$this->Xml->options(array('verifyNs' => false));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in a new issue