Skip to content

Commit

Permalink
New Messages API Features (#465)
Browse files Browse the repository at this point in the history
* New fields added

* Last of the changes for messages
  • Loading branch information
SecondeJK authored Jan 25, 2024
1 parent c412306 commit 2b0306e
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 22 deletions.
39 changes: 39 additions & 0 deletions src/Messages/Channel/BaseMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ abstract class BaseMessage implements Message
protected string $from;
protected string $channel;
protected ?string $clientRef = null;
protected ?string $webhookUrl = null;
protected ?string $webhookVersion = null;

protected array $permittedVersions = [
'v0.1',
'v1'
];

public const MESSAGES_SUBTYPE_TEXT = 'text';
public const MESSAGES_SUBTYPE_IMAGE = 'image';
Expand Down Expand Up @@ -60,6 +67,30 @@ public function setTo(string $to): void
$this->to = $to;
}

public function setWebhookUrl(string $url): void
{
$this->webhookUrl = $url;
}

public function getWebhookUrl(): ?string
{
return $this->webhookUrl;
}

public function setWebhookVersion(string $version): void
{
if (! in_array($version, $this->permittedVersions, true)) {
throw new \InvalidArgumentException($version . ' is not a valid webhook version');
}

$this->webhookVersion = $version;
}

public function getWebhookVersion(): ?string
{
return $this->webhookVersion;
}

public function getBaseMessageUniversalOutputArray(): array
{
$returnArray = [
Expand All @@ -73,6 +104,14 @@ public function getBaseMessageUniversalOutputArray(): array
$returnArray['client_ref'] = $this->getClientRef();
}

if ($this->getWebhookUrl()) {
$returnArray['webhook_url'] = $this->getWebhookUrl();
}

if ($this->getWebhookVersion()) {
$returnArray['webhook_version'] = $this->getWebhookVersion();
}

return $returnArray;
}
}
4 changes: 4 additions & 0 deletions src/Messages/Channel/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function getClientRef(): ?string;
public function getChannel(): string;
public function getSubType(): string;
public function setClientRef(string $clientRef): void;
public function getWebhookUrl(): ?string;
public function setWebhookUrl(string $url): void;
public function getWebhookVersion(): ?string;
public function setWebhookVersion(string $version): void;

/**
* All message types have shared outputs required by the endpoint.
Expand Down
70 changes: 70 additions & 0 deletions src/Messages/Channel/SMS/SMSText.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ class SMSText extends BaseMessage
{
use TextTrait;

protected array $permittedEncodingTypes = [
'text',
'unicode',
'auto'
];

protected string $subType = BaseMessage::MESSAGES_SUBTYPE_TEXT;
protected string $channel = 'sms';
protected ?int $ttl = null;
protected ?string $encodingType = null;
protected ?string $contentId = null;
protected ?string $entityId = null;

public function __construct(
string $to,
Expand All @@ -22,11 +32,71 @@ public function __construct(
$this->text = $message;
}

public function getEncodingType(): ?string
{
return $this->encodingType;
}

public function setEncodingType(?string $encodingType): void
{
if (! in_array($encodingType, $this->permittedEncodingTypes, true)) {
throw new \InvalidArgumentException($encodingType . ' is not a valid encoding type');
}

$this->encodingType = $encodingType;
}

public function getContentId(): ?string
{
return $this->contentId;
}

public function setContentId(?string $contentId): void
{
$this->contentId = $contentId;
}

public function getEntityId(): ?string
{
return $this->entityId;
}

public function setEntityId(?string $entityId): void
{
$this->entityId = $entityId;
}

public function getTtl(): ?int
{
return $this->ttl;
}

public function setTtl(?int $ttl): void
{
$this->ttl = $ttl;
}

public function toArray(): array
{
$returnArray = $this->getBaseMessageUniversalOutputArray();
$returnArray['text'] = $this->getText();

if ($this->getEncodingType()) {
$returnArray['sms']['encoding_type'] = $this->getEncodingType();
}

if ($this->getContentId()) {
$returnArray['sms']['content_id'] = $this->getContentId();
}

if ($this->getEntityId()) {
$returnArray['sms']['entity_id'] = $this->getEntityId();
}

if ($this->getTtl()) {
$returnArray['ttl'] = $this->getTtl();
}

return $returnArray;
}
}
9 changes: 5 additions & 4 deletions src/Messages/Channel/Viber/ViberFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Vonage\Messages\MessageObjects\FileObject;
use Vonage\Messages\Channel\BaseMessage;

class ViberFile extends BaseMessage
{
use ViberServiceObjectTrait;
Expand All @@ -26,11 +27,11 @@ public function toArray(): array
$returnArray['file'] = $this->fileObject->toArray();

if ($this->requiresViberServiceObject()) {
$this->getCategory() ? $returnArray['viber_service']['category'] = $this->getCategory(): null;
$this->getTtl() ? $returnArray['viber_service']['ttl'] = $this->getTtl(): null;
$this->getType() ? $returnArray['viber_service']['type'] = $this->getType(): null;
$this->getCategory() ? $returnArray['viber_service']['category'] = $this->getCategory() : null;
$this->getTtl() ? $returnArray['viber_service']['ttl'] = $this->getTtl() : null;
$this->getType() ? $returnArray['viber_service']['type'] = $this->getType() : null;
}

return $returnArray;
}
}
}
22 changes: 22 additions & 0 deletions src/Messages/Channel/Viber/ViberServiceObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ trait ViberServiceObjectTrait
private ?int $ttl = null;
private ?string $type = null;
private ?ViberActionObject $action = null;
private string $duration;
private string $fileSize;

public function requiresViberServiceObject(): bool
{
Expand Down Expand Up @@ -63,4 +65,24 @@ public function setAction(ViberActionObject $viberActionObject): static

return $this;
}

public function getDuration(): string
{
return $this->duration;
}

public function setDuration(string $duration): void
{
$this->duration = $duration;
}

public function getFileSize(): string
{
return $this->fileSize;
}

public function setFileSize(string $fileSize): void
{
$this->fileSize = $fileSize;
}
}
14 changes: 13 additions & 1 deletion src/Messages/Channel/Viber/ViberVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public function __construct(
string $to,
string $from,
string $thumbUrl,
protected VideoObject $videoObject
protected VideoObject $videoObject,
string $duration,
string $fileSize
) {
$this->fileSize = $fileSize;
$this->duration = $duration;
$this->to = $to;
$this->from = $from;
$this->thumbUrl = $thumbUrl;
Expand All @@ -33,6 +37,14 @@ public function toArray(): array
$videoArray['thumb_url'] = $this->thumbUrl;
$returnArray['video'] = $videoArray;

$returnArray['viber_service']['duration'] = $this->getDuration();
$returnArray['viber_service']['file_size'] = $this->getFileSize();

$this->getCategory() ? $returnArray['viber_service']['category'] = $this->getCategory() : null;
$this->getTtl() ? $returnArray['viber_service']['ttl'] = $this->getTtl() : null;
$this->getType() ? $returnArray['viber_service']['type'] = $this->getType() : null;
$this->getAction() ? $returnArray['viber_service']['action'] = $this->getAction()->toArray() : null;

return $returnArray;
}
}
29 changes: 25 additions & 4 deletions src/Messages/MessageObjects/FileObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

class FileObject implements ArrayHydrateInterface
{
public function __construct(private string $url, private string $caption = '')
{
public function __construct(
private string $url,
private string $caption = '',
private ?string $name = null,
) {
}

public function fromArray(array $data): FileObject
Expand All @@ -18,6 +21,10 @@ public function fromArray(array $data): FileObject
$this->caption = $data['caption'];
}

if (isset($data['name'])) {
$this->name = $data['name'];
}

return $this;
}

Expand All @@ -27,8 +34,12 @@ public function toArray(): array
'url' => $this->url
];

if ($this->caption) {
$returnArray['caption'] = $this->caption;
if ($this->getCaption()) {
$returnArray['caption'] = $this->getCaption();
}

if ($this->getName()) {
$returnArray['name'] = $this->getName();
}

return $returnArray;
Expand All @@ -43,4 +54,14 @@ public function getCaption(): string
{
return $this->caption;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): void
{
$this->name = $name;
}
}
10 changes: 6 additions & 4 deletions src/Messages/MessageObjects/ImageObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

class ImageObject implements ArrayHydrateInterface
{
public function __construct(private string $url, private string $caption = '')
{
public function __construct(
private string $url,
private string $caption = '',
) {
}

public function fromArray(array $data): ImageObject
Expand All @@ -27,8 +29,8 @@ public function toArray(): array
'url' => $this->url
];

if ($this->caption) {
$returnArray['caption'] = $this->caption;
if ($this->getCaption()) {
$returnArray['caption'] = $this->getCaption();
}

return $returnArray;
Expand Down
16 changes: 14 additions & 2 deletions src/Messages/MessageObjects/VCardObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

class VCardObject implements ArrayHydrateInterface
{
public function __construct(private string $url)
{
public function __construct(
private string $url,
private ?string $caption = null
) {
}

public function fromArray(array $data): VCardObject
Expand All @@ -28,4 +30,14 @@ public function getUrl(): string
{
return $this->url;
}

public function getCaption(): ?string
{
return $this->caption;
}

public function setCaption(?string $caption): void
{
$this->caption = $caption;
}
}
4 changes: 2 additions & 2 deletions src/Messages/MessageObjects/VideoObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function toArray(): array
'url' => $this->url
];

if ($this->caption) {
$returnArray['caption'] = $this->caption;
if ($this->getCaption()) {
$returnArray['caption'] = $this->getCaption();
}

return $returnArray;
Expand Down
Loading

0 comments on commit 2b0306e

Please sign in to comment.