From 393be1a6c0f99157e6cac2289084d7c282e36543 Mon Sep 17 00:00:00 2001 From: netkas Date: Sat, 31 Aug 2024 16:55:13 -0400 Subject: [PATCH] Added SerializableInterface.php & made Entity.php implement this new interface --- src/Socialbox/Abstracts/Entity.php | 29 ++++++++++++++++++- .../Interfaces/SerializableInterface.php | 22 ++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/Socialbox/Interfaces/SerializableInterface.php diff --git a/src/Socialbox/Abstracts/Entity.php b/src/Socialbox/Abstracts/Entity.php index 129ac00..03dbab7 100644 --- a/src/Socialbox/Abstracts/Entity.php +++ b/src/Socialbox/Abstracts/Entity.php @@ -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); + } } \ No newline at end of file diff --git a/src/Socialbox/Interfaces/SerializableInterface.php b/src/Socialbox/Interfaces/SerializableInterface.php new file mode 100644 index 0000000..3f73ab5 --- /dev/null +++ b/src/Socialbox/Interfaces/SerializableInterface.php @@ -0,0 +1,22 @@ +