|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\RestServer\OutputProcessor; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\RestServer\Exception\OperationIdInvalidException; |
|
6
|
|
|
use ByJG\RestServer\HttpResponse; |
|
7
|
|
|
use ByJG\RestServer\ResponseBag; |
|
8
|
|
|
use ByJG\RestServer\Writer\WriterInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class BaseOutputProcessor implements OutputProcessorInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected $buildNull = true; |
|
13
|
|
|
protected $onlyString = false; |
|
14
|
|
|
protected $header = []; |
|
15
|
|
|
protected $contentType = ""; |
|
16
|
|
|
|
|
17
|
|
|
/** @var WriterInterface */ |
|
18
|
|
|
protected $writer; |
|
19
|
|
|
|
|
20
|
16 |
|
public function setWriter(WriterInterface $writer) |
|
21
|
|
|
{ |
|
22
|
16 |
|
$this->writer = $writer; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
4 |
|
public static function getFromContentType($contentType) |
|
26
|
|
|
{ |
|
27
|
|
|
$mimeTypeOutputProcessor = [ |
|
28
|
|
|
"text/xml" => XmlOutputProcessor::class, |
|
29
|
|
|
"application/xml" => XmlOutputProcessor::class, |
|
30
|
|
|
"text/html" => HtmlOutputProcessor::class, |
|
31
|
|
|
"application/json" => JsonOutputProcessor::class, |
|
32
|
|
|
"*/*" => JsonOutputProcessor::class, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
4 |
|
if (!isset($mimeTypeOutputProcessor[$contentType])) { |
|
36
|
|
|
throw new OperationIdInvalidException("There is no output processor for $contentType"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
return $mimeTypeOutputProcessor[$contentType]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public static function getFromHttpAccept() |
|
43
|
|
|
{ |
|
44
|
|
|
$accept = isset($_SERVER["HTTP_ACCEPT"]) ? $_SERVER["HTTP_ACCEPT"] : "application/json"; |
|
45
|
|
|
|
|
46
|
|
|
$acceptList = explode(",", $accept); |
|
47
|
|
|
|
|
48
|
|
|
return self::getFromClassName(self::getFromContentType($acceptList[0])); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $className |
|
53
|
|
|
* @return OutputProcessorInterface |
|
54
|
|
|
*/ |
|
55
|
12 |
|
public static function getFromClassName($className) |
|
56
|
|
|
{ |
|
57
|
12 |
|
if ($className instanceof \Closure) { |
|
58
|
|
|
return $className(); |
|
59
|
|
|
} |
|
60
|
12 |
|
return new $className(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function getOutputProcessorInstance($contentType) |
|
64
|
|
|
{ |
|
65
|
|
|
$class = self::getFromContentType($contentType); |
|
66
|
|
|
|
|
67
|
|
|
return new $class(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
12 |
|
public function writeContentType() |
|
71
|
|
|
{ |
|
72
|
12 |
|
$this->writer->header("Content-Type: $this->contentType", true); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
public function getContentType() |
|
76
|
|
|
{ |
|
77
|
4 |
|
return $this->contentType; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
15 |
|
public function writeHeader(HttpResponse $response) |
|
81
|
|
|
{ |
|
82
|
15 |
|
$this->writer->responseCode($response->getResponseCode(), $response->getResponseCodeDescription()); |
|
83
|
|
|
|
|
84
|
15 |
|
foreach ($response->getHeaders() as $header => $value) { |
|
85
|
4 |
|
if (is_array($value)) { |
|
86
|
|
|
$this->writer->header("$header: " . array_shift($value), true); |
|
87
|
|
|
foreach ($value as $headerValue) { |
|
88
|
|
|
$this->writer->header("$header: $headerValue", false); |
|
89
|
|
|
} |
|
90
|
|
|
} else { |
|
91
|
4 |
|
$this->writer->header("$header: $value", true); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
15 |
|
public function writeData($data) |
|
97
|
|
|
{ |
|
98
|
15 |
|
$this->writer->echo($data); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
15 |
|
public function processResponse(HttpResponse $response) |
|
102
|
|
|
{ |
|
103
|
15 |
|
$this->writeHeader($response); |
|
104
|
|
|
|
|
105
|
|
|
$serialized = $response |
|
106
|
15 |
|
->getResponseBag() |
|
107
|
15 |
|
->process($this->buildNull, $this->onlyString); |
|
108
|
|
|
|
|
109
|
15 |
|
if ($response->getResponseBag()->getSerializationRule() === ResponseBag::RAW) { |
|
110
|
2 |
|
$this->writeData($serialized); |
|
111
|
|
|
} else { |
|
112
|
13 |
|
$this->writeData( |
|
113
|
13 |
|
$this->getFormatter()->process($serialized) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
15 |
|
$this->writer->flush(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|