@@ -31,126 +31,126 @@ |
||
31 | 31 | use OCP\AppFramework\Http\Response; |
32 | 32 | |
33 | 33 | abstract class BaseResponse extends Response { |
34 | - /** @var array */ |
|
35 | - protected $data; |
|
36 | - |
|
37 | - /** @var string */ |
|
38 | - protected $format; |
|
39 | - |
|
40 | - /** @var string */ |
|
41 | - protected $statusMessage; |
|
42 | - |
|
43 | - /** @var int */ |
|
44 | - protected $itemsCount; |
|
45 | - |
|
46 | - /** @var int */ |
|
47 | - protected $itemsPerPage; |
|
48 | - |
|
49 | - /** |
|
50 | - * BaseResponse constructor. |
|
51 | - * |
|
52 | - * @param DataResponse $dataResponse |
|
53 | - * @param string $format |
|
54 | - * @param string|null $statusMessage |
|
55 | - * @param int|null $itemsCount |
|
56 | - * @param int|null $itemsPerPage |
|
57 | - */ |
|
58 | - public function __construct(DataResponse $dataResponse, |
|
59 | - $format = 'xml', |
|
60 | - $statusMessage = null, |
|
61 | - $itemsCount = null, |
|
62 | - $itemsPerPage = null) { |
|
63 | - parent::__construct(); |
|
64 | - |
|
65 | - $this->format = $format; |
|
66 | - $this->statusMessage = $statusMessage; |
|
67 | - $this->itemsCount = $itemsCount; |
|
68 | - $this->itemsPerPage = $itemsPerPage; |
|
69 | - |
|
70 | - $this->data = $dataResponse->getData(); |
|
71 | - |
|
72 | - $this->setHeaders($dataResponse->getHeaders()); |
|
73 | - $this->setStatus($dataResponse->getStatus()); |
|
74 | - $this->setETag($dataResponse->getETag()); |
|
75 | - $this->setLastModified($dataResponse->getLastModified()); |
|
76 | - $this->setCookies($dataResponse->getCookies()); |
|
77 | - |
|
78 | - if ($dataResponse->isThrottled()) { |
|
79 | - $throttleMetadata = $dataResponse->getThrottleMetadata(); |
|
80 | - $this->throttle($throttleMetadata); |
|
81 | - } |
|
82 | - |
|
83 | - if ($format === 'json') { |
|
84 | - $this->addHeader( |
|
85 | - 'Content-Type', 'application/json; charset=utf-8' |
|
86 | - ); |
|
87 | - } else { |
|
88 | - $this->addHeader( |
|
89 | - 'Content-Type', 'application/xml; charset=utf-8' |
|
90 | - ); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param string[] $meta |
|
96 | - * @return string |
|
97 | - */ |
|
98 | - protected function renderResult(array $meta): string { |
|
99 | - $status = $this->getStatus(); |
|
100 | - if ($status === Http::STATUS_NO_CONTENT || |
|
101 | - $status === Http::STATUS_NOT_MODIFIED || |
|
102 | - ($status >= 100 && $status <= 199)) { |
|
103 | - // Those status codes are not supposed to have a body: |
|
104 | - // https://stackoverflow.com/q/8628725 |
|
105 | - return ''; |
|
106 | - } |
|
107 | - |
|
108 | - $response = [ |
|
109 | - 'ocs' => [ |
|
110 | - 'meta' => $meta, |
|
111 | - 'data' => $this->data, |
|
112 | - ], |
|
113 | - ]; |
|
114 | - |
|
115 | - if ($this->format === 'json') { |
|
116 | - return json_encode($response, JSON_HEX_TAG); |
|
117 | - } |
|
118 | - |
|
119 | - $writer = new \XMLWriter(); |
|
120 | - $writer->openMemory(); |
|
121 | - $writer->setIndent(true); |
|
122 | - $writer->startDocument(); |
|
123 | - $this->toXML($response, $writer); |
|
124 | - $writer->endDocument(); |
|
125 | - return $writer->outputMemory(true); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @param array $array |
|
130 | - * @param \XMLWriter $writer |
|
131 | - */ |
|
132 | - protected function toXML(array $array, \XMLWriter $writer) { |
|
133 | - foreach ($array as $k => $v) { |
|
134 | - if (\is_string($k) && strpos($k, '@') === 0) { |
|
135 | - $writer->writeAttribute(substr($k, 1), $v); |
|
136 | - continue; |
|
137 | - } |
|
138 | - |
|
139 | - if (\is_numeric($k)) { |
|
140 | - $k = 'element'; |
|
141 | - } |
|
142 | - |
|
143 | - if (\is_array($v)) { |
|
144 | - $writer->startElement($k); |
|
145 | - $this->toXML($v, $writer); |
|
146 | - $writer->endElement(); |
|
147 | - } else { |
|
148 | - $writer->writeElement($k, $v); |
|
149 | - } |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - public function getOCSStatus() { |
|
154 | - return parent::getStatus(); |
|
155 | - } |
|
34 | + /** @var array */ |
|
35 | + protected $data; |
|
36 | + |
|
37 | + /** @var string */ |
|
38 | + protected $format; |
|
39 | + |
|
40 | + /** @var string */ |
|
41 | + protected $statusMessage; |
|
42 | + |
|
43 | + /** @var int */ |
|
44 | + protected $itemsCount; |
|
45 | + |
|
46 | + /** @var int */ |
|
47 | + protected $itemsPerPage; |
|
48 | + |
|
49 | + /** |
|
50 | + * BaseResponse constructor. |
|
51 | + * |
|
52 | + * @param DataResponse $dataResponse |
|
53 | + * @param string $format |
|
54 | + * @param string|null $statusMessage |
|
55 | + * @param int|null $itemsCount |
|
56 | + * @param int|null $itemsPerPage |
|
57 | + */ |
|
58 | + public function __construct(DataResponse $dataResponse, |
|
59 | + $format = 'xml', |
|
60 | + $statusMessage = null, |
|
61 | + $itemsCount = null, |
|
62 | + $itemsPerPage = null) { |
|
63 | + parent::__construct(); |
|
64 | + |
|
65 | + $this->format = $format; |
|
66 | + $this->statusMessage = $statusMessage; |
|
67 | + $this->itemsCount = $itemsCount; |
|
68 | + $this->itemsPerPage = $itemsPerPage; |
|
69 | + |
|
70 | + $this->data = $dataResponse->getData(); |
|
71 | + |
|
72 | + $this->setHeaders($dataResponse->getHeaders()); |
|
73 | + $this->setStatus($dataResponse->getStatus()); |
|
74 | + $this->setETag($dataResponse->getETag()); |
|
75 | + $this->setLastModified($dataResponse->getLastModified()); |
|
76 | + $this->setCookies($dataResponse->getCookies()); |
|
77 | + |
|
78 | + if ($dataResponse->isThrottled()) { |
|
79 | + $throttleMetadata = $dataResponse->getThrottleMetadata(); |
|
80 | + $this->throttle($throttleMetadata); |
|
81 | + } |
|
82 | + |
|
83 | + if ($format === 'json') { |
|
84 | + $this->addHeader( |
|
85 | + 'Content-Type', 'application/json; charset=utf-8' |
|
86 | + ); |
|
87 | + } else { |
|
88 | + $this->addHeader( |
|
89 | + 'Content-Type', 'application/xml; charset=utf-8' |
|
90 | + ); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param string[] $meta |
|
96 | + * @return string |
|
97 | + */ |
|
98 | + protected function renderResult(array $meta): string { |
|
99 | + $status = $this->getStatus(); |
|
100 | + if ($status === Http::STATUS_NO_CONTENT || |
|
101 | + $status === Http::STATUS_NOT_MODIFIED || |
|
102 | + ($status >= 100 && $status <= 199)) { |
|
103 | + // Those status codes are not supposed to have a body: |
|
104 | + // https://stackoverflow.com/q/8628725 |
|
105 | + return ''; |
|
106 | + } |
|
107 | + |
|
108 | + $response = [ |
|
109 | + 'ocs' => [ |
|
110 | + 'meta' => $meta, |
|
111 | + 'data' => $this->data, |
|
112 | + ], |
|
113 | + ]; |
|
114 | + |
|
115 | + if ($this->format === 'json') { |
|
116 | + return json_encode($response, JSON_HEX_TAG); |
|
117 | + } |
|
118 | + |
|
119 | + $writer = new \XMLWriter(); |
|
120 | + $writer->openMemory(); |
|
121 | + $writer->setIndent(true); |
|
122 | + $writer->startDocument(); |
|
123 | + $this->toXML($response, $writer); |
|
124 | + $writer->endDocument(); |
|
125 | + return $writer->outputMemory(true); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @param array $array |
|
130 | + * @param \XMLWriter $writer |
|
131 | + */ |
|
132 | + protected function toXML(array $array, \XMLWriter $writer) { |
|
133 | + foreach ($array as $k => $v) { |
|
134 | + if (\is_string($k) && strpos($k, '@') === 0) { |
|
135 | + $writer->writeAttribute(substr($k, 1), $v); |
|
136 | + continue; |
|
137 | + } |
|
138 | + |
|
139 | + if (\is_numeric($k)) { |
|
140 | + $k = 'element'; |
|
141 | + } |
|
142 | + |
|
143 | + if (\is_array($v)) { |
|
144 | + $writer->startElement($k); |
|
145 | + $this->toXML($v, $writer); |
|
146 | + $writer->endElement(); |
|
147 | + } else { |
|
148 | + $writer->writeElement($k, $v); |
|
149 | + } |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + public function getOCSStatus() { |
|
154 | + return parent::getStatus(); |
|
155 | + } |
|
156 | 156 | } |