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.

106 lines
3.3KB

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