From 436022dcbd622fe1b06bb4aea31f342600d33dfd Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 30 Oct 2024 15:28:44 -0400 Subject: [PATCH] Add SelfUser class to handle user-related operations --- src/Socialbox/Objects/Standard/SelfUser.php | 152 ++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/Socialbox/Objects/Standard/SelfUser.php diff --git a/src/Socialbox/Objects/Standard/SelfUser.php b/src/Socialbox/Objects/Standard/SelfUser.php new file mode 100644 index 0000000..109c21e --- /dev/null +++ b/src/Socialbox/Objects/Standard/SelfUser.php @@ -0,0 +1,152 @@ +uuid = $data->getUuid(); + $this->username = $data->getUsername(); + $this->address = + $this->displayName = $data->getDisplayName(); + $this->flags = $data->getFlags(); + $this->created = $data->getCreated()->getTimestamp(); + + return; + } + + $this->uuid = $data['uuid']; + $this->username = $data['username']; + $this->displayName = $data['display_name'] ?? null; + + if(is_string($data['flags'])) + { + $this->flags = PeerFlags::fromString($data['flags']); + } + elseif(is_array($data['flags'])) + { + $this->flags = $data['flags']; + } + else + { + $this->flags = []; + } + + if($data['created'] instanceof DateTime) + { + $this->created = $data['created']->getTimestamp(); + } + else + { + $this->created = $data['created']; + } + + return; + } + + /** + * Retrieves the UUID of the object. + * + * @return string The UUID of the object. + */ + public function getUuid(): string + { + return $this->uuid; + } + + /** + * + * @return string The username of the user. + */ + public function getUsername(): string + { + return $this->username; + } + + /** + * + * @return string|null The display name. + */ + public function getDisplayName(): ?string + { + return $this->displayName; + } + + /** + * + * @return array + */ + public function getFlags(): array + { + return $this->flags; + } + + /** + * + * @return bool + */ + public function isEnabled(): bool + { + return $this->enabled; + } + + /** + * + * @return int The timestamp when the object was created. + */ + public function getCreated(): int + { + return $this->created; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): SelfUser + { + return new self($data); + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + $flags = []; + foreach($this->flags as $flag) + { + $flags[] = $flag->value; + } + + return [ + 'uuid' => $this->uuid, + 'username' => $this->username, + 'display_name' => $this->displayName, + 'flags' => $flags, + 'created' => $this->created + ]; + } +} \ No newline at end of file