diff options
author | Paul Tirk <paultirk@paultirk.com> | 2020-12-27 19:35:58 +0000 |
---|---|---|
committer | Sean Molenaar <SMillerDev@users.noreply.github.com> | 2021-04-08 10:23:11 +0200 |
commit | 566a21b4a88cc1d149aef39eff7b95b42613cec5 (patch) | |
tree | afcfa2372d84aac600578aebb6ec21235360e9ac | |
parent | 06a45387ef36ec29fb0b80e6481db09911ad1935 (diff) |
add possibility to serialize a reduced version of an entity
Signed-off-by: Paul Tirk <paultirk@paultirk.com>
-rw-r--r-- | lib/Controller/ApiV2ResponseTrait.php | 6 | ||||
-rw-r--r-- | lib/Db/IAPI.php | 2 | ||||
-rw-r--r-- | 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(), |