Refactored Peer Information to use InformationFields rather than being hard-coded into the peer record

This commit is contained in:
netkas 2025-01-24 15:10:20 -05:00
parent 75de51c910
commit f689e36378
45 changed files with 1422 additions and 1337 deletions

View file

@ -10,7 +10,7 @@
use Socialbox\Exceptions\RequestException;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\Database\RegisteredPeerRecord;
use Socialbox\Objects\Database\PeerRecord;
use Socialbox\Objects\Database\SessionRecord;
class ClientRequest
@ -182,10 +182,10 @@
/**
* Retrieves the associated peer for the current session.
*
* @return RegisteredPeerRecord|null Returns the associated RegisteredPeerRecord if available, or null if no session exists.
* @return PeerRecord|null Returns the associated RegisteredPeerRecord if available, or null if no session exists.
* @throws DatabaseOperationException Thrown if an error occurs while retrieving the peer.
*/
public function getPeer(): ?RegisteredPeerRecord
public function getPeer(): ?PeerRecord
{
$session = $this->getSession();

View file

@ -0,0 +1,105 @@
<?php
namespace Socialbox\Objects\Database;
use Socialbox\Enums\PrivacyState;
use Socialbox\Enums\Types\InformationFieldName;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Standard\InformationField;
use Socialbox\Objects\Standard\InformationFieldState;
class PeerInformationFieldRecord implements SerializableInterface
{
private string $peerUuid;
private InformationFieldName $propertyName;
private string $propertyValue;
private PrivacyState $privacyState;
public function __construct(array $data)
{
$this->peerUuid = $data['peer_uuid'];
$this->propertyName = InformationFieldName::from($data['property_name']);
$this->propertyValue = $data['property_value'];
$this->privacyState = PrivacyState::from($data['privacy_state']);
}
/**
* @return string
*/
public function getPeerUuid(): string
{
return $this->peerUuid;
}
/**
* @return InformationFieldName
*/
public function getPropertyName(): InformationFieldName
{
return $this->propertyName;
}
/**
* @return string
*/
public function getPropertyValue(): string
{
return $this->propertyValue;
}
/**
* @return PrivacyState
*/
public function getPrivacyState(): PrivacyState
{
return $this->privacyState;
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): PeerInformationFieldRecord
{
return new self($data);
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'peer_uuid' => $this->peerUuid,
'property_name' => $this->propertyName->value,
'property_value' => $this->propertyValue,
'privacy_state' => $this->privacyState->value
];
}
/**
* Converts the record to a standard InformationField object
*
* @return InformationField
*/
public function toInformationField(): InformationField
{
return new InformationField([
'name' => $this->propertyName->value,
'value' => $this->propertyValue
]);
}
/**
* Converts the record to a standard InformationFieldState object
*
* @return InformationFieldState
*/
public function toInformationFieldState(): InformationFieldState
{
return new InformationFieldState([
'name' => $this->propertyName->value,
'value' => $this->propertyValue,
'privacy_state' => $this->privacyState->value
]);
}
}

View file

@ -10,16 +10,11 @@
use Socialbox\Objects\Standard\Peer;
use Socialbox\Objects\Standard\SelfUser;
class RegisteredPeerRecord implements SerializableInterface
class PeerRecord implements SerializableInterface
{
private string $uuid;
private string $username;
private string $server;
private ?string $displayName;
private ?string $displayPicture;
private ?string $emailAddress;
private ?string $phoneNumber;
private ?DateTime $birthday;
/**
* @var PeerFlags[]
*/
@ -38,31 +33,6 @@
$this->uuid = $data['uuid'];
$this->username = $data['username'];
$this->server = $data['server'];
$this->displayName = $data['display_name'] ?? null;
$this->displayPicture = $data['display_picture'] ?? null;
$this->emailAddress = $data['email_address'] ?? null;
$this->phoneNumber = $data['phone_number'] ?? null;
if(!isset($data['birthday']))
{
$this->birthday = null;
}
elseif(is_int($data['birthday']))
{
$this->birthday = (new DateTime())->setTimestamp($data['birthday']);
}
elseif(is_string($data['birthday']))
{
$this->birthday = new DateTime($data['birthday']);
}
elseif($data['birthday'] instanceof DateTime)
{
$this->birthday = $data['birthday'];
}
else
{
throw new InvalidArgumentException("The birthday field must be a valid timestamp or date string.");
}
if($data['flags'])
{
@ -150,56 +120,6 @@
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;
}
/**
* Retrieves the display picture.
*
* @return string|null The display picture if set, or null otherwise.
*/
public function getDisplayPicture(): ?string
{
return $this->displayPicture;
}
/**
* Retrieves the email address.
*
* @return string|null The email address if set, or null otherwise.
*/
public function getEmailAddress(): ?string
{
return $this->emailAddress;
}
/**
* Retrieves the phone number.
*
* @return string|null The phone number if set, or null otherwise.
*/
public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
/**
* Retrieves the birthday.
*
* @return DateTime|null The birthday if set, or null otherwise.
*/
public function getBirthday(): ?DateTime
{
return $this->birthday;
}
/**
* Retrieves the flags.
*
@ -292,9 +212,9 @@
*/
public function toStandardPeer(): Peer
{
// TODO: TO be updated
return Peer::fromArray([
'address' => $this->getAddress(),
'display_name' => $this->displayName,
'flags' => array_map(fn(PeerFlags $flag) => $flag->value, $this->flags),
'registered' => $this->created->getTimestamp()
]);
@ -317,11 +237,6 @@
'uuid' => $this->uuid,
'username' => $this->username,
'server' => $this->server,
'display_name' => $this->displayName,
'display_picture' => $this->displayPicture,
'email_address' => $this->emailAddress,
'phone_number' => $this->phoneNumber,
'birthday' => $this->birthday?->getTimestamp(),
'flags' => PeerFlags::toString($this->flags),
'enabled' => $this->enabled,
'created' => $this->created,

View file

@ -0,0 +1,53 @@
<?php
namespace Socialbox\Objects\Standard;
use Socialbox\Enums\Types\InformationFieldName;
use Socialbox\Interfaces\SerializableInterface;
class InformationField implements SerializableInterface
{
private InformationFieldName $name;
private string $value;
public function __construct(array $data)
{
$this->name = InformationFieldName::from($data['name']);
$this->value = $data['value'];
}
/**
* @return InformationFieldName
*/
public function getName(): InformationFieldName
{
return $this->name;
}
/**
* @return string
*/
public function getValue(): string
{
return $this->value;
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): InformationField
{
return new self($data);
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'name' => $this->name->getValue(),
'value' => $this->value,
];
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace Socialbox\Objects\Standard;
use Socialbox\Enums\PrivacyState;
use Socialbox\Enums\Types\InformationFieldName;
use Socialbox\Interfaces\SerializableInterface;
class InformationFieldState implements SerializableInterface
{
private InformationFieldName $name;
private string $value;
private PrivacyState $privacyState;
public function __construct(array $data)
{
$this->name = InformationFieldName::from($data['name']);
$this->value = $data['value'];
$this->privacyState = PrivacyState::from($data['privacy_state']);
}
/**
* @return InformationFieldName
*/
public function getName(): InformationFieldName
{
return $this->name;
}
/**
* @return string
*/
public function getValue(): string
{
return $this->value;
}
public function getPrivacyState(): PrivacyState
{
return $this->privacyState;
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): InformationFieldState
{
return new self($data);
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'name' => $this->name->value,
'value' => $this->value,
'privacy_state' => $this->privacyState->value
];
}
}

View file

@ -5,7 +5,7 @@ namespace Socialbox\Objects\Standard;
use DateTime;
use Socialbox\Enums\Flags\PeerFlags;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Database\RegisteredPeerRecord;
use Socialbox\Objects\Database\PeerRecord;
class SelfUser implements SerializableInterface
{
@ -23,11 +23,11 @@ class SelfUser implements SerializableInterface
/**
* Constructor for initializing the object with provided data.
*
* @param array|RegisteredPeerRecord $data Data array containing initial values for object properties.
* @param array|PeerRecord $data Data array containing initial values for object properties.
*/
public function __construct(array|RegisteredPeerRecord $data)
public function __construct(array|PeerRecord $data)
{
if($data instanceof RegisteredPeerRecord)
if($data instanceof PeerRecord)
{
$this->uuid = $data->getUuid();
$this->enabled = $data->isEnabled();