Skip to content

Commit

Permalink
New Message Types (#388)
Browse files Browse the repository at this point in the history
* PHP8: remove frivolous docblocks

* Added two new files types for viber

* Added new whatsapp sticker type

* Tests for ViberVideo

* dynamic property
  • Loading branch information
SecondeJK authored Mar 22, 2023
1 parent 180c7f4 commit ea91ad0
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/Messages/Channel/BaseMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ abstract class BaseMessage implements Message
public const MESSAGES_SUBTYPE_VIDEO = 'video';
public const MESSAGES_SUBTYPE_FILE = 'file';
public const MESSAGES_SUBTYPE_TEMPLATE = 'template';
public const MESSAGES_SUBTYPE_STICKER = 'sticker';
public const MESSAGES_SUBTYPE_CUSTOM = 'custom';

public function getClientRef(): ?string
Expand Down
38 changes: 38 additions & 0 deletions src/Messages/Channel/Viber/MessageObjects/ViberActionObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Vonage\Messages\Channel\Viber\MessageObjects;

use Vonage\Entity\Hydrator\ArrayHydrateInterface;

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

public function fromArray(array $data): ViberActionObject
{
$this->url = $data['url'];
$this->text = $data['text'];

return $this;
}

public function toArray(): array
{
return [
'url' => $this->getUrl(),
'text' => $this->getText()
];
}

public function getUrl(): string
{
return $this->url;
}

public function getText(): string
{
return $this->text;
}
}
36 changes: 36 additions & 0 deletions src/Messages/Channel/Viber/ViberFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Vonage\Messages\Channel\Viber;

use Vonage\Messages\MessageObjects\FileObject;
use Vonage\Messages\Channel\BaseMessage;
class ViberFile extends BaseMessage
{
use ViberServiceObjectTrait;

protected string $channel = 'viber_service';
protected string $subType = BaseMessage::MESSAGES_SUBTYPE_FILE;

public function __construct(
string $to,
string $from,
protected FileObject $fileObject
) {
$this->to = $to;
$this->from = $from;
}

public function toArray(): array
{
$returnArray = $this->getBaseMessageUniversalOutputArray();
$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;
}

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

namespace Vonage\Messages\Channel\Viber;

use Vonage\Messages\Channel\Viber\MessageObjects\ViberActionObject;
use Vonage\Messages\MessageObjects\ImageObject;
use Vonage\Messages\Channel\BaseMessage;

Expand All @@ -18,13 +19,15 @@ public function __construct(
protected ImageObject $image,
?string $category = null,
?int $ttl = null,
?string $type = null
?string $type = null,
?ViberActionObject $viberActionObject = null
) {
$this->to = $to;
$this->from = $from;
$this->category = $category;
$this->ttl = $ttl;
$this->type = $type;
$this->action = $viberActionObject;
}

public function toArray(): array
Expand All @@ -33,9 +36,10 @@ public function toArray(): array
$returnArray['image'] = $this->image->toArray();

if ($this->requiresViberServiceObject()) {
$returnArray['viber_service']['category'] = $this->getCategory();
$returnArray['viber_service']['ttl'] = $this->getTtl();
$returnArray['viber_service']['type'] = $this->getType();
$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 array_filter($returnArray);
Expand Down
50 changes: 28 additions & 22 deletions src/Messages/Channel/Viber/ViberServiceObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,65 @@

namespace Vonage\Messages\Channel\Viber;

use Vonage\Messages\Channel\Viber\MessageObjects\ViberActionObject;

trait ViberServiceObjectTrait
{
private ?string $category;
private ?int $ttl;
private ?string $type;
private ?string $category = null;
private ?int $ttl = null;
private ?string $type = null;
private ?ViberActionObject $action = null;

public function requiresViberServiceObject(): bool
{
return $this->getCategory() || $this->getTtl() || $this->getType();
return $this->getCategory() || $this->getTtl() || $this->getType() || $this->getAction();
}

/**
* @return string|null
*/
public function getCategory(): ?string
{
return $this->category;
}

public function setCategory(?string $category): void
public function setCategory(?string $category): static
{
$this->category = $category;

return $this;
}

/**
* @return int|null
*/
public function getTtl(): ?int
{
return $this->ttl;
}

/**
* @param int|null $ttl
*/
public function setTtl(int $ttl): void
public function setTtl(int $ttl): static
{
$this->ttl = $ttl;

return $this;
}

/**
* @return string|null
*/
public function getType(): ?string
{
return $this->type;
}

/**
* @param string|null $type
*/
public function setType(string $type): void
public function setType(string $type): static
{
$this->type = $type;

return $this;
}

public function getAction(): ?ViberActionObject
{
return $this->action;
}

public function setAction(ViberActionObject $viberActionObject): static
{
$this->action = $viberActionObject;

return $this;
}
}
12 changes: 8 additions & 4 deletions src/Messages/Channel/Viber/ViberText.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vonage\Messages\Channel\Viber;

use Vonage\Messages\Channel\Viber\MessageObjects\ViberActionObject;
use Vonage\Messages\MessageTraits\TextTrait;
use Vonage\Messages\Channel\BaseMessage;

Expand All @@ -19,14 +20,16 @@ public function __construct(
string $message,
?string $category = null,
?int $ttl = null,
?string $type = null
?string $type = null,
?ViberActionObject $viberActionObject = null,
) {
$this->to = $to;
$this->from = $from;
$this->text = $message;
$this->category = $category;
$this->ttl = $ttl;
$this->type = $type;
$this->action = $viberActionObject;
}

public function toArray(): array
Expand All @@ -35,9 +38,10 @@ public function toArray(): array
$returnArray['text'] = $this->getText();

if ($this->requiresViberServiceObject()) {
$returnArray['viber_service']['category'] = $this->getCategory();
$returnArray['viber_service']['ttl'] = $this->getTtl();
$returnArray['viber_service']['type'] = $this->getType();
$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 array_filter($returnArray);
Expand Down
38 changes: 38 additions & 0 deletions src/Messages/Channel/Viber/ViberVideo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Vonage\Messages\Channel\Viber;

use Vonage\Messages\MessageObjects\VideoObject;
use Vonage\Messages\MessageTraits\TextTrait;
use Vonage\Messages\Channel\BaseMessage;

class ViberVideo extends BaseMessage
{
use TextTrait;
use ViberServiceObjectTrait;

protected string $subType = BaseMessage::MESSAGES_SUBTYPE_VIDEO;
protected string $channel = 'viber_service';
protected string $thumbUrl = "";

public function __construct(
string $to,
string $from,
string $thumbUrl,
protected VideoObject $videoObject
) {
$this->to = $to;
$this->from = $from;
$this->thumbUrl = $thumbUrl;
}

public function toArray(): array
{
$returnArray = $this->getBaseMessageUniversalOutputArray();
$videoArray = $this->videoObject->toArray();
$videoArray['thumb_url'] = $this->thumbUrl;
$returnArray['video'] = $videoArray;

return $returnArray;
}
}
69 changes: 69 additions & 0 deletions src/Messages/Channel/WhatsApp/MessageObjects/StickerObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Vonage\Messages\Channel\WhatsApp\MessageObjects;

use Vonage\Entity\Hydrator\ArrayHydrateInterface;

class StickerObject implements ArrayHydrateInterface
{
public const STICKER_URL = 'url';
public const STICKER_ID = 'id';

private array $allowedTypes = [
self::STICKER_URL,
self::STICKER_ID
];

public function __construct(private string $type, private string $value = '')
{
if (! in_array($type, $this->allowedTypes, true)) {
throw new \InvalidArgumentException($type . ' is an invalid type of Sticker');
}
}

public function fromArray(array $data): StickerObject
{
if (! in_array($data['type'], $this->allowedTypes, true)) {
throw new \InvalidArgumentException($data['type'] . ' is an invalid type of Sticker');
}

return $this;
}

/**
* @return string
*/
public function getType(): string
{
return $this->type;
}

/**
* @param string $type
*/
public function setType(string $type): void
{
$this->type = $type;
}

/**
* @return string
*/
public function getValue(): string
{
return $this->value;
}

/**
* @param string $value
*/
public function setValue(string $value): void
{
$this->value = $value;
}

public function toArray(): array
{
return [$this->getType() => $this->getValue()];
}
}
Loading

0 comments on commit ea91ad0

Please sign in to comment.