@ -10,7 +10,6 @@ namespace yii\web;
use DOMDocument;
use DOMElement;
use DOMException;
use DOMText;
use yii\base\Arrayable;
use yii\base\Component;
use yii\helpers\StringHelper;
@ -38,7 +37,10 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
*/
public $encoding;
/**
* @var string the name of the root element. If set to false, null or is empty then no root tag should be added.
* @var string|string[]|false the name of the root element. If set to false, null or is empty then no root tag
* should be added.
*
* Since 2.0.43 URI namespace could be specified by passing `[namespace, tag name]` array.
*/
public $rootTag = 'response';
/**
@ -52,14 +54,26 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
*/
public $useTraversableAsArray = true;
/**
* @var bool if object tags should be added
* @var bool if object class names should be used as tag names
* @since 2.0.11
*/
public $useObjectTags = true;
/**
* @var bool if true, converts object tags to lowercase, `$useObjectTags` must be enabled
* @since 2.0.43
*/
public $objectTagToLowercase = false;
/**
* @var DOMDocument the XML document, serves as the root of the document tree
* @since 2.0.43
*/
protected $dom;
/**
* Formats the specified response.
*
* @param Response $response the response to be formatted.
*/
public function format($response)
@ -70,21 +84,27 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
}
$response->getHeaders()->set('Content-Type', $this->contentType);
if ($response->data !== null) {
$dom = new DOMDocument($this->version, $charset);
$this-> dom = new DOMDocument($this->version, $charset);
if (!empty($this->rootTag)) {
$root = new DOMElement($this->rootTag);
$dom->appendChild($root);
if (is_array($this->rootTag)) {
$root = $this->dom->createElementNS($this->rootTag[0], $this->rootTag[1]);
} else {
$root = $this->dom->createElement($this->rootTag);
}
$this->dom->appendChild($root);
$this->buildXml($root, $response->data);
} else {
$this->buildXml($dom, $response->data);
$this->buildXml($this-> dom, $response->data);
}
$response->content = $dom->saveXML();
$response->content = $this-> dom->saveXML();
}
}
/**
* @param DOMElement $element
* @param mixed $data
* Recursively adds data to XML document.
*
* @param DOMElement|DOMDocument $element current element
* @param mixed $data content of the current element
*/
protected function buildXml($element, $data)
{
@ -95,18 +115,22 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
if (is_int($name) & & is_object($value)) {
$this->buildXml($element, $value);
} elseif (is_array($value) || is_object($value)) {
$child = new DOM Element($this->getValidXmlElementName($name));
$child = $this->dom->create Element($this->getValidXmlElementName($name));
$element->appendChild($child);
$this->buildXml($child, $value);
} else {
$child = new DOMElement($this->getValidXmlElementName($name));
$child = $this->dom->createElement($this->getValidXmlElementName($name));
$child->appendChild($this->dom->createTextNode($this->formatScalarValue($value)));
$element->appendChild($child);
$child->appendChild(new DOMText($this->formatScalarValue($value)));
}
}
} elseif (is_object($data)) {
if ($this->useObjectTags) {
$child = new DOMElement(StringHelper::basename(get_class($data)));
$name = StringHelper::basename(get_class($data));
if ($this->objectTagToLowercase) {
$name = strtolower($name);
}
$child = $this->dom->createElement($name);
$element->appendChild($child);
} else {
$child = $element;
@ -121,7 +145,7 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
$this->buildXml($child, $array);
}
} else {
$element->appendChild(new DOMText ($this->formatScalarValue($data)));
$element->appendChild($this->dom->createTextNode ($this->formatScalarValue($data)));
}
}
@ -152,7 +176,7 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
*
* Falls back to [[itemTag]] otherwise.
*
* @param mixed $name
* @param mixed $name the original name
* @return string
* @since 2.0.12
*/
@ -168,7 +192,7 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
/**
* Checks if name is valid to be used in XML.
*
* @param mixed $name
* @param mixed $name the name to test
* @return bool
* @see http://stackoverflow.com/questions/2519845/how-to-check-if-string-is-a-valid-xml-element-name/2519943#2519943
* @since 2.0.12
@ -176,8 +200,7 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
protected function isValidXmlName($name)
{
try {
new DOMElement($name);
return true;
return $this->dom->createElement($name) !== false;
} catch (DOMException $e) {
return false;
}