Updated method AddressBookGetContact to return a full standard contact, improved standard contact object

This commit is contained in:
netkas 2025-01-30 19:51:37 -05:00
parent 4f1f0ce151
commit 83a47ddd5e
5 changed files with 333 additions and 6 deletions

View file

@ -2,6 +2,7 @@
namespace Socialbox\Objects\Standard;
use InvalidArgumentException;
use Socialbox\Enums\Types\ContactRelationshipType;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\PeerAddress;
@ -9,7 +10,12 @@
class ContactRecord implements SerializableInterface
{
private PeerAddress $address;
private ?Peer $peer;
private ContactRelationshipType $relationship;
/**
* @var KnownSigningKey[]
*/
private array $knownKeys;
private int $addedTimestamp;
/**
@ -20,7 +26,53 @@
public function __construct(array $data)
{
$this->address = PeerAddress::fromAddress($data['address']);
$this->relationship = ContactRelationshipType::tryFrom($data['relationship']) ?? ContactRelationshipType::MUTUAL;
if(is_array($data['peer']))
{
$this->peer = Peer::fromArray($data['peer']);
}
elseif($data['peer'] instanceof Peer)
{
$this->peer = $data['peer'];
}
elseif(is_null($data['peer']))
{
$this->peer = null;
}
else
{
throw new InvalidArgumentException('Invalid peer data');
}
if($data['relationship'] instanceof ContactRelationshipType)
{
$this->relationship = $data['relationship'];
}
elseif(is_string($data['relationship']))
{
$this->relationship = ContactRelationshipType::tryFrom($data['relationship']) ?? ContactRelationshipType::MUTUAL;
}
else
{
throw new InvalidArgumentException('Invalid relationship data');
}
$this->knownKeys = [];
foreach($data['known_keys'] as $key)
{
if(is_array($key))
{
$this->knownKeys[] = KnownSigningKey::fromArray($key);
}
elseif($key instanceof KnownSigningKey)
{
$this->knownKeys[] = $key;
}
else
{
throw new InvalidArgumentException('Invalid known key data');
}
}
$this->addedTimestamp = $data['added_timestamp'];
}
@ -34,6 +86,16 @@
return $this->address;
}
/**
* Retrieves the peer of the contact.
*
* @return Peer|null Returns the peer of the contact. If the peer is not known, null is returned.
*/
public function getPeer(): ?Peer
{
return $this->peer;
}
/**
* Retrieves the relationship of the contact.
*
@ -44,6 +106,16 @@
return $this->relationship;
}
/**
* Retrieves the known keys of the contact.
*
* @return KnownSigningKey[] Returns the known keys of the contact.
*/
public function getKnownKeys(): array
{
return $this->knownKeys;
}
/**
* Retrieves the timestamp when the contact was added.
*
@ -57,7 +129,7 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): object
public static function fromArray(array $data): ContactRecord
{
return new self($data);
}
@ -69,7 +141,9 @@
{
return [
'address' => $this->address->getAddress(),
'peer' => $this->peer?->toArray(),
'relationship' => $this->relationship->value,
'known_keys' => array_map(function($key) {return $key->toArray();}, $this->knownKeys),
'added_timestamp' => $this->addedTimestamp
];
}