forked from yiisoft/yii2-httpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlFormatter.php
112 lines (104 loc) · 3.36 KB
/
XmlFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\httpclient;
use DOMDocument;
use DOMElement;
use DOMText;
use SimpleXMLElement;
use yii\base\Arrayable;
use yii\base\Object;
use Yii;
use yii\helpers\StringHelper;
/**
* XmlFormatter formats HTTP message as XML.
*
* @author Paul Klimov <[email protected]>
* @since 2.0
*/
class XmlFormatter extends Object implements FormatterInterface
{
/**
* @var string the Content-Type header for the response
*/
public $contentType = 'application/xml';
/**
* @var string the XML version
*/
public $version = '1.0';
/**
* @var string the XML encoding. If not set, it will use the value of [[\yii\base\Application::charset]].
*/
public $encoding;
/**
* @var string the name of the root element.
*/
public $rootTag = 'request';
/**
* @inheritdoc
*/
public function format(Request $request)
{
$contentType = $this->contentType;
$charset = $this->encoding === null ? Yii::$app->charset : $this->encoding;
if (stripos($contentType, 'charset') === false) {
$contentType .= '; charset=' . $charset;
}
$request->getHeaders()->set('Content-Type', $contentType);
$data = $request->getData();
if ($data !== null) {
if ($data instanceof DOMDocument) {
$content = $data->saveXML();
} elseif ($data instanceof SimpleXMLElement) {
$content = $data->saveXML();
} else {
$dom = new DOMDocument($this->version, $charset);
$root = new DOMElement($this->rootTag);
$dom->appendChild($root);
$this->buildXml($root, $data);
$content = $dom->saveXML();
}
$request->setContent($content);
}
return $request;
}
/**
* @param DOMElement $element
* @param mixed $data
*/
protected function buildXml($element, $data)
{
if (is_object($data)) {
$child = new DOMElement(StringHelper::basename(get_class($data)));
$element->appendChild($child);
if ($data instanceof Arrayable) {
$this->buildXml($child, $data->toArray());
} else {
$array = [];
foreach ($data as $name => $value) {
$array[$name] = $value;
}
$this->buildXml($child, $array);
}
} elseif (is_array($data)) {
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(is_int($name) ? $this->itemTag : $name);
$element->appendChild($child);
$this->buildXml($child, $value);
} else {
$child = new DOMElement(is_int($name) ? $this->itemTag : $name);
$element->appendChild($child);
$child->appendChild(new DOMText((string) $value));
}
}
} else {
$element->appendChild(new DOMText((string) $data));
}
}
}