separate serialization functions
Signed-off-by: Paul Tirk <paultirk@paultirk.com>
This commit is contained in:
parent
f22eddb91d
commit
323f215e3a
|
@ -20,28 +20,29 @@ use \OCA\News\Db\IAPI;
|
|||
trait ApiV2ResponseTrait
|
||||
{
|
||||
/**
|
||||
* Serialize all data
|
||||
* Serialize an entity
|
||||
*
|
||||
* @param mixed $data IAPI or array,
|
||||
* anything else will return an empty array
|
||||
* @param IAPI $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function serialize($data, bool $reduced = false): array
|
||||
public function serializeEntity($data, bool $reduced = false): array
|
||||
{
|
||||
if ($data instanceof IAPI) {
|
||||
return $data->toAPI2($reduced);
|
||||
}
|
||||
return $data->toAPI2($reduced);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize array of entities
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function serializeEntities($data, bool $reduced = false): array
|
||||
{
|
||||
$return = [];
|
||||
if (!is_array($data)) {
|
||||
return $return;
|
||||
}
|
||||
|
||||
foreach ($data as $entity) {
|
||||
if ($entity instanceof IAPI) {
|
||||
$return[] = $entity->toAPI2($reduced);
|
||||
}
|
||||
$return[] = $entity->toAPI2($reduced);
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ class FolderApiV2Controller extends ApiController
|
|||
{
|
||||
try {
|
||||
$this->folderService->purgeDeleted($this->getUserId(), false);
|
||||
$responseData = $this->serialize(
|
||||
$responseData = $this->serializeEntity(
|
||||
$this->folderService->create($this->getUserId(), $name)
|
||||
);
|
||||
return $this->response([
|
||||
|
@ -103,7 +103,7 @@ class FolderApiV2Controller extends ApiController
|
|||
public function deleteFolder($folderId)
|
||||
{
|
||||
try {
|
||||
$responseData = $this->serialize(
|
||||
$responseData = $this->serializeEntity(
|
||||
$this->folderService->delete($this->getUserId(), $folderId)
|
||||
);
|
||||
return $this->response([
|
||||
|
|
|
@ -175,7 +175,7 @@ class Folder extends Entity implements IAPI, \JsonSerializable
|
|||
);
|
||||
}
|
||||
|
||||
public function toAPI2(): array
|
||||
public function toAPI2(bool $reduced = false): array
|
||||
{
|
||||
return $this->toAPI();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue