From f52cdaa750f1efa8cb76f6d88c9b1dc291f7b328 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 14 May 2014 17:49:52 +0200 Subject: add JsonSerializable to entities --- db/entityjsonserializer.php | 28 ++++++++++++++++++ db/feed.php | 69 ++++++++++++++++++++++++++++++--------------- db/folder.php | 35 ++++++++++++++++------- db/item.php | 51 +++++++++++++++++++++++---------- 4 files changed, 135 insertions(+), 48 deletions(-) create mode 100644 db/entityjsonserializer.php (limited to 'db') diff --git a/db/entityjsonserializer.php b/db/entityjsonserializer.php new file mode 100644 index 000000000..7e3357304 --- /dev/null +++ b/db/entityjsonserializer.php @@ -0,0 +1,28 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Db; + +trait EntityJSONSerializer { + + + public function serializeFields($properties) { + $result = []; + foreach($properties as $property) { + $result[$property] = $this->$property; + } + return $result; + } + + +} \ No newline at end of file diff --git a/db/feed.php b/db/feed.php index 30bcbf865..4f1e0979e 100644 --- a/db/feed.php +++ b/db/feed.php @@ -43,20 +43,22 @@ use \OCP\AppFramework\Db\Entity; * @method integer getArticlesPerUpdate() * @method void setArticlesPerUpdate(integer $value) */ -class Feed extends Entity implements IAPI { +class Feed extends Entity implements IAPI, \JsonSerializable { - public $userId; - public $urlHash; - public $url; - public $title; - public $faviconLink; - public $added; - public $folderId; - public $unreadCount; - public $link; - public $preventUpdate; - public $deletedAt; - public $articlesPerUpdate; + use EntityJSONSerializer; + + protected $userId; + protected $urlHash; + protected $url; + protected $title; + protected $faviconLink; + protected $added; + protected $folderId; + protected $unreadCount; + protected $link; + protected $preventUpdate; + protected $deletedAt; + protected $articlesPerUpdate; public function __construct(){ $this->addType('parentId', 'integer'); @@ -69,17 +71,38 @@ class Feed extends Entity implements IAPI { } + /** + * Turns entitie attributes into an array + */ + public function jsonSerialize() { + return $this->serializeFields([ + 'id', + 'userId', + 'urlHash', + 'url', + 'title', + 'faviconLink', + 'added', + 'folderId', + 'unreadCount', + 'link', + 'preventUpdate', + 'deletedAt', + 'articlesPerUpdate', + ]); + } + public function toAPI() { - return [ - 'id' => $this->getId(), - 'url' => $this->getUrl(), - 'title' => $this->getTitle(), - 'faviconLink' => $this->getFaviconLink(), - 'added' => $this->getAdded(), - 'folderId' => $this->getFolderId(), - 'unreadCount' => $this->getUnreadCount(), - 'link' => $this->getLink() - ]; + return $this->serializeFields([ + 'id', + 'url', + 'title', + 'faviconLink', + 'added', + 'folderId', + 'unreadCount', + 'link' + ]); } diff --git a/db/folder.php b/db/folder.php index ed536e18c..d5f50685f 100644 --- a/db/folder.php +++ b/db/folder.php @@ -29,13 +29,15 @@ use \OCP\AppFramework\Db\Entity; * @method integer getDeletedAt() * @method void setDeletedAt(integer $value) */ -class Folder extends Entity implements IAPI { +class Folder extends Entity implements IAPI, \JsonSerializable { - public $parentId; - public $name; - public $userId; - public $opened; - public $deletedAt; + use EntityJSONSerializer; + + protected $parentId; + protected $name; + protected $userId; + protected $opened; + protected $deletedAt; public function __construct(){ $this->addType('parentId', 'integer'); @@ -43,11 +45,24 @@ class Folder extends Entity implements IAPI { $this->addType('deletedAt', 'integer'); } + /** + * Turns entitie attributes into an array + */ + public function jsonSerialize() { + return $this->serializeFields([ + 'id', + 'parentId', + 'name', + 'userId', + 'opened', + 'deletedAt', + ]); + } public function toAPI() { - return [ - 'id' => $this->getId(), - 'name' => $this->getName() - ]; + return $this->serializeFields([ + 'id', + 'name' + ]); } } \ No newline at end of file diff --git a/db/item.php b/db/item.php index 3b18cbc8e..e7cbb7793 100644 --- a/db/item.php +++ b/db/item.php @@ -44,21 +44,22 @@ use \OCP\AppFramework\Db\Entity; * @method integer getLastModified() * @method void setLastModified(integer $value) */ -class Item extends Entity implements IAPI { - - public $guidHash; - public $guid; - public $url; - public $title; - public $author; - public $pubDate; - public $body; - public $enclosureMime; - public $enclosureLink; - public $feedId; - public $status = 0; - public $lastModified; - +class Item extends Entity implements IAPI, \JsonSerializable { + + use EntityJSONSerializer; + + protected $guidHash; + protected $guid; + protected $url; + protected $title; + protected $author; + protected $pubDate; + protected $body; + protected $enclosureMime; + protected $enclosureLink; + protected $feedId; + protected $status = 0; + protected $lastModified; public function __construct(){ $this->addType('pubDate', 'integer'); @@ -104,6 +105,26 @@ class Item extends Entity implements IAPI { return !$this->isStarred(); } + /** + * Turns entitie attributes into an array + */ + public function jsonSerialize() { + return $this->serializeFields([ + 'id', + 'guidHash', + 'guid', + 'url', + 'title', + 'author', + 'pubDate', + 'body', + 'enclosureMime', + 'enclosureLink', + 'feedId', + 'status', + 'lastModified', + ]); + } public function toAPI() { return [ -- cgit v1.2.3