Added SerializableInterface.php & made Entity.php implement this new interface
This commit is contained in:
parent
ef62893f21
commit
393be1a6c0
2 changed files with 50 additions and 1 deletions
|
@ -3,8 +3,9 @@
|
||||||
namespace Socialbox\Abstracts;
|
namespace Socialbox\Abstracts;
|
||||||
|
|
||||||
use Socialbox\Enums\EntityType;
|
use Socialbox\Enums\EntityType;
|
||||||
|
use Socialbox\Interfaces\SerializableInterface;
|
||||||
|
|
||||||
abstract class Entity
|
abstract class Entity implements SerializableInterface
|
||||||
{
|
{
|
||||||
protected string $uuid;
|
protected string $uuid;
|
||||||
protected EntityType $type;
|
protected EntityType $type;
|
||||||
|
@ -85,4 +86,30 @@ abstract class Entity
|
||||||
{
|
{
|
||||||
return sprintf('%s@%s', $this->username, $this->domain);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
22
src/Socialbox/Interfaces/SerializableInterface.php
Normal file
22
src/Socialbox/Interfaces/SerializableInterface.php
Normal 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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue