Skip to content

feat: added new MMS types #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Messages/Channel/BaseMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class BaseMessage implements Message
public const MESSAGES_SUBTYPE_TEMPLATE = 'template';
public const MESSAGES_SUBTYPE_STICKER = 'sticker';
public const MESSAGES_SUBTYPE_CUSTOM = 'custom';
public const MESSAGES_SUBTYPE_CONTENT = 'content';

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

namespace Vonage\Messages\Channel\MMS;

use Vonage\Messages\MessageObjects\ContentObject;
use Vonage\Messages\Channel\BaseMessage;
use Vonage\Messages\MessageTraits\TtlTrait;

class MMSContent extends BaseMessage
{
use TtlTrait;

protected string $channel = 'mms';
protected string $subType = BaseMessage::MESSAGES_SUBTYPE_CONTENT;
protected bool $validatesE164 = true;

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

public function validatesE164(): bool
{
return $this->validatesE164;
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an array that can contain multiple content objects?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea the content object will return an array of objects


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

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

namespace Vonage\Messages\Channel\MMS;

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

class MMSFile extends BaseMessage
{
use TtlTrait;

protected string $channel = 'mms';
protected string $subType = BaseMessage::MESSAGES_SUBTYPE_FILE;
protected bool $validatesE164 = true;

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

public function validatesE164(): bool
{
return $this->validatesE164;
}

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

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

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

namespace Vonage\Messages\Channel\MMS;

use Vonage\Messages\Channel\BaseMessage;
use Vonage\Messages\MessageTraits\TtlTrait;

class MMSText extends BaseMessage
{
use TtlTrait;

protected string $channel = 'mms';
protected string $subType = BaseMessage::MESSAGES_SUBTYPE_TEXT;
protected bool $validatesE164 = true;
protected string $text;

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

public function validatesE164(): bool
{
return $this->validatesE164;
}

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

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

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

namespace Vonage\Messages\MessageObjects;

use Vonage\Entity\Hydrator\ArrayHydrateInterface;

class ContentObject implements ArrayHydrateInterface
{
public const TYPE_IMAGE = 'image';
public const TYPE_AUDIO = 'audio';
public const TYPE_VIDEO = 'video';
public const TYPE_VCARD = 'vcard';
public const TYPE_FILE = 'file';

public function __construct(
private string $url,
private string $caption = '',
private string $type = self::TYPE_IMAGE
| self::TYPE_AUDIO
| self::TYPE_VIDEO
| self:: TYPE_VCARD
| self::TYPE_FILE,
) {
}

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

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

return $this;
}

public function toArray(): array
{
$returnArray = [
'url' => $this->url,
'type' => $this->type,
];

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

return $returnArray;
}

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

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

public function getType(): string
{
return $this->type;
}
}
91 changes: 91 additions & 0 deletions test/Messages/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
use Vonage\Client;
use Vonage\Client\APIResource;
use Vonage\Messages\Channel\BaseMessage;
use Vonage\Messages\Channel\MMS\MMSContent;
use Vonage\Messages\Channel\MMS\MMSText;
use Vonage\Messages\Channel\Messenger\MessengerAudio;
use Vonage\Messages\Channel\Messenger\MessengerFile;
use Vonage\Messages\Channel\Messenger\MessengerImage;
use Vonage\Messages\Channel\Messenger\MessengerText;
use Vonage\Messages\Channel\Messenger\MessengerVideo;
use Vonage\Messages\Channel\MMS\MMSAudio;
use Vonage\Messages\Channel\MMS\MMSFile;
use Vonage\Messages\Channel\MMS\MMSImage;
use Vonage\Messages\Channel\MMS\MMSvCard;
use Vonage\Messages\Channel\MMS\MMSVideo;
Expand All @@ -42,6 +45,7 @@
use Vonage\Messages\Client as MessagesClient;
use Vonage\Messages\ExceptionErrorHandler;
use Vonage\Messages\MessageObjects\AudioObject;
use Vonage\Messages\MessageObjects\ContentObject;
use Vonage\Messages\MessageObjects\FileObject;
use Vonage\Messages\MessageObjects\ImageObject;
use Vonage\Messages\MessageObjects\TemplateObject;
Expand Down Expand Up @@ -198,6 +202,93 @@ public function testCanSendSMSWtihFailover(): void
$this->assertArrayHasKey('message_uuid', $result);
}

public function testCanSendMMSContent(): void
{
$contentUrl = 'https://picsum.photos/200/300';
$mmsContentObject = new ContentObject($contentUrl, 'Picture of a skateboarder', ContentObject::TYPE_IMAGE);

$payload = [
'to' => '447700900000',
'from' => '16105551212',
'content' => $mmsContentObject
];

$message = new MMSContent($payload['to'], $payload['from'], $mmsContentObject);
$message->setTtl(400);

$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
$this->assertRequestJsonBodyContains('to', $payload['to'], $request);
$this->assertRequestJsonBodyContains('from', $payload['from'], $request);
$this->assertRequestJsonBodyContains('content', $payload['content']->toArray(), $request);
$this->assertRequestJsonBodyContains('channel', 'mms', $request);
$this->assertRequestJsonBodyContains('message_type', 'content', $request);
$this->assertRequestJsonBodyContains('ttl', 400, $request);
$this->assertEquals('POST', $request->getMethod());

return true;
}))->willReturn($this->getResponse('sms-success', 202));
$result = $this->messageClient->send($message);
$this->assertIsArray($result);
$this->assertArrayHasKey('message_uuid', $result);
}

public function testCanSendMMSText(): void
{
$payload = [
'to' => '447700900000',
'from' => '16105551212',
'text' => 'my cool message'
];

$message = new MMSText($payload['to'], $payload['from'], $payload['text']);
$message->setTtl(400);

$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
$this->assertRequestJsonBodyContains('to', $payload['to'], $request);
$this->assertRequestJsonBodyContains('from', $payload['from'], $request);
$this->assertRequestJsonBodyContains('text', $payload['text'], $request);
$this->assertRequestJsonBodyContains('channel', 'mms', $request);
$this->assertRequestJsonBodyContains('message_type', 'text', $request);
$this->assertRequestJsonBodyContains('ttl', 400, $request);
$this->assertEquals('POST', $request->getMethod());

return true;
}))->willReturn($this->getResponse('sms-success', 202));
$result = $this->messageClient->send($message);
$this->assertIsArray($result);
$this->assertArrayHasKey('message_uuid', $result);
}

public function testCanSendMMSFile(): void
{
$fileUrl = 'https://picsum.photos/200/300';
$mmsFileObject = new FileObject($fileUrl, 'Picture of a skateboarder');

$payload = [
'to' => '447700900000',
'from' => '16105551212',
'file' => $mmsFileObject
];

$message = new MMSFile($payload['to'], $payload['from'], $mmsFileObject);
$message->setTtl(400);

$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
$this->assertRequestJsonBodyContains('to', $payload['to'], $request);
$this->assertRequestJsonBodyContains('from', $payload['from'], $request);
$this->assertRequestJsonBodyContains('file', $payload['file']->toArray(), $request);
$this->assertRequestJsonBodyContains('channel', 'mms', $request);
$this->assertRequestJsonBodyContains('message_type', 'file', $request);
$this->assertRequestJsonBodyContains('ttl', 400, $request);
$this->assertEquals('POST', $request->getMethod());

return true;
}))->willReturn($this->getResponse('sms-success', 202));
$result = $this->messageClient->send($message);
$this->assertIsArray($result);
$this->assertArrayHasKey('message_uuid', $result);
}

public function testCanSendMMSImage(): void
{
$imageUrl = 'https://picsum.photos/200/300';
Expand Down
Loading