XmlResponseFormatter.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 DOMText;
  11. use yii\base\Arrayable;
  12. use yii\base\Component;
  13. use yii\helpers\StringHelper;
  14. /**
  15. * XmlResponseFormatter formats the given data into an XML response content.
  16. *
  17. * It is used by [[Response]] to format response data.
  18. *
  19. * @author Qiang Xue <qiang.xue@gmail.com>
  20. * @since 2.0
  21. */
  22. class XmlResponseFormatter extends Component implements ResponseFormatterInterface
  23. {
  24. /**
  25. * @var string the Content-Type header for the response
  26. */
  27. public $contentType = 'application/xml';
  28. /**
  29. * @var string the XML version
  30. */
  31. public $version = '1.0';
  32. /**
  33. * @var string the XML encoding. If not set, it will use the value of [[Response::charset]].
  34. */
  35. public $encoding;
  36. /**
  37. * @var string the name of the root element. If set to false, null or is empty then no root tag should be added.
  38. */
  39. public $rootTag = 'response';
  40. /**
  41. * @var string the name of the elements that represent the array elements with numeric keys.
  42. */
  43. public $itemTag = 'item';
  44. /**
  45. * @var bool whether to interpret objects implementing the [[\Traversable]] interface as arrays.
  46. * Defaults to `true`.
  47. * @since 2.0.7
  48. */
  49. public $useTraversableAsArray = true;
  50. /**
  51. * @var bool if object tags should be added
  52. * @since 2.0.11
  53. */
  54. public $useObjectTags = true;
  55. /**
  56. * Formats the specified response.
  57. * @param Response $response the response to be formatted.
  58. */
  59. public function format($response)
  60. {
  61. $charset = $this->encoding === null ? $response->charset : $this->encoding;
  62. if (stripos($this->contentType, 'charset') === false) {
  63. $this->contentType .= '; charset=' . $charset;
  64. }
  65. $response->getHeaders()->set('Content-Type', $this->contentType);
  66. if ($response->data !== null) {
  67. $dom = new DOMDocument($this->version, $charset);
  68. if (!empty($this->rootTag)) {
  69. $root = new DOMElement($this->rootTag);
  70. $dom->appendChild($root);
  71. $this->buildXml($root, $response->data);
  72. } else {
  73. $this->buildXml($dom, $response->data);
  74. }
  75. $response->content = $dom->saveXML();
  76. }
  77. }
  78. /**
  79. * @param DOMElement $element
  80. * @param mixed $data
  81. */
  82. protected function buildXml($element, $data)
  83. {
  84. if (is_array($data) ||
  85. ($data instanceof \Traversable && $this->useTraversableAsArray && !$data instanceof Arrayable)
  86. ) {
  87. foreach ($data as $name => $value) {
  88. if (is_int($name) && is_object($value)) {
  89. $this->buildXml($element, $value);
  90. } elseif (is_array($value) || is_object($value)) {
  91. $child = new DOMElement(is_int($name) ? $this->itemTag : $name);
  92. $element->appendChild($child);
  93. $this->buildXml($child, $value);
  94. } else {
  95. $child = new DOMElement(is_int($name) ? $this->itemTag : $name);
  96. $element->appendChild($child);
  97. $child->appendChild(new DOMText($this->formatScalarValue($value)));
  98. }
  99. }
  100. } elseif (is_object($data)) {
  101. if ($this->useObjectTags) {
  102. $child = new DOMElement(StringHelper::basename(get_class($data)));
  103. $element->appendChild($child);
  104. } else {
  105. $child = $element;
  106. }
  107. if ($data instanceof Arrayable) {
  108. $this->buildXml($child, $data->toArray());
  109. } else {
  110. $array = [];
  111. foreach ($data as $name => $value) {
  112. $array[$name] = $value;
  113. }
  114. $this->buildXml($child, $array);
  115. }
  116. } else {
  117. $element->appendChild(new DOMText($this->formatScalarValue($data)));
  118. }
  119. }
  120. /**
  121. * Formats scalar value to use in XML text node
  122. *
  123. * @param int|string|bool $value
  124. * @return string
  125. * @since 2.0.11
  126. */
  127. protected function formatScalarValue($value)
  128. {
  129. if ($value === true) {
  130. return 'true';
  131. }
  132. if ($value === false) {
  133. return 'false';
  134. }
  135. return (string) $value;
  136. }
  137. }