Add Peer class and conversion method in RegisteredPeerRecord

This commit is contained in:
netkas 2025-01-03 18:30:40 -05:00
parent a3976742d6
commit d732c89632
2 changed files with 119 additions and 0 deletions

View file

@ -6,6 +6,7 @@
use Socialbox\Classes\Configuration; use Socialbox\Classes\Configuration;
use Socialbox\Enums\Flags\PeerFlags; use Socialbox\Enums\Flags\PeerFlags;
use Socialbox\Interfaces\SerializableInterface; use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Standard\Peer;
use Socialbox\Objects\Standard\SelfUser; use Socialbox\Objects\Standard\SelfUser;
class RegisteredPeerRecord implements SerializableInterface class RegisteredPeerRecord implements SerializableInterface
@ -191,6 +192,16 @@
return new SelfUser($this); 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 * @inheritDoc
*/ */

View file

@ -0,0 +1,108 @@
<?php
namespace Socialbox\Objects\Standard;
use InvalidArgumentException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\PeerAddress;
class Peer implements SerializableInterface
{
private PeerAddress $address;
private string $displayName;
private array $flags;
private int $registered;
/**
* Constructor method.
*
* @param array $data An associative array containing the keys:
* - 'address': A string or an instance of PeerAddress. Used to set the address property.
* - 'display_name': The display name for the entity.
* - 'flags': Flags associated with the entity.
* - 'registered': Registration status or date for the entity.
*
* @return void
* @throws InvalidArgumentException If the 'address' value is neither a string nor an instance of PeerAddress.
*/
public function __construct(array $data)
{
if(is_string($data['address']))
{
$this->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
];
}
}