123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace yii\web;
- use DOMDocument;
- use DOMElement;
- use DOMException;
- use DOMText;
- use yii\base\Arrayable;
- use yii\base\Component;
- use yii\helpers\StringHelper;
- class XmlResponseFormatter extends Component implements ResponseFormatterInterface
- {
-
- public $contentType = 'application/xml';
-
- public $version = '1.0';
-
- public $encoding;
-
- public $rootTag = 'response';
-
- public $itemTag = 'item';
-
- public $useTraversableAsArray = true;
-
- public $useObjectTags = true;
-
- 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();
- }
- }
-
- 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($this->getValidXmlElementName($name));
- $element->appendChild($child);
- $this->buildXml($child, $value);
- } else {
- $child = new DOMElement($this->getValidXmlElementName($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)));
- }
- }
-
- protected function formatScalarValue($value)
- {
- if ($value === true) {
- return 'true';
- }
- if ($value === false) {
- return 'false';
- }
- if (is_float($value)) {
- return StringHelper::floatToString($value);
- }
- return (string) $value;
- }
-
- protected function getValidXmlElementName($name)
- {
- if (empty($name) || is_int($name) || !$this->isValidXmlName($name)) {
- return $this->itemTag;
- }
- return $name;
- }
-
- protected function isValidXmlName($name)
- {
- try {
- new DOMElement($name);
- return true;
- } catch (DOMException $e) {
- return false;
- }
- }
- }
|