Merge pull request #66 from nextcloud/default-json

Add jsonSerialize method to RelationalEntity
This commit is contained in:
Julius Härtl
2017-03-20 14:37:16 +01:00
committed by GitHub
8 changed files with 45 additions and 70 deletions

View File

@@ -71,16 +71,9 @@ class Acl extends RelationalEntity implements \JsonSerializable {
} }
public function jsonSerialize() { public function jsonSerialize() {
return [ $json = parent::jsonSerialize();
'id' => $this->id, $json['type'] = $this->getTypeString();
'participant' => $this->participant, return $json;
'type' => $this->getTypeString(),
'boardId' => $this->boardId,
'permissionEdit' => $this->getPermissionEdit(),
'permissionShare' => $this->getPermissionShare(),
'permissionManage' => $this->getPermissionManage(),
'owner' => $this->getOwner()
];
} }
public function getTypeString() { public function getTypeString() {

View File

@@ -47,18 +47,11 @@ class Board extends RelationalEntity implements JsonSerializable {
} }
public function jsonSerialize() { public function jsonSerialize() {
$result = [ $json = parent::jsonSerialize();
'id' => $this->id, if ($this->shared === -1) {
'title' => $this->title, unset($json['shared']);
'owner' => $this->owner,
'color' => $this->color,
'labels' => $this->labels,
'acl' => $this->acl,
];
if ($this->shared !== -1) {
$result['shared'] = $this->shared;
} }
return $result; return $json;
} }
public function setLabels($labels) { public function setLabels($labels) {

View File

@@ -50,19 +50,4 @@ class Card extends RelationalEntity implements JsonSerializable {
$this->addRelation('labels'); $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,
];
}
} }

View File

@@ -33,16 +33,8 @@ class Label extends RelationalEntity implements JsonSerializable {
protected $color; protected $color;
protected $boardId; protected $boardId;
protected $cardId; protected $cardId;
public function __construct() { public function __construct() {
$this->addType('id', 'integer'); $this->addType('id', 'integer');
} }
public function jsonSerialize() {
return [
'id' => $this->id,
'title' => $this->title,
'boardId' => $this->boardId,
'cardId' => $this->cardId,
'color' => $this->color,
];
}
} }

View File

@@ -21,17 +21,10 @@
* *
*/ */
/**
* Created by PhpStorm.
* User: jus
* Date: 22.06.16
* Time: 13:32
*/
namespace OCA\Deck\Db; namespace OCA\Deck\Db;
class RelationalEntity extends \OCP\AppFramework\Db\Entity { class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSerializable {
private $_relations = array(); 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;
}
} }

View File

@@ -45,20 +45,10 @@ class Stack extends RelationalEntity implements JsonSerializable {
} }
public function jsonSerialize() { public function jsonSerialize() {
if (!empty($this->cards)) { $json = parent::jsonSerialize();
return [ if (empty($this->cards)) {
'id' => $this->id, unset($json['cards']);
'title' => $this->title,
'order' => $this->order,
'boardId' => $this->boardId,
'cards' => $this->cards
];
} }
return [ return $json;
'id' => $this->id,
'title' => $this->title,
'order' => $this->order,
'boardId' => $this->boardId
];
} }
} }

View File

@@ -21,7 +21,8 @@ class BoardTest extends \PHPUnit_Framework_TestCase {
'owner' => "admin", 'owner' => "admin",
'color' => "000000", 'color' => "000000",
'labels' => null, 'labels' => null,
'acl' => null 'acl' => null,
'archived' => false
], $board->jsonSerialize()); ], $board->jsonSerialize());
} }
@@ -34,7 +35,8 @@ class BoardTest extends \PHPUnit_Framework_TestCase {
'owner' => "admin", 'owner' => "admin",
'color' => "000000", 'color' => "000000",
'labels' => array("foo", "bar"), 'labels' => array("foo", "bar"),
'acl' => null 'acl' => null,
'archived' => false
], $board->jsonSerialize()); ], $board->jsonSerialize());
} }
public function testSetAcl() { public function testSetAcl() {
@@ -55,6 +57,7 @@ class BoardTest extends \PHPUnit_Framework_TestCase {
'color' => "000000", 'color' => "000000",
'labels' => null, 'labels' => null,
'acl' => null, 'acl' => null,
'archived' => false,
'shared' => 1, 'shared' => 1,
], $board->jsonSerialize()); ], $board->jsonSerialize());
} }

View File

@@ -23,7 +23,7 @@
namespace OCA\Deck\Db; namespace OCA\Deck\Db;
class EntityTest extends \PHPUnit_Framework_TestCase { class RelationalEntityTest extends \Test\TestCase {
public function testRelation() { public function testRelation() {
$entity = new RelationalEntity(); $entity = new RelationalEntity();
@@ -40,4 +40,13 @@ class EntityTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals(['foo'=>true], $entity->getUpdatedFields()); $this->assertEquals(['foo'=>true], $entity->getUpdatedFields());
} }
public function testJsonSerialize() {
$entity = new RelationalEntity();
$entity->setId(123);
$json = [
'id' => 123,
];
$this->assertEquals($json, $entity->jsonSerialize());
}
} }