XmlResponseFormatter.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use DOMDocument;
  9. use DOMElement;
  10. use DOMException;
  11. use DOMText;
  12. use yii\base\Arrayable;
  13. use yii\base\Component;
  14. use yii\helpers\StringHelper;
  15. /**
  16. * XmlResponseFormatter formats the given data into an XML response content.
  17. *
  18. * It is used by [[Response]] to format response data.
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @since 2.0
  22. */
  23. class XmlResponseFormatter extends Component implements ResponseFormatterInterface
  24. {
  25. /**
  26. * @var string the Content-Type header for the response
  27. */
  28. public $contentType = 'application/xml';
  29. /**
  30. * @var string the XML version
  31. */
  32. public $version = '1.0';
  33. /**
  34. * @var string the XML encoding. If not set, it will use the value of [[Response::charset]].
  35. */
  36. public $encoding;
  37. /**
  38. * @var string the name of the root element. If set to false, null or is empty then no root tag should be added.
  39. */
  40. public $rootTag = 'response';
  41. /**
  42. * @var string the name of the elements that represent the array elements with numeric keys.
  43. */
  44. public $itemTag = 'item';
  45. /**
  46. * @var bool whether to interpret objects implementing the [[\Traversable]] interface as arrays.
  47. * Defaults to `true`.
  48. * @since 2.0.7
  49. */
  50. public $useTraversableAsArray = true;
  51. /**
  52. * @var bool if object tags should be added
  53. * @since 2.0.11
  54. */
  55. public $useObjectTags = true;
  56. /**
  57. * Formats the specified response.
  58. * @param Response $response the response to be formatted.
  59. */
  60. public function format($response)
  61. {
  62. $charset = $this->encoding === null ? $response->charset : $this->encoding;
  63. if (stripos($this->contentType, 'charset') === false) {
  64. $this->contentType .= '; charset=' . $charset;
  65. }
  66. $response->getHeaders()->set('Content-Type', $this->contentType);
  67. if ($response->data !== null) {
  68. $dom = new DOMDocument($this->version, $charset);
  69. if (!empty($this->rootTag)) {
  70. $root = new DOMElement($this->rootTag);
  71. $dom->appendChild($root);
  72. $this->buildXml($root, $response->data);
  73. } else {
  74. $this->buildXml($dom, $response->data);
  75. }
  76. $response->content = $dom->saveXML();
  77. }
  78. }
  79. /**
  80. * @param DOMElement $element
  81. * @param mixed $data
  82. */
  83. protected function buildXml($element, $data)
  84. {
  85. if (is_array($data) ||
  86. ($data instanceof \Traversable && $this->useTraversableAsArray && !$data instanceof Arrayable)
  87. ) {
  88. foreach ($data as $name => $value) {
  89. if (is_int($name) && is_object($value)) {
  90. $this->buildXml($element, $value);
  91. } elseif (is_array($value) || is_object($value)) {
  92. $child = new DOMElement($this->getValidXmlElementName($name));
  93. $element->appendChild($child);
  94. $this->buildXml($child, $value);
  95. } else {
  96. $child = new DOMElement($this->getValidXmlElementName($name));
  97. $element->appendChild($child);
  98. $child->appendChild(new DOMText($this->formatScalarValue($value)));
  99. }
  100. }
  101. } elseif (is_object($data)) {
  102. if ($this->useObjectTags) {
  103. $child = new DOMElement(StringHelper::basename(get_class($data)));
  104. $element->appendChild($child);
  105. } else {
  106. $child = $element;
  107. }
  108. if ($data instanceof Arrayable) {
  109. $this->buildXml($child, $data->toArray());
  110. } else {
  111. $array = [];
  112. foreach ($data as $name => $value) {
  113. $array[$name] = $value;
  114. }
  115. $this->buildXml($child, $array);
  116. }
  117. } else {
  118. $element->appendChild(new DOMText($this->formatScalarValue($data)));
  119. }
  120. }
  121. /**
  122. * Formats scalar value to use in XML text node.
  123. *
  124. * @param int|string|bool|float $value a scalar value.
  125. * @return string string representation of the value.
  126. * @since 2.0.11
  127. */
  128. protected function formatScalarValue($value)
  129. {
  130. if ($value === true) {
  131. return 'true';
  132. }
  133. if ($value === false) {
  134. return 'false';
  135. }
  136. if (is_float($value)) {
  137. return StringHelper::floatToString($value);
  138. }
  139. return (string) $value;
  140. }
  141. /**
  142. * Returns element name ready to be used in DOMElement if
  143. * name is not empty, is not int and is valid.
  144. *
  145. * Falls back to [[itemTag]] otherwise.
  146. *
  147. * @param mixed $name
  148. * @return string
  149. * @since 2.0.12
  150. */
  151. protected function getValidXmlElementName($name)
  152. {
  153. if (empty($name) || is_int($name) || !$this->isValidXmlName($name)) {
  154. return $this->itemTag;
  155. }
  156. return $name;
  157. }
  158. /**
  159. * Checks if name is valid to be used in XML.
  160. *
  161. * @param mixed $name
  162. * @return bool
  163. * @see http://stackoverflow.com/questions/2519845/how-to-check-if-string-is-a-valid-xml-element-name/2519943#2519943
  164. * @since 2.0.12
  165. */
  166. protected function isValidXmlName($name)
  167. {
  168. try {
  169. new DOMElement($name);
  170. return true;
  171. } catch (DOMException $e) {
  172. return false;
  173. }
  174. }
  175. }