Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

146 lines
4.5 KiB

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
use DOMDocument;
use DOMElement;
use DOMText;
use yii\base\Arrayable;
use yii\base\Component;
use yii\helpers\StringHelper;
/**
* XmlResponseFormatter formats the given data into an XML response content.
*
* It is used by [[Response]] to format response data.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class XmlResponseFormatter extends Component implements ResponseFormatterInterface
{
/**
* @var string the Content-Type header for the response
*/
public $contentType = 'application/xml';
/**
* @var string the XML version
*/
public $version = '1.0';
/**
* @var string the XML encoding. If not set, it will use the value of [[Response::charset]].
*/
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.
*/
public $rootTag = 'response';
/**
* @var string the name of the elements that represent the array elements with numeric keys.
*/
public $itemTag = 'item';
/**
* @var bool whether to interpret objects implementing the [[\Traversable]] interface as arrays.
* Defaults to `true`.
* @since 2.0.7
*/
public $useTraversableAsArray = true;
/**
* @var bool if object tags should be added
* @since 2.0.11
*/
public $useObjectTags = true;
/**
* Formats the specified response.
* @param Response $response the response to be formatted.
*/
public function format($response)
{
$charset = $this->encoding === null ? $response->charset : $this->encoding;
if (stripos($this->contentType, 'charset') === false) {
$this->contentType .= '; charset=' . $charset;
}
$response->getHeaders()->set('Content-Type', $this->contentType);
if ($response->data !== null) {
$dom = new DOMDocument($this->version, $charset);
if (!empty($this->rootTag)) {
$root = new DOMElement($this->rootTag);
$dom->appendChild($root);
$this->buildXml($root, $response->data);
} else {
$this->buildXml($dom, $response->data);
}
$response->content = $dom->saveXML();
}
}
/**
* @param DOMElement $element
* @param mixed $data
*/
protected function buildXml($element, $data)
{
if (is_array($data) ||
($data instanceof \Traversable && $this->useTraversableAsArray && !$data instanceof Arrayable)
) {
foreach ($data as $name => $value) {
if (is_int($name) && is_object($value)) {
$this->buildXml($element, $value);
} elseif (is_array($value) || is_object($value)) {
$child = new DOMElement(is_int($name) ? $this->itemTag : $name);
$element->appendChild($child);
$this->buildXml($child, $value);
} else {
$child = new DOMElement(is_int($name) ? $this->itemTag : $name);
$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)));
$element->appendChild($child);
} else {
$child = $element;
}
if ($data instanceof Arrayable) {
$this->buildXml($child, $data->toArray());
} else {
$array = [];
foreach ($data as $name => $value) {
$array[$name] = $value;
}
$this->buildXml($child, $array);
}
} else {
$element->appendChild(new DOMText($this->formatScalarValue($data)));
}
}
/**
* Formats scalar value to use in XML text node
*
* @param int|string|bool $value
* @return string
* @since 2.0.11
*/
protected function formatScalarValue($value)
{
if ($value === true) {
return 'true';
}
if ($value === false) {
return 'false';
}
return (string) $value;
}
}