Added SerializableInterface.php & made Entity.php implement this new interface

This commit is contained in:
netkas 2024-08-31 16:55:13 -04:00
parent ef62893f21
commit 393be1a6c0
2 changed files with 50 additions and 1 deletions

View file

@ -3,8 +3,9 @@
namespace Socialbox\Abstracts;
use Socialbox\Enums\EntityType;
use Socialbox\Interfaces\SerializableInterface;
abstract class Entity
abstract class Entity implements SerializableInterface
{
protected string $uuid;
protected EntityType $type;
@ -85,4 +86,30 @@ abstract class Entity
{
return sprintf('%s@%s', $this->username, $this->domain);
}
/**
* Serializes the entity to an array.
*
* @return array
*/
public function toArray(): array
{
return [
'uuid' => $this->uuid,
'type' => $this->type,
'username' => $this->username,
'domain' => $this->domain,
'display_name' => $this->display_name,
];
}
/**
* Constructs the entity object from an array of data.
*
* @param array $data The data to construct the entity from.
*/
public static function fromArray(array $data): Entity
{
return new static($data);
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace Socialbox\Interfaces;
use Socialbox\Abstracts\Entity;
interface SerializableInterface
{
/**
* Serializes the object to an array.
*
* @return array
*/
public function toArray(): array;
/**
* Constructs the object from an array of data.
*
* @param array $data The data to construct the object from.
*/
public static function fromArray(array $data): Entity;
}