Skip to content

Commit

Permalink
Messages validation added to remove a leading plus for the developer,…
Browse files Browse the repository at this point in the history
… but only valid E164 can be sent (#507)
  • Loading branch information
SecondeJK authored Sep 26, 2024
1 parent 24bcc1a commit 34a172e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Messages/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ public function getAPIResource(): APIResource

public function send(BaseMessage $message): ?array
{
return $this->getAPIResource()->create($message->toArray());
$messageArray = $message->toArray();

if ($this->isValidE164($messageArray['to'])) {
$messageArray['to'] = $this->stripLeadingPlus($messageArray['to']);
return $this->getAPIResource()->create($messageArray);
};

throw new \InvalidArgumentException('Number provided is not a valid E164 number');
}

public function updateRcsStatus(string $messageUuid, string $status): bool
Expand All @@ -36,4 +43,22 @@ public function updateRcsStatus(string $messageUuid, string $status): bool
}
return false;
}

protected function stripLeadingPlus(string $phoneNumber): string
{
if (str_starts_with($phoneNumber, '+')) {
return substr($phoneNumber, 1);
}

return $phoneNumber;
}

public function isValidE164(string $phoneNumber): bool
{
$phoneNumber = $this->stripLeadingPlus($phoneNumber);

$regex = '/^\+?[1-9]\d{1,14}$/';

return preg_match($regex, $phoneNumber) === 1;
}
}
46 changes: 46 additions & 0 deletions test/Messages/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1243,4 +1243,50 @@ public function stickerTypeProvider(): array
['caption', 'this is not valid', false]
];
}

public function testWillSendValidE164Number()
{
$message = new SMSText('447700900000', '16105551212', 'Reticulating Splines');

$this->vonageClient->send(Argument::that(function (Request $request) {
$this->assertRequestJsonBodyContains('to', '447700900000', $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 testWillSendValidE164NumberWithPlus()
{
$message = new SMSText('+447700900000', '16105551212', 'Reticulating Splines');

$this->vonageClient->send(Argument::that(function (Request $request) {
$this->assertRequestJsonBodyContains('to', '447700900000', $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 testWillErrorOnInvalidE164Number()
{
$this->expectException(\InvalidArgumentException::class);
$message = new SMSText('00447700900000', '16105551212', 'Reticulating Splines');

$result = $this->messageClient->send($message);
}

public function testWillErrorOnInvalidE164NumberWithPlus()
{
$this->expectException(\InvalidArgumentException::class);
$message = new SMSText('+00447700900000', '16105551212', 'Reticulating Splines');

$result = $this->messageClient->send($message);
}
}

0 comments on commit 34a172e

Please sign in to comment.