Skip to content

Commit 121ef74

Browse files
committed
SmsValidator: allow empty values for non-bool fields
1 parent e12727a commit 121ef74

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

src/Validator/BaseValidator.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,35 @@ public function __construct(array $parameters = [], array $boolOptions = []) {
1717
$this->boolOptions = $boolOptions;
1818
}
1919

20+
public static function isValidDate(string $date): bool {
21+
return Util::isValidDate($date, 'Y-m-d');
22+
}
23+
24+
public function isValidBool($string): bool {
25+
$string = (int)$string;
26+
27+
return 1 === $string || 0 === $string;
28+
}
29+
30+
/**
31+
* @param string $key
32+
* @param mixed $fallback
33+
* @return mixed
34+
*/
35+
public function fallback(string $key, $fallback = null) {
36+
return $this->parameters[$key] ?? $fallback;
37+
}
38+
2039
/** @throws InvalidBooleanOptionException */
2140
protected function validate(): void {
2241
foreach ($this->boolOptions as $k) {
2342
$v = $this->toBool($k, $this->parameters[$k] ?? null);
2443

25-
if (null !== $v) {
26-
$this->parameters[$k] = $v;
44+
if (null === $v) {
45+
continue;
2746
}
47+
48+
$this->parameters[$k] = $v;
2849
}
2950
}
3051

@@ -46,23 +67,4 @@ private function toBool(string $k, $v): ?int {
4667

4768
throw new InvalidBooleanOptionException($k, $v);
4869
}
49-
50-
public static function isValidDate(string $date): bool {
51-
return Util::isValidDate($date, 'Y-m-d');
52-
}
53-
54-
public function isValidBool($string): bool {
55-
$string = (int)$string;
56-
57-
return 1 === $string || 0 === $string;
58-
}
59-
60-
/**
61-
* @param string $key
62-
* @param mixed $fallback
63-
* @return mixed
64-
*/
65-
public function fallback(string $key, $fallback = null) {
66-
return $this->parameters[$key] ?? $fallback;
67-
}
6870
}

src/Validator/SmsValidator.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use Sms77\Api\Exception\InvalidBooleanOptionException;
77
use Sms77\Api\Exception\InvalidOptionalArgumentException;
88
use Sms77\Api\Exception\InvalidRequiredArgumentException;
9-
use Sms77\Api\Params\SmsParamsInterface;
109
use Sms77\Api\Library\Util;
10+
use Sms77\Api\Params\SmsParamsInterface;
1111

1212
class SmsValidator extends BaseValidator implements ValidatorInterface {
1313
/* @var SmsParamsInterface $params */
@@ -50,7 +50,7 @@ public function validate(): void {
5050
public function delay(): void {
5151
$delay = $this->params->getDelay();
5252

53-
if (null === $delay) {
53+
if (null === $delay || '' === $delay) {
5454
return;
5555
}
5656

@@ -70,7 +70,7 @@ public function delay(): void {
7070
public function foreign_id(): void {
7171
$foreignId = $this->params->getForeignId();
7272

73-
if (null === $foreignId) {
73+
if (null === $foreignId || '' === $foreignId) {
7474
return;
7575
}
7676

@@ -91,7 +91,7 @@ public function foreign_id(): void {
9191
public function from(): void {
9292
$from = $this->params->getFrom();
9393

94-
if (null === $from) {
94+
if (null === $from || '' === $from) {
9595
return;
9696
}
9797

@@ -123,7 +123,7 @@ public function from(): void {
123123
public function label(): void {
124124
$label = $this->params->getLabel();
125125

126-
if (null === $label) {
126+
if (null === $label || '' === $label) {
127127
return;
128128
}
129129

@@ -161,7 +161,9 @@ public function text(): void {
161161

162162
/** @throws InvalidRequiredArgumentException */
163163
public function to(): void {
164-
if (null === $this->params->getTo()) {
164+
$to = $this->params->getTo();
165+
166+
if (null === $to || '' === $to) {
165167
throw new InvalidRequiredArgumentException(
166168
'You cannot send a message without specifying a recipient.');
167169
}
@@ -171,19 +173,26 @@ public function to(): void {
171173
public function ttl(): void {
172174
$ttl = $this->params->getTtl();
173175

174-
if (null !== $ttl) {
175-
$min = SmsConstants::TTL_MIN;
176-
$max = SmsConstants::TTL_MAX;
176+
if (null === $ttl) {
177+
return;
178+
}
177179

178-
if ($ttl < $min) {
179-
throw new InvalidOptionalArgumentException(
180-
"ttl must be at least $min.");
181-
}
180+
if (0 === $ttl) {
181+
$this->params->setTtl(null);
182+
return;
183+
}
182184

183-
if ($ttl > $max) {
184-
throw new InvalidOptionalArgumentException(
185-
"ttl may not exceed $max.");
186-
}
185+
$min = SmsConstants::TTL_MIN;
186+
$max = SmsConstants::TTL_MAX;
187+
188+
if ($ttl < $min) {
189+
throw new InvalidOptionalArgumentException(
190+
"ttl must be at least $min.");
191+
}
192+
193+
if ($ttl > $max) {
194+
throw new InvalidOptionalArgumentException(
195+
"ttl may not exceed $max.");
187196
}
188197
}
189198
}

0 commit comments

Comments
 (0)