summaryrefslogtreecommitdiffstats
path: root/db
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-05-14 17:49:52 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-05-14 17:49:52 +0200
commitf52cdaa750f1efa8cb76f6d88c9b1dc291f7b328 (patch)
treea2d6c7b403ea08cef061f0d599a51103f14ff9be /db
parent160a0dfebaeb21cc75d7166dfbac6d0ef1a51460 (diff)
add JsonSerializable to entities
Diffstat (limited to 'db')
-rw-r--r--db/entityjsonserializer.php28
-rw-r--r--db/feed.php69
-rw-r--r--db/folder.php35
-rw-r--r--db/item.php51
4 files changed, 135 insertions, 48 deletions
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 @@
+<?php
+/**
+ * ownCloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Alessandro Cosentino <cosenal@gmail.com>
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @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 [