Skip to content

Commit db2cdad

Browse files
authored
Add from validation on request (#477)
1 parent f1bc731 commit db2cdad

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/Verify2/VerifyObjects/VerificationWorkflow.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function __construct(
3131
if (! in_array($channel, $this->allowedWorkflows, true)) {
3232
throw new \InvalidArgumentException($this->channel . ' is not a valid workflow');
3333
}
34+
35+
if ($this->isInvalidFromValue($this->from)) {
36+
throw new \InvalidArgumentException($this->from . ' is not a valid from value');
37+
}
3438
}
3539

3640
public function getChannel(): string
@@ -112,4 +116,30 @@ public function setCustomKeys(array $customKeys): self
112116

113117
return $this;
114118
}
119+
120+
protected function isInvalidFromValue(string $fromValue): bool
121+
{
122+
if ($fromValue === '') {
123+
// This is a null value and doesn't need to be validated
124+
return false;
125+
}
126+
127+
if (($this->channel === self::WORKFLOW_EMAIL) && filter_var($fromValue, FILTER_VALIDATE_EMAIL)) {
128+
return false;
129+
}
130+
131+
if (is_numeric($fromValue)) {
132+
$length = strlen($fromValue);
133+
134+
return $length < 11 || $length > 15;
135+
}
136+
137+
if (ctype_alnum($fromValue)) {
138+
$length = strlen($fromValue);
139+
140+
return $length < 3 || $length > 11;
141+
}
142+
143+
return true;
144+
}
115145
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Verify2\VerifyObjects;
6+
7+
use InvalidArgumentException;
8+
use Vonage\Verify2\VerifyObjects\VerificationWorkflow;
9+
use VonageTest\VonageTestCase;
10+
11+
class VerificationWorkflowTest extends VonageTestCase
12+
{
13+
public function testWillTakeValidNumericFromValue(): void
14+
{
15+
$workflow = new VerificationWorkflow(
16+
VerificationWorkflow::WORKFLOW_SMS,
17+
'xxx',
18+
'44773666552'
19+
);
20+
21+
$this->assertInstanceOf(VerificationWorkflow::class, $workflow);
22+
}
23+
24+
public function testWillTakeValidAlphaFromValue(): void
25+
{
26+
$workflow = new VerificationWorkflow(
27+
VerificationWorkflow::WORKFLOW_SMS,
28+
'xxx',
29+
'ACMECOMPANY'
30+
);
31+
32+
$this->assertInstanceOf(VerificationWorkflow::class, $workflow);
33+
}
34+
35+
public function testWillThrowErrorOnInvalidNumericFromValue(): void
36+
{
37+
$this->expectException(InvalidArgumentException::class);
38+
39+
$workflow = new VerificationWorkflow(
40+
VerificationWorkflow::WORKFLOW_SMS,
41+
'xxx',
42+
'3459568445'
43+
);
44+
}
45+
46+
public function testWillThrowErrorOnInvalidAlphaFromValue(): void
47+
{
48+
$this->expectException(InvalidArgumentException::class);
49+
50+
$workflow = new VerificationWorkflow(
51+
VerificationWorkflow::WORKFLOW_SMS,
52+
'xxx',
53+
'MYAWESOMECOMPANYISTOOBIG'
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)