blob: 8f0ff586659aded0720ed6c04181a25a42307d26 (
plain)
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
|
<?php
namespace OCA\News\Controller;
use \OCP\AppFramework\Http;
use \OCP\AppFramework\Http\JSONResponse;
use \OCA\News\Db\IAPI;
trait ApiV2ResponseTrait
{
/**
* Serialize all data
*
* @param mixed $data IAPI or array,
* anything else will return an empty array
*
* @return array
*/
public function serialize($data): array
{
if ($data instanceof IAPI) {
return $data->toAPI2();
}
$return = [];
if (!is_array($data)) {
return $return;
}
foreach ($data as $entity) {
if ($entity instanceof IAPI) {
$return[] = $entity->toAPI2();
}
}
return $return;
}
public function response($data, $code=Http::STATUS_OK)
{
return new JSONResponse($data, $code);
}
/**
* @param \Exception $exception
* @param int $code
* @return \OCP\AppFramework\Http\JSONResponse
*/
public function errorResponse(\Exception $exception, $code)
{
return new JSONResponse([
'error' => [
'code' => $exception->getCode(),
'message' => $exception->getMessage()
]
], $code);
}
}
|