From d732c89632ad42cfe91702a8e36f3a6ef0c35a3f Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 3 Jan 2025 18:30:40 -0500 Subject: [PATCH] Add `Peer` class and conversion method in `RegisteredPeerRecord` --- .../Objects/Database/RegisteredPeerRecord.php | 11 ++ src/Socialbox/Objects/Standard/Peer.php | 108 ++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/Socialbox/Objects/Standard/Peer.php diff --git a/src/Socialbox/Objects/Database/RegisteredPeerRecord.php b/src/Socialbox/Objects/Database/RegisteredPeerRecord.php index 7d805bb..4f6171e 100644 --- a/src/Socialbox/Objects/Database/RegisteredPeerRecord.php +++ b/src/Socialbox/Objects/Database/RegisteredPeerRecord.php @@ -6,6 +6,7 @@ use Socialbox\Classes\Configuration; use Socialbox\Enums\Flags\PeerFlags; use Socialbox\Interfaces\SerializableInterface; + use Socialbox\Objects\Standard\Peer; use Socialbox\Objects\Standard\SelfUser; class RegisteredPeerRecord implements SerializableInterface @@ -191,6 +192,16 @@ return new SelfUser($this); } + /** + * Converts the current instance to a Peer object. + * + * @return Peer The Peer representation of the current instance. + */ + public function toPeer(): Peer + { + return Peer::fromArray($this->toArray()); + } + /** * @inheritDoc */ diff --git a/src/Socialbox/Objects/Standard/Peer.php b/src/Socialbox/Objects/Standard/Peer.php new file mode 100644 index 0000000..16b3c02 --- /dev/null +++ b/src/Socialbox/Objects/Standard/Peer.php @@ -0,0 +1,108 @@ +address = PeerAddress::fromAddress($data['address']); + } + elseif($data['address'] instanceof PeerAddress) + { + $this->address = $data['address']; + } + else + { + throw new InvalidArgumentException('Invalid address value'); + } + + $this->displayName = $data['display_name']; + $this->flags = $data['flags']; + $this->registered = $data['registered']; + } + + /** + * Retrieves the address property. + * + * @return PeerAddress The address associated with the instance. + */ + public function getAddress(): PeerAddress + { + return $this->address; + } + + /** + * Retrieves the display name of the entity. + * + * @return string The display name associated with the entity. + */ + public function getDisplayName(): string + { + return $this->displayName; + } + + /** + * Retrieves the flags associated with the entity. + * + * @return array An array containing the flags. + */ + public function getFlags(): array + { + return $this->flags; + } + + /** + * Retrieves the registered value. + * + * @return int The registered property value. + */ + public function getRegistered(): int + { + return $this->registered; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): Peer + { + return new self($data); + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'address' => $this->address->getAddress(), + 'display_name' => $this->displayName, + 'flags' => $this->flags, + 'registered' => $this->registered + ]; + } + } \ No newline at end of file