add JsonSerializable to entities

This commit is contained in:
Bernhard Posselt 2014-05-14 17:49:52 +02:00
Родитель 160a0dfeba
Коммит f52cdaa750
4 изменённых файлов: 133 добавлений и 46 удалений

Просмотреть файл

@ -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;
}
}

Просмотреть файл

@ -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'
]);
}

Просмотреть файл

@ -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'
]);
}
}

Просмотреть файл

@ -44,21 +44,22 @@ use \OCP\AppFramework\Db\Entity;
* @method integer getLastModified()
* @method void setLastModified(integer $value)
*/
class Item extends Entity implements IAPI {
class Item extends Entity implements IAPI, \JsonSerializable {
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;
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 [