forked from GNUsocial/gnu-social
[ENTITY] Add JsonSerializable interface to Entity base class and implement it for the Note class
This commit is contained in:
parent
8e627f2c18
commit
66b39d3607
@ -23,13 +23,14 @@ namespace App\Core;
|
|||||||
|
|
||||||
use App\Core\DB\DB;
|
use App\Core\DB\DB;
|
||||||
use App\Util\Exception\NotFoundException;
|
use App\Util\Exception\NotFoundException;
|
||||||
|
use App\Util\Exception\ServerException;
|
||||||
use App\Util\Formatting;
|
use App\Util\Formatting;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class to all entities, with some utilities
|
* Base class to all entities, with some utilities
|
||||||
*/
|
*/
|
||||||
abstract class Entity
|
abstract class Entity implements \JsonSerializable
|
||||||
{
|
{
|
||||||
public function __call(string $name , array $arguments): mixed
|
public function __call(string $name , array $arguments): mixed
|
||||||
{
|
{
|
||||||
@ -86,8 +87,10 @@ abstract class Entity
|
|||||||
$obj = DB::findOneBy($table, $find_by);
|
$obj = DB::findOneBy($table, $find_by);
|
||||||
} catch (NotFoundException) {
|
} catch (NotFoundException) {
|
||||||
$obj = null;
|
$obj = null;
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::unexpected_exception($e);
|
Log::unexpected_exception($e);
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
$is_update = $obj !== null;
|
$is_update = $obj !== null;
|
||||||
return [self::create($args, $obj), $is_update];
|
return [self::create($args, $obj), $is_update];
|
||||||
@ -119,4 +122,17 @@ abstract class Entity
|
|||||||
return null;
|
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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -262,6 +262,14 @@ class Note extends Entity
|
|||||||
[], ['id' => $this->id]));
|
[], ['id' => $this->id]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'content' => $this->getContent(),
|
||||||
|
'author' => $this->getGSActorId(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public static function schemaDef(): array
|
public static function schemaDef(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
Loading…
Reference in New Issue
Block a user