1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\RestServer\OutputProcessor; |
4
|
|
|
|
5
|
|
|
use ByJG\RestServer\Exception\OperationIdInvalidException; |
6
|
|
|
use ByJG\RestServer\HttpResponse; |
7
|
|
|
|
8
|
|
|
abstract class BaseOutputProcessor implements OutputProcessorInterface |
9
|
|
|
{ |
10
|
|
|
protected $buildNull = true; |
11
|
|
|
protected $onlyString = false; |
12
|
|
|
protected $header = []; |
13
|
|
|
protected $contentType = ""; |
14
|
|
|
|
15
|
7 |
|
public static function getFromContentType($contentType) |
16
|
|
|
{ |
17
|
|
|
$mimeTypeOutputProcessor = [ |
18
|
|
|
"text/xml" => XmlOutputProcessor::class, |
19
|
|
|
"application/xml" => XmlOutputProcessor::class, |
20
|
|
|
"text/html" => HtmlOutputProcessor::class, |
21
|
|
|
"application/json" => JsonOutputProcessor::class, |
22
|
|
|
"*/*" => JsonOutputProcessor::class, |
23
|
|
|
]; |
24
|
|
|
|
25
|
7 |
|
if (!isset($mimeTypeOutputProcessor[$contentType])) { |
26
|
|
|
throw new OperationIdInvalidException("There is no output rocessor for $contentType"); |
27
|
|
|
} |
28
|
|
|
|
29
|
7 |
|
return $mimeTypeOutputProcessor[$contentType]; |
30
|
|
|
} |
31
|
|
|
|
32
|
3 |
|
public static function getFromHttpAccept() |
33
|
|
|
{ |
34
|
3 |
|
$accept = isset($_SERVER["HTTP_ACCEPT"]) ? $_SERVER["HTTP_ACCEPT"] : "application/json"; |
35
|
|
|
|
36
|
3 |
|
$acceptList = explode(",", $accept); |
37
|
|
|
|
38
|
3 |
|
return self::getFromClassName(self::getFromContentType($acceptList[0])); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $className |
43
|
|
|
* @return OutputProcessorInterface |
44
|
|
|
*/ |
45
|
6 |
|
public static function getFromClassName($className) |
46
|
|
|
{ |
47
|
6 |
|
if ($className instanceof \Closure) { |
48
|
|
|
return $className(); |
49
|
|
|
} |
50
|
6 |
|
return new $className(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function getOutputProcessorInstance($contentType) |
54
|
|
|
{ |
55
|
|
|
$class = self::getFromContentType($contentType); |
56
|
|
|
|
57
|
|
|
return new $class(); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
public function writeContentType() |
61
|
|
|
{ |
62
|
3 |
|
if (defined("RESTSERVER_TEST")) { |
63
|
3 |
|
return; |
64
|
|
|
} |
65
|
|
|
header("Content-Type: " . $this->contentType); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
public function getContentType() |
69
|
|
|
{ |
70
|
4 |
|
return $this->contentType; |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
protected function writeHeader(HttpResponse $response) |
74
|
|
|
{ |
75
|
4 |
|
foreach ($response->getHeaders() as $header) { |
76
|
|
|
if (is_array($header)) { |
77
|
|
|
$this->header($header[0], $header[1]); |
|
|
|
|
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
$this->header($header); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
http_response_code($response->getResponseCode()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function writeData($data) |
87
|
|
|
{ |
88
|
|
|
echo $data; |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
public function processResponse(HttpResponse $response) |
92
|
|
|
{ |
93
|
6 |
|
$this->writeHeader($response); |
94
|
|
|
|
95
|
|
|
$serialized = $response |
96
|
6 |
|
->getResponseBag() |
97
|
6 |
|
->process($this->buildNull, $this->onlyString); |
98
|
|
|
|
99
|
6 |
|
$this->writeData( |
100
|
6 |
|
$this->getFormatter()->process($serialized) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.