blob: c40ec9c25260e5395643d0032bdd0bce3b0c05f4 (
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
60
61
62
63
64
65
66
67
68
69
70
71
|
<?php
namespace OCA\News\Controller;
use \OCP\AppFramework\Http;
use \OCP\AppFramework\Http\JSONResponse;
use OCA\News\Db\IAPI;
trait ApiPayloadTrait
{
/**
* Serialize all data
*
* @param mixed $data IAPI or array,
* anything else will return an empty array
*
* @return array
*/
public function serialize($data): array
{
$return = [];
if ($data instanceof IAPI) {
return [$data->toAPI()];
}
if (!is_array($data)) {
return $return;
}
foreach ($data as $entity) {
if ($entity instanceof IAPI) {
$return[] = $entity->toAPI();
}
}
return $return;
}
/**
* Serialize an entity
*
* @param IAPI $data
*
* @return array
*/
public function serializeEntityV2($data, bool $reduced = false): array
{
return $data->toAPI2($reduced);
}
/**
* Serialize array of entities
*
* @param array $data
*
* @return array
*/
public function serializeEntitiesV2($data, bool $reduced = false): array
{
$return = [];
foreach ($data as $entity) {
$return[] = $entity->toAPI2($reduced);
}
return $return;
}
public function responseV2($data, $code = Http::STATUS_OK)
{
return new JSONResponse($data, $code);
}
}
|