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.

114 line
3.6KB

  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.
  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 boolean 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. * Formats the specified response.
  52. * @param Response $response the response to be formatted.
  53. */
  54. public function format($response)
  55. {
  56. $charset = $this->encoding === null ? $response->charset : $this->encoding;
  57. if (stripos($this->contentType, 'charset') === false) {
  58. $this->contentType .= '; charset=' . $charset;
  59. }
  60. $response->getHeaders()->set('Content-Type', $this->contentType);
  61. if ($response->data !== null) {
  62. $dom = new DOMDocument($this->version, $charset);
  63. $root = new DOMElement($this->rootTag);
  64. $dom->appendChild($root);
  65. $this->buildXml($root, $response->data);
  66. $response->content = $dom->saveXML();
  67. }
  68. }
  69. /**
  70. * @param DOMElement $element
  71. * @param mixed $data
  72. */
  73. protected function buildXml($element, $data)
  74. {
  75. if (is_array($data) ||
  76. ($data instanceof \Traversable && $this->useTraversableAsArray && !$data instanceof Arrayable)
  77. ) {
  78. foreach ($data as $name => $value) {
  79. if (is_int($name) && is_object($value)) {
  80. $this->buildXml($element, $value);
  81. } elseif (is_array($value) || is_object($value)) {
  82. $child = new DOMElement(is_int($name) ? $this->itemTag : $name);
  83. $element->appendChild($child);
  84. $this->buildXml($child, $value);
  85. } else {
  86. $child = new DOMElement(is_int($name) ? $this->itemTag : $name);
  87. $element->appendChild($child);
  88. $child->appendChild(new DOMText((string) $value));
  89. }
  90. }
  91. } elseif (is_object($data)) {
  92. $child = new DOMElement(StringHelper::basename(get_class($data)));
  93. $element->appendChild($child);
  94. if ($data instanceof Arrayable) {
  95. $this->buildXml($child, $data->toArray());
  96. } else {
  97. $array = [];
  98. foreach ($data as $name => $value) {
  99. $array[$name] = $value;
  100. }
  101. $this->buildXml($child, $array);
  102. }
  103. } else {
  104. $element->appendChild(new DOMText((string) $data));
  105. }
  106. }
  107. }