diff --git a/.idea/socialbox-php.iml b/.idea/socialbox-php.iml index 9ac0b9d..c5da166 100644 --- a/.idea/socialbox-php.iml +++ b/.idea/socialbox-php.iml @@ -2,6 +2,7 @@ + diff --git a/src/Socialbox/Objects/Database/RegisteredPeerRecord.php b/src/Socialbox/Objects/Database/RegisteredPeerRecord.php index acb864a..2226b7f 100644 --- a/src/Socialbox/Objects/Database/RegisteredPeerRecord.php +++ b/src/Socialbox/Objects/Database/RegisteredPeerRecord.php @@ -1,168 +1,189 @@ uuid = $data['uuid']; - $this->username = $data['username']; - $this->displayName = $data['display_name'] ?? null; + private string $uuid; + private string $username; + private string $server; + private ?string $displayName; + /** + * @var PeerFlags[] + */ + private ?array $flags; + private bool $enabled; + private DateTime $created; - if($data['flags']) + /** + * Constructor for initializing class properties from provided data. + * + * @param array $data Array containing initialization data. + * @throws \DateMalformedStringException + */ + public function __construct(array $data) { - $this->flags = PeerFlags::fromString($data['flags']); - } - else - { - $this->flags = []; + $this->uuid = $data['uuid']; + $this->username = $data['username']; + $this->server = $data['server']; + $this->displayName = $data['display_name'] ?? null; + + if($data['flags']) + { + $this->flags = PeerFlags::fromString($data['flags']); + } + else + { + $this->flags = []; + } + + $this->enabled = $data['enabled']; + + if (is_string($data['created'])) + { + $this->created = new DateTime($data['created']); + } + else + { + $this->created = $data['created']; + } } - $this->enabled = $data['enabled']; - - if (is_string($data['created'])) + /** + * Retrieves the UUID of the current instance. + * + * @return string The UUID. + */ + public function getUuid(): string { - $this->created = new DateTime($data['created']); + return $this->uuid; } - else + + /** + * Retrieves the username. + * + * @return string The username. + */ + public function getUsername(): string { - $this->created = $data['created']; + return $this->username; } - } - /** - * Retrieves the UUID of the current instance. - * - * @return string The UUID. - */ - public function getUuid(): string - { - return $this->uuid; - } - - /** - * Retrieves the username. - * - * @return string The username. - */ - public function getUsername(): string - { - return $this->username; - } - - /** - * Constructs and retrieves the peer address using the current instance's username and the domain from the configuration. - * - * @return string The constructed peer address. - */ - public function getAddress(): string - { - return sprintf("%s@%s", $this->username, Configuration::getInstanceConfiguration()->getDomain()); - } - - /** - * Retrieves the display name. - * - * @return string|null The display name if set, or null otherwise. - */ - public function getDisplayName(): ?string - { - return $this->displayName; - } - - public function getFlags(): array - { - return $this->flags; - } - - public function flagExists(PeerFlags $flag): bool - { - return in_array($flag, $this->flags, true); - } - - public function removeFlag(PeerFlags $flag): void - { - $key = array_search($flag, $this->flags, true); - if($key !== false) + /** + * Retrieves the server. + * + * @return string The server. + */ + public function getServer(): string { - unset($this->flags[$key]); + return $this->server; } - } - /** - * Checks if the current instance is enabled. - * - * @return bool True if enabled, false otherwise. - */ - public function isEnabled(): bool - { - return $this->enabled; - } + /** + * Constructs and retrieves the peer address using the current instance's username and the domain from the configuration. + * + * @return string The constructed peer address. + */ + public function getAddress(): string + { + return sprintf("%s@%s", $this->username, Configuration::getInstanceConfiguration()->getDomain()); + } - /** - * Retrieves the creation date and time. - * - * @return DateTime The creation date and time. - */ - public function getCreated(): DateTime - { - return $this->created; - } + /** + * Retrieves the display name. + * + * @return string|null The display name if set, or null otherwise. + */ + public function getDisplayName(): ?string + { + return $this->displayName; + } - /** - * Converts the current instance to a SelfUser object. - * - * @return SelfUser The SelfUser object. - */ - public function toSelfUser(): SelfUser - { - return new SelfUser($this); - } + public function getFlags(): array + { + return $this->flags; + } - /** - * @inheritDoc - */ - public static function fromArray(array $data): object - { - return new self($data); - } + public function flagExists(PeerFlags $flag): bool + { + return in_array($flag, $this->flags, true); + } - /** - * @inheritDoc - */ - public function toArray(): array - { - return [ - 'uuid' => $this->uuid, - 'username' => $this->username, - 'display_name' => $this->displayName, - 'flags' => PeerFlags::toString($this->flags), - 'enabled' => $this->enabled, - 'created' => $this->created - ]; - } -} \ No newline at end of file + public function removeFlag(PeerFlags $flag): void + { + $key = array_search($flag, $this->flags, true); + if($key !== false) + { + unset($this->flags[$key]); + } + } + + /** + * Checks if the current instance is enabled. + * + * @return bool True if enabled, false otherwise. + */ + public function isEnabled(): bool + { + return $this->enabled; + } + + /** + * Retrieves the creation date and time. + * + * @return DateTime The creation date and time. + */ + public function getCreated(): DateTime + { + return $this->created; + } + + /** + * Determines if the server is external. + * + * @return bool True if the server is external, false otherwise. + */ + public function isExternal(): bool + { + return $this->server === 'host'; + } + + /** + * Converts the current instance to a SelfUser object. + * + * @return SelfUser The SelfUser object. + */ + public function toSelfUser(): SelfUser + { + return new SelfUser($this); + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): object + { + return new self($data); + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'uuid' => $this->uuid, + 'username' => $this->username, + 'display_name' => $this->displayName, + 'flags' => PeerFlags::toString($this->flags), + 'enabled' => $this->enabled, + 'created' => $this->created + ]; + } + } \ No newline at end of file