1
|
|
|
<?php |
2
|
|
|
namespace Flipside\Http\Rest; |
3
|
|
|
|
4
|
|
|
use \Psr\Http\Message\ServerRequestInterface as Request; |
5
|
|
|
use \Psr\Http\Message\ResponseInterface as Response; |
6
|
|
|
|
7
|
|
|
require 'vendor/autoload.php'; |
8
|
|
|
|
9
|
|
|
class SerializationMiddleware |
10
|
|
|
{ |
11
|
|
|
protected $format = null; |
12
|
|
|
|
13
|
|
|
private function getFormatFromHeader($request) |
14
|
|
|
{ |
15
|
|
|
$mimeType = $request->getHeaderLine('Accept'); |
16
|
|
|
switch($mimeType) |
17
|
|
|
{ |
18
|
|
|
case 'text/csv': |
19
|
|
|
return 'csv'; |
20
|
|
|
case 'text/x-vCard': |
21
|
|
|
return 'vcard'; |
22
|
|
|
default: |
23
|
|
|
return 'json'; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function getParamFromArrayIfSet($array, $param, $default = null) |
28
|
|
|
{ |
29
|
|
|
if(isset($array[$param])) |
30
|
|
|
{ |
31
|
|
|
return $array[$param]; |
32
|
|
|
} |
33
|
|
|
return $default; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function getFormat($request, $response) |
37
|
|
|
{ |
38
|
|
|
$params = $request->getQueryParams(); |
39
|
|
|
$this->format = $this->getParamFromArrayIfSet($params, 'fmt'); |
40
|
|
|
if($this->format === null) |
41
|
|
|
{ |
42
|
|
|
$this->format = $this->getParamFromArrayIfSet($params, '$format'); |
43
|
|
|
if($this->format === null) |
44
|
|
|
{ |
45
|
|
|
$this->format = $this->getFormatFromHeader($request); |
46
|
|
|
} |
47
|
|
|
else if($this->format === 'json') |
48
|
|
|
{ |
49
|
|
|
//OData json is different from notmal json |
50
|
|
|
$this->format = 'odata-json'; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
if(strstr($this->format, 'odata.streaming=true')) |
54
|
|
|
{ |
55
|
|
|
return $response->withStatus(406); |
56
|
|
|
} |
57
|
|
|
return $response; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function reserializeBody($response, $serializer) |
61
|
|
|
{ |
62
|
|
|
$body = $response->getBody(); |
63
|
|
|
$body->rewind(); |
64
|
|
|
$data = json_decode($body->getContents()); |
65
|
|
|
$serializer = new $serializer(); |
66
|
|
|
$res = $serializer->serializeData($this->format, $data); |
67
|
|
|
$response = $response->withBody(new \Slim\Http\Body(fopen('php://temp', 'r+'))); |
68
|
|
|
$response->getBody()->write($res); |
69
|
|
|
return $response->withHeader('Content-Type', $this->format); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function __invoke($request, $response, $next) |
73
|
|
|
{ |
74
|
|
|
$response = $this->getFormat($request, $response); |
75
|
|
|
if($response->getStatusCode() !== 200) |
76
|
|
|
{ |
77
|
|
|
return $response; |
78
|
|
|
} |
79
|
|
|
$request = $request->withAttribute('format', $this->format)->withAttribute('serializeOverrides', new \Slim\Collection()); |
80
|
|
|
$response = $next($request, $response); |
81
|
|
|
if($response->getHeaderLine('Content-Type') !== 'application/json;charset=utf-8' && $response->getHeaderLine('Content-Type') !== 'application/json') |
82
|
|
|
{ |
83
|
|
|
//The underlying API call gave us back a different content type. Just pass that on... |
84
|
|
|
return $response; |
85
|
|
|
} |
86
|
|
|
$overrides = $request->getAttribute('serializeOverrides'); |
87
|
|
|
if($overrides->has($this->format)) |
88
|
|
|
{ |
89
|
|
|
return $this->reserializeBody($response, $overrides->get($this->format)); |
90
|
|
|
} |
91
|
|
|
switch($this->format) |
92
|
|
|
{ |
93
|
|
|
case 'application/json': |
94
|
|
|
case 'application/x-javascript': |
95
|
|
|
case 'text/javascript': |
96
|
|
|
case 'text/x-javascript': |
97
|
|
|
case 'text/x-json': |
98
|
|
|
case 'json': |
99
|
|
|
return $response; |
100
|
|
|
case 'json-ss': |
101
|
|
|
case 'json-ss-dt': |
102
|
|
|
return $this->reserializeBody($response, '\Flipside\Serialize\JsonSpreadSheet'); |
103
|
|
View Code Duplication |
case 'data-table': |
|
|
|
|
104
|
|
|
//This is a special case for json... |
105
|
|
|
$body = $response->getBody(); |
106
|
|
|
$body->rewind(); |
107
|
|
|
$data = json_decode($body->getContents()); |
108
|
|
|
return $response->withJson(array('data'=>$data)); |
109
|
|
View Code Duplication |
case 'odata-json': |
|
|
|
|
110
|
|
|
//This is a special case for json... |
111
|
|
|
$body = $response->getBody(); |
112
|
|
|
$body->rewind(); |
113
|
|
|
$data = json_decode($body->getContents()); |
114
|
|
|
return $response->withJson(array('value'=>$data)); |
115
|
|
|
case 'xml': |
116
|
|
|
case 'application/xml': |
117
|
|
|
case 'text/xml': |
118
|
|
|
return $this->reserializeBody($response, '\Flipside\Serialize\XMLSerializer'); |
119
|
|
|
case 'csv': |
120
|
|
|
case 'text/csv': |
121
|
|
|
return $this->reserializeBody($response, '\Flipside\Serialize\CSVSerializer'); |
122
|
|
|
case 'xlsx': |
123
|
|
|
case 'xls': |
124
|
|
|
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': |
125
|
|
|
case 'application/vnd.ms-excel': |
126
|
|
|
return $this->reserializeBody($response, '\Flipside\Serialize\ExcelSerializer'); |
127
|
|
|
case 'yaml': |
128
|
|
|
case 'application/x-yaml': |
129
|
|
|
case 'text/x-yaml': |
130
|
|
|
return $this->reserializeBody($response, '\Flipside\Serialize\YAMLSerializer'); |
131
|
|
|
default: |
132
|
|
|
print_r($this->format); die(); |
133
|
|
|
break; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
return $response; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.