From f4c3954b065fb01b84e71b522f27169c35c6bfe8 Mon Sep 17 00:00:00 2001 From: netkas Date: Mon, 3 Mar 2025 14:59:37 -0500 Subject: [PATCH] Add encryption channel methods and related classes --- .../Objects/Standard/EncryptionChannel.php | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/Socialbox/Objects/Standard/EncryptionChannel.php diff --git a/src/Socialbox/Objects/Standard/EncryptionChannel.php b/src/Socialbox/Objects/Standard/EncryptionChannel.php new file mode 100644 index 0000000..63e6028 --- /dev/null +++ b/src/Socialbox/Objects/Standard/EncryptionChannel.php @@ -0,0 +1,119 @@ +uuid = $data['uuid']; + $this->status = EncryptionChannelStatus::from($data['status']); + $this->callingPeer = PeerAddress::fromAddress($data['calling_peer']); + $this->callingPublicEncryptionKey = $data['calling_public_encryption_key']; + $this->receivingPeer = PeerAddress::fromAddress($data['receiving_peer']); + $this->receivingPublicEncryptionKey = $data['receiving_public_encryption_key'] ?? null; + + if(is_int($data['created'])) + { + $this->created = $data['created']; + } + if(is_string($data['created'])) + { + try + { + $this->created = (new DateTime($data['created']))->getTimestamp(); + } + catch (DateMalformedStringException $e) + { + throw new InvalidArgumentException($e->getMessage(), $e->getCode()); + } + } + elseif($data['created'] instanceof DateTime) + { + $this->created = $data['created']->getTimestamp(); + } + else + { + throw new InvalidArgumentException('Invalid created type, got: ' . gettype($data['created'])); + } + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getStatus(): EncryptionChannelStatus + { + return $this->status; + } + + public function getCallingPeer(): PeerAddress + { + return $this->callingPeer; + } + + public function getCallingPublicEncryptionKey(): string + { + return $this->callingPublicEncryptionKey; + } + + public function getReceivingPeer(): PeerAddress + { + return $this->receivingPeer; + } + + public function getReceivingPublicEncryptionKey(): ?string + { + return $this->receivingPublicEncryptionKey; + } + + public function getCreated(): int + { + return $this->created; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): EncryptionChannel + { + return new self($data); + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'uuid' => $this->uuid, + 'status' => $this->status->value, + 'calling_peer' => $this->callingPeer->getAddress(), + 'calling_public_encryption_key' => $this->callingPublicEncryptionKey, + 'receiving_peer' => $this->receivingPeer->getAddress(), + 'receiving_public_encryption_key' => $this->receivingPublicEncryptionKey, + 'created' => $this->created + ]; + } + } \ No newline at end of file