diff --git a/lib/Db/Acl.php b/lib/Db/Acl.php index 5f5f25963..b9ad83abf 100644 --- a/lib/Db/Acl.php +++ b/lib/Db/Acl.php @@ -71,16 +71,9 @@ class Acl extends RelationalEntity implements \JsonSerializable { } public function jsonSerialize() { - return [ - 'id' => $this->id, - 'participant' => $this->participant, - 'type' => $this->getTypeString(), - 'boardId' => $this->boardId, - 'permissionEdit' => $this->getPermissionEdit(), - 'permissionShare' => $this->getPermissionShare(), - 'permissionManage' => $this->getPermissionManage(), - 'owner' => $this->getOwner() - ]; + $json = parent::jsonSerialize(); + $json['type'] = $this->getTypeString(); + return $json; } public function getTypeString() { diff --git a/lib/Db/Board.php b/lib/Db/Board.php index d8b76fa02..b24a74102 100644 --- a/lib/Db/Board.php +++ b/lib/Db/Board.php @@ -47,18 +47,11 @@ class Board extends RelationalEntity implements JsonSerializable { } public function jsonSerialize() { - $result = [ - 'id' => $this->id, - 'title' => $this->title, - 'owner' => $this->owner, - 'color' => $this->color, - 'labels' => $this->labels, - 'acl' => $this->acl, - ]; - if ($this->shared !== -1) { - $result['shared'] = $this->shared; + $json = parent::jsonSerialize(); + if ($this->shared === -1) { + unset($json['shared']); } - return $result; + return $json; } public function setLabels($labels) { diff --git a/lib/Db/Card.php b/lib/Db/Card.php index c4c938a06..bdbde9cff 100644 --- a/lib/Db/Card.php +++ b/lib/Db/Card.php @@ -50,19 +50,4 @@ class Card extends RelationalEntity implements JsonSerializable { $this->addRelation('labels'); } - public function jsonSerialize() { - return [ - 'id' => $this->id, - 'title' => $this->title, - 'description' => $this->description, - 'type' => $this->type, - 'lastModified' => $this->lastModified, - 'createdAt' => $this->createdAt, - 'owner' => $this->owner, - 'order' => $this->order, - 'stackId' => $this->stackId, - 'labels' => $this->labels, - 'archived' => $this->archived, - ]; - } } diff --git a/lib/Db/Label.php b/lib/Db/Label.php index 48bc922d9..5ea31d467 100644 --- a/lib/Db/Label.php +++ b/lib/Db/Label.php @@ -33,16 +33,8 @@ class Label extends RelationalEntity implements JsonSerializable { protected $color; protected $boardId; protected $cardId; + public function __construct() { $this->addType('id', 'integer'); } - public function jsonSerialize() { - return [ - 'id' => $this->id, - 'title' => $this->title, - 'boardId' => $this->boardId, - 'cardId' => $this->cardId, - 'color' => $this->color, - ]; - } } \ No newline at end of file diff --git a/lib/Db/RelationalEntity.php b/lib/Db/RelationalEntity.php index a2da98ba5..130e6fe09 100644 --- a/lib/Db/RelationalEntity.php +++ b/lib/Db/RelationalEntity.php @@ -21,17 +21,10 @@ * */ -/** - * Created by PhpStorm. - * User: jus - * Date: 22.06.16 - * Time: 13:32 - */ - namespace OCA\Deck\Db; -class RelationalEntity extends \OCP\AppFramework\Db\Entity { +class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSerializable { private $_relations = array(); @@ -56,4 +49,21 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity { } } + /** + * @return array serialized data + */ + public function jsonSerialize() { + $properties = get_object_vars($this); + $reflection = new \ReflectionClass($this); + $json = []; + foreach($properties as $property=>$value) { + if(substr($property, 0, 1) !== '_' && $reflection->hasProperty($property)) { + $propertyReflection = $reflection->getProperty($property); + if(!$propertyReflection->isPrivate()) { + $json[$property] = $this->getter($property); + } + } + } + return $json; + } } \ No newline at end of file diff --git a/lib/Db/Stack.php b/lib/Db/Stack.php index b94499dba..9ca46db64 100644 --- a/lib/Db/Stack.php +++ b/lib/Db/Stack.php @@ -45,20 +45,10 @@ class Stack extends RelationalEntity implements JsonSerializable { } public function jsonSerialize() { - if (!empty($this->cards)) { - return [ - 'id' => $this->id, - 'title' => $this->title, - 'order' => $this->order, - 'boardId' => $this->boardId, - 'cards' => $this->cards - ]; + $json = parent::jsonSerialize(); + if (empty($this->cards)) { + unset($json['cards']); } - return [ - 'id' => $this->id, - 'title' => $this->title, - 'order' => $this->order, - 'boardId' => $this->boardId - ]; + return $json; } } \ No newline at end of file diff --git a/tests/unit/Db/BoardTest.php b/tests/unit/Db/BoardTest.php index dc814d7c6..b8af9b41d 100644 --- a/tests/unit/Db/BoardTest.php +++ b/tests/unit/Db/BoardTest.php @@ -21,7 +21,8 @@ class BoardTest extends \PHPUnit_Framework_TestCase { 'owner' => "admin", 'color' => "000000", 'labels' => null, - 'acl' => null + 'acl' => null, + 'archived' => false ], $board->jsonSerialize()); } @@ -34,7 +35,8 @@ class BoardTest extends \PHPUnit_Framework_TestCase { 'owner' => "admin", 'color' => "000000", 'labels' => array("foo", "bar"), - 'acl' => null + 'acl' => null, + 'archived' => false ], $board->jsonSerialize()); } public function testSetAcl() { @@ -55,6 +57,7 @@ class BoardTest extends \PHPUnit_Framework_TestCase { 'color' => "000000", 'labels' => null, 'acl' => null, + 'archived' => false, 'shared' => 1, ], $board->jsonSerialize()); } diff --git a/tests/unit/Db/EntityTest.php b/tests/unit/Db/RelationalEntityTest.php similarity index 84% rename from tests/unit/Db/EntityTest.php rename to tests/unit/Db/RelationalEntityTest.php index 251c28c07..4ede1ed60 100644 --- a/tests/unit/Db/EntityTest.php +++ b/tests/unit/Db/RelationalEntityTest.php @@ -23,7 +23,7 @@ namespace OCA\Deck\Db; -class EntityTest extends \PHPUnit_Framework_TestCase { +class RelationalEntityTest extends \Test\TestCase { public function testRelation() { $entity = new RelationalEntity(); @@ -40,4 +40,13 @@ class EntityTest extends \PHPUnit_Framework_TestCase { $this->assertEquals(['foo'=>true], $entity->getUpdatedFields()); } + public function testJsonSerialize() { + $entity = new RelationalEntity(); + $entity->setId(123); + $json = [ + 'id' => 123, + ]; + $this->assertEquals($json, $entity->jsonSerialize()); + } + } \ No newline at end of file