[ENTITY] Add JsonSerializable interface to Entity base class and implement it for the Note class

This commit is contained in:
Hugo Sales 2021-08-08 00:39:39 +00:00 committed by Diogo Peralta Cordeiro
parent 8e627f2c18
commit 66b39d3607
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 25 additions and 1 deletions

View File

@ -23,13 +23,14 @@ namespace App\Core;
use App\Core\DB\DB;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\ServerException;
use App\Util\Formatting;
use DateTime;
/**
* Base class to all entities, with some utilities
*/
abstract class Entity
abstract class Entity implements \JsonSerializable
{
public function __call(string $name , array $arguments): mixed
{
@ -86,8 +87,10 @@ abstract class Entity
$obj = DB::findOneBy($table, $find_by);
} catch (NotFoundException) {
$obj = null;
// @codeCoverageIgnoreStart
} catch (\Exception $e) {
Log::unexpected_exception($e);
// @codeCoverageIgnoreEnd
}
$is_update = $obj !== null;
return [self::create($args, $obj), $is_update];
@ -119,4 +122,17 @@ abstract class Entity
return null;
}
}
/**
* Called when json_encode encounters this object. Not all
* entities will need json encoding, so it doesn't make sense to
* make this abstract
*
* @throw ServerException
* @codeCoverageIgnore
*/
public function jsonSerialize()
{
throw new ServerException(_m('Unimplemented method'));
}
}

View File

@ -262,6 +262,14 @@ class Note extends Entity
[], ['id' => $this->id]));
}
public function jsonSerialize()
{
return [
'content' => $this->getContent(),
'author' => $this->getGSActorId(),
];
}
public static function schemaDef(): array
{
return [