Skip to content

Commit ea91ad0

Browse files
authored
New Message Types (#388)
* PHP8: remove frivolous docblocks * Added two new files types for viber * Added new whatsapp sticker type * Tests for ViberVideo * dynamic property
1 parent 180c7f4 commit ea91ad0

File tree

10 files changed

+399
-30
lines changed

10 files changed

+399
-30
lines changed

src/Messages/Channel/BaseMessage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ abstract class BaseMessage implements Message
1717
public const MESSAGES_SUBTYPE_VIDEO = 'video';
1818
public const MESSAGES_SUBTYPE_FILE = 'file';
1919
public const MESSAGES_SUBTYPE_TEMPLATE = 'template';
20+
public const MESSAGES_SUBTYPE_STICKER = 'sticker';
2021
public const MESSAGES_SUBTYPE_CUSTOM = 'custom';
2122

2223
public function getClientRef(): ?string
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Vonage\Messages\Channel\Viber\MessageObjects;
4+
5+
use Vonage\Entity\Hydrator\ArrayHydrateInterface;
6+
7+
class ViberActionObject implements ArrayHydrateInterface
8+
{
9+
public function __construct(private string $url, private string $text = '')
10+
{
11+
}
12+
13+
public function fromArray(array $data): ViberActionObject
14+
{
15+
$this->url = $data['url'];
16+
$this->text = $data['text'];
17+
18+
return $this;
19+
}
20+
21+
public function toArray(): array
22+
{
23+
return [
24+
'url' => $this->getUrl(),
25+
'text' => $this->getText()
26+
];
27+
}
28+
29+
public function getUrl(): string
30+
{
31+
return $this->url;
32+
}
33+
34+
public function getText(): string
35+
{
36+
return $this->text;
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Vonage\Messages\Channel\Viber;
4+
5+
use Vonage\Messages\MessageObjects\FileObject;
6+
use Vonage\Messages\Channel\BaseMessage;
7+
class ViberFile extends BaseMessage
8+
{
9+
use ViberServiceObjectTrait;
10+
11+
protected string $channel = 'viber_service';
12+
protected string $subType = BaseMessage::MESSAGES_SUBTYPE_FILE;
13+
14+
public function __construct(
15+
string $to,
16+
string $from,
17+
protected FileObject $fileObject
18+
) {
19+
$this->to = $to;
20+
$this->from = $from;
21+
}
22+
23+
public function toArray(): array
24+
{
25+
$returnArray = $this->getBaseMessageUniversalOutputArray();
26+
$returnArray['file'] = $this->fileObject->toArray();
27+
28+
if ($this->requiresViberServiceObject()) {
29+
$this->getCategory() ? $returnArray['viber_service']['category'] = $this->getCategory(): null;
30+
$this->getTtl() ? $returnArray['viber_service']['ttl'] = $this->getTtl(): null;
31+
$this->getType() ? $returnArray['viber_service']['type'] = $this->getType(): null;
32+
}
33+
34+
return $returnArray;
35+
}
36+
}

src/Messages/Channel/Viber/ViberImage.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Vonage\Messages\Channel\Viber;
44

5+
use Vonage\Messages\Channel\Viber\MessageObjects\ViberActionObject;
56
use Vonage\Messages\MessageObjects\ImageObject;
67
use Vonage\Messages\Channel\BaseMessage;
78

@@ -18,13 +19,15 @@ public function __construct(
1819
protected ImageObject $image,
1920
?string $category = null,
2021
?int $ttl = null,
21-
?string $type = null
22+
?string $type = null,
23+
?ViberActionObject $viberActionObject = null
2224
) {
2325
$this->to = $to;
2426
$this->from = $from;
2527
$this->category = $category;
2628
$this->ttl = $ttl;
2729
$this->type = $type;
30+
$this->action = $viberActionObject;
2831
}
2932

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

3538
if ($this->requiresViberServiceObject()) {
36-
$returnArray['viber_service']['category'] = $this->getCategory();
37-
$returnArray['viber_service']['ttl'] = $this->getTtl();
38-
$returnArray['viber_service']['type'] = $this->getType();
39+
$this->getCategory() ? $returnArray['viber_service']['category'] = $this->getCategory(): null;
40+
$this->getTtl() ? $returnArray['viber_service']['ttl'] = $this->getTtl(): null;
41+
$this->getType() ? $returnArray['viber_service']['type'] = $this->getType(): null;
42+
$this->getAction() ? $returnArray['viber_service']['action'] = $this->getAction()->toArray(): null;
3943
}
4044

4145
return array_filter($returnArray);

src/Messages/Channel/Viber/ViberServiceObjectTrait.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,65 @@
22

33
namespace Vonage\Messages\Channel\Viber;
44

5+
use Vonage\Messages\Channel\Viber\MessageObjects\ViberActionObject;
6+
57
trait ViberServiceObjectTrait
68
{
7-
private ?string $category;
8-
private ?int $ttl;
9-
private ?string $type;
9+
private ?string $category = null;
10+
private ?int $ttl = null;
11+
private ?string $type = null;
12+
private ?ViberActionObject $action = null;
1013

1114
public function requiresViberServiceObject(): bool
1215
{
13-
return $this->getCategory() || $this->getTtl() || $this->getType();
16+
return $this->getCategory() || $this->getTtl() || $this->getType() || $this->getAction();
1417
}
1518

16-
/**
17-
* @return string|null
18-
*/
1919
public function getCategory(): ?string
2020
{
2121
return $this->category;
2222
}
2323

24-
public function setCategory(?string $category): void
24+
public function setCategory(?string $category): static
2525
{
2626
$this->category = $category;
27+
28+
return $this;
2729
}
2830

29-
/**
30-
* @return int|null
31-
*/
3231
public function getTtl(): ?int
3332
{
3433
return $this->ttl;
3534
}
3635

37-
/**
38-
* @param int|null $ttl
39-
*/
40-
public function setTtl(int $ttl): void
36+
public function setTtl(int $ttl): static
4137
{
4238
$this->ttl = $ttl;
39+
40+
return $this;
4341
}
4442

45-
/**
46-
* @return string|null
47-
*/
4843
public function getType(): ?string
4944
{
5045
return $this->type;
5146
}
5247

53-
/**
54-
* @param string|null $type
55-
*/
56-
public function setType(string $type): void
48+
public function setType(string $type): static
5749
{
5850
$this->type = $type;
51+
52+
return $this;
53+
}
54+
55+
public function getAction(): ?ViberActionObject
56+
{
57+
return $this->action;
58+
}
59+
60+
public function setAction(ViberActionObject $viberActionObject): static
61+
{
62+
$this->action = $viberActionObject;
63+
64+
return $this;
5965
}
6066
}

src/Messages/Channel/Viber/ViberText.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Vonage\Messages\Channel\Viber;
44

5+
use Vonage\Messages\Channel\Viber\MessageObjects\ViberActionObject;
56
use Vonage\Messages\MessageTraits\TextTrait;
67
use Vonage\Messages\Channel\BaseMessage;
78

@@ -19,14 +20,16 @@ public function __construct(
1920
string $message,
2021
?string $category = null,
2122
?int $ttl = null,
22-
?string $type = null
23+
?string $type = null,
24+
?ViberActionObject $viberActionObject = null,
2325
) {
2426
$this->to = $to;
2527
$this->from = $from;
2628
$this->text = $message;
2729
$this->category = $category;
2830
$this->ttl = $ttl;
2931
$this->type = $type;
32+
$this->action = $viberActionObject;
3033
}
3134

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

3740
if ($this->requiresViberServiceObject()) {
38-
$returnArray['viber_service']['category'] = $this->getCategory();
39-
$returnArray['viber_service']['ttl'] = $this->getTtl();
40-
$returnArray['viber_service']['type'] = $this->getType();
41+
$this->getCategory() ? $returnArray['viber_service']['category'] = $this->getCategory(): null;
42+
$this->getTtl() ? $returnArray['viber_service']['ttl'] = $this->getTtl(): null;
43+
$this->getType() ? $returnArray['viber_service']['type'] = $this->getType(): null;
44+
$this->getAction() ? $returnArray['viber_service']['action'] = $this->getAction()->toArray(): null;
4145
}
4246

4347
return array_filter($returnArray);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Vonage\Messages\Channel\Viber;
4+
5+
use Vonage\Messages\MessageObjects\VideoObject;
6+
use Vonage\Messages\MessageTraits\TextTrait;
7+
use Vonage\Messages\Channel\BaseMessage;
8+
9+
class ViberVideo extends BaseMessage
10+
{
11+
use TextTrait;
12+
use ViberServiceObjectTrait;
13+
14+
protected string $subType = BaseMessage::MESSAGES_SUBTYPE_VIDEO;
15+
protected string $channel = 'viber_service';
16+
protected string $thumbUrl = "";
17+
18+
public function __construct(
19+
string $to,
20+
string $from,
21+
string $thumbUrl,
22+
protected VideoObject $videoObject
23+
) {
24+
$this->to = $to;
25+
$this->from = $from;
26+
$this->thumbUrl = $thumbUrl;
27+
}
28+
29+
public function toArray(): array
30+
{
31+
$returnArray = $this->getBaseMessageUniversalOutputArray();
32+
$videoArray = $this->videoObject->toArray();
33+
$videoArray['thumb_url'] = $this->thumbUrl;
34+
$returnArray['video'] = $videoArray;
35+
36+
return $returnArray;
37+
}
38+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Vonage\Messages\Channel\WhatsApp\MessageObjects;
4+
5+
use Vonage\Entity\Hydrator\ArrayHydrateInterface;
6+
7+
class StickerObject implements ArrayHydrateInterface
8+
{
9+
public const STICKER_URL = 'url';
10+
public const STICKER_ID = 'id';
11+
12+
private array $allowedTypes = [
13+
self::STICKER_URL,
14+
self::STICKER_ID
15+
];
16+
17+
public function __construct(private string $type, private string $value = '')
18+
{
19+
if (! in_array($type, $this->allowedTypes, true)) {
20+
throw new \InvalidArgumentException($type . ' is an invalid type of Sticker');
21+
}
22+
}
23+
24+
public function fromArray(array $data): StickerObject
25+
{
26+
if (! in_array($data['type'], $this->allowedTypes, true)) {
27+
throw new \InvalidArgumentException($data['type'] . ' is an invalid type of Sticker');
28+
}
29+
30+
return $this;
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getType(): string
37+
{
38+
return $this->type;
39+
}
40+
41+
/**
42+
* @param string $type
43+
*/
44+
public function setType(string $type): void
45+
{
46+
$this->type = $type;
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getValue(): string
53+
{
54+
return $this->value;
55+
}
56+
57+
/**
58+
* @param string $value
59+
*/
60+
public function setValue(string $value): void
61+
{
62+
$this->value = $value;
63+
}
64+
65+
public function toArray(): array
66+
{
67+
return [$this->getType() => $this->getValue()];
68+
}
69+
}

0 commit comments

Comments
 (0)