From 566a21b4a88cc1d149aef39eff7b95b42613cec5 Mon Sep 17 00:00:00 2001 From: Paul Tirk Date: Sun, 27 Dec 2020 19:35:58 +0000 Subject: add possibility to serialize a reduced version of an entity Signed-off-by: Paul Tirk --- lib/Controller/ApiV2ResponseTrait.php | 6 +++--- lib/Db/IAPI.php | 2 +- lib/Db/Item.php | 12 ++++++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/Controller/ApiV2ResponseTrait.php b/lib/Controller/ApiV2ResponseTrait.php index 2a8ed18ab..3538f1948 100644 --- a/lib/Controller/ApiV2ResponseTrait.php +++ b/lib/Controller/ApiV2ResponseTrait.php @@ -27,10 +27,10 @@ trait ApiV2ResponseTrait * * @return array */ - public function serialize($data): array + public function serialize($data, bool $reduced = false): array { if ($data instanceof IAPI) { - return $data->toAPI2(); + return $data->toAPI2($reduced); } $return = []; @@ -40,7 +40,7 @@ trait ApiV2ResponseTrait foreach ($data as $entity) { if ($entity instanceof IAPI) { - $return[] = $entity->toAPI2(); + $return[] = $entity->toAPI2($reduced); } } return $return; diff --git a/lib/Db/IAPI.php b/lib/Db/IAPI.php index 120d5551c..3184d7a79 100644 --- a/lib/Db/IAPI.php +++ b/lib/Db/IAPI.php @@ -18,5 +18,5 @@ namespace OCA\News\Db; interface IAPI { public function toAPI(); - public function toAPI2(); + public function toAPI2(bool $reduced = false); } diff --git a/lib/Db/Item.php b/lib/Db/Item.php index 9fae011f1..4b679dfa0 100644 --- a/lib/Db/Item.php +++ b/lib/Db/Item.php @@ -576,8 +576,16 @@ class Item extends Entity implements IAPI, \JsonSerializable ]; } - public function toAPI2(): array - { + public function toAPI2(bool $reduced = false): array + { + if ($reduced) { + return [ + 'id' => $this->getId(), + 'isUnread' => $this->isUnread(), + 'isStarred' => $this->isStarred() + ]; + } + return [ 'id' => $this->getId(), 'url' => $this->getUrl(), -- cgit v1.2.3