-
Notifications
You must be signed in to change notification settings - Fork 1k
/
XmlMiddleware.php
157 lines (151 loc) · 5.83 KB
/
XmlMiddleware.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Tqdev\PhpCrudApi\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Tqdev\PhpCrudApi\Column\ReflectionService;
use Tqdev\PhpCrudApi\Config\Config;
use Tqdev\PhpCrudApi\Controller\Responder;
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
use Tqdev\PhpCrudApi\Middleware\Router\Router;
use Tqdev\PhpCrudApi\ResponseFactory;
class XmlMiddleware extends Middleware
{
public function __construct(Router $router, Responder $responder, Config $config, string $middleware)
{
parent::__construct($router, $responder, $config, $middleware);
}
private function json2xml($json, $types = 'null,boolean,number,string,object,array')
{
$a = json_decode($json);
$d = new \DOMDocument();
$c = $d->createElement("root");
$d->appendChild($c);
$t = function ($v) {
$type = gettype($v);
switch ($type) {
case 'integer':
return 'number';
case 'double':
return 'number';
default:
return strtolower($type);
}
};
$ts = explode(',', $types);
$f = function ($f, $c, $a, $s = false) use ($t, $d, $ts) {
if (in_array($t($a), $ts)) {
$c->setAttribute('type', $t($a));
}
if ($t($a) != 'array' && $t($a) != 'object') {
if ($t($a) == 'boolean') {
$c->appendChild($d->createTextNode($a ? 'true' : 'false'));
} else {
$c->appendChild($d->createTextNode($a));
}
} else {
foreach ($a as $k => $v) {
$k = preg_replace('/[^a-z0-9\-\_\.]/', '', $k);
if ($k == '__type' && $t($a) == 'object') {
$c->setAttribute('__type', $v);
} else {
if ($t($v) == 'object') {
$ch = $c->appendChild($d->createElementNS(null, $s ? 'item' : $k));
$f($f, $ch, $v);
} else if ($t($v) == 'array') {
$ch = $c->appendChild($d->createElementNS(null, $s ? 'item' : $k));
$f($f, $ch, $v, true);
} else {
$va = $d->createElementNS(null, $s ? 'item' : $k);
if ($t($v) == 'boolean') {
$va->appendChild($d->createTextNode($v ? 'true' : 'false'));
} else {
$va->appendChild($d->createTextNode((string) $v));
}
$ch = $c->appendChild($va);
if (in_array($t($v), $ts)) {
$ch->setAttribute('type', $t($v));
}
}
}
}
}
};
$f($f, $c, $a, $t($a) == 'array');
return $d->saveXML($d->documentElement);
}
private function xml2json($xml): string
{
$o = @simplexml_load_string($xml);
if ($o === false) {
return '';
}
$a = @dom_import_simplexml($o);
if (!$a) {
return '';
}
$t = function ($v) {
$t = $v->getAttribute('type');
$txt = $v->firstChild->nodeType == XML_TEXT_NODE;
return $t ?: ($txt ? 'string' : 'object');
};
$f = function ($f, $a) use ($t) {
$c = null;
if ($t($a) == 'null') {
$c = null;
} else if ($t($a) == 'boolean') {
$b = substr(strtolower($a->textContent), 0, 1);
$c = in_array($b, array('1', 't'));
} else if ($t($a) == 'number') {
$c = $a->textContent + 0;
} else if ($t($a) == 'string') {
$c = $a->textContent;
} else if ($t($a) == 'object') {
$c = array();
if ($a->getAttribute('__type')) {
$c['__type'] = $a->getAttribute('__type');
}
for ($i = 0; $i < $a->childNodes->length; $i++) {
$v = $a->childNodes[$i];
$c[$v->nodeName] = $f($f, $v);
}
$c = (object) $c;
} else if ($t($a) == 'array') {
$c = array();
for ($i = 0; $i < $a->childNodes->length; $i++) {
$v = $a->childNodes[$i];
$c[$i] = $f($f, $v);
}
}
return $c;
};
$c = $f($f, $a);
return (string) json_encode($c);
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
{
parse_str($request->getUri()->getQuery(), $params);
$isXml = isset($params['format']) && $params['format'] == 'xml';
if ($isXml) {
$body = $request->getBody()->getContents();
if ($body) {
$json = $this->xml2json($body);
$request = $request->withParsedBody(json_decode($json));
}
}
$response = $next->handle($request);
if ($isXml) {
$body = $response->getBody()->getContents();
if ($body) {
$types = implode(',', $this->getArrayProperty('types', 'null,array'));
if ($types == '' || $types == 'all') {
$xml = $this->json2xml($body);
} else {
$xml = $this->json2xml($body, $types);
}
$response = ResponseFactory::fromXml(ResponseFactory::OK, $xml);
}
}
return $response;
}
}