Skip to content

Commit 7ff7a54

Browse files
committed
implemented app exceptions
1 parent 537bfda commit 7ff7a54

11 files changed

+132
-92
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Sms77\Api\Exception;
4+
5+
use Exception;
6+
7+
class InvalidOptionalArgumentException extends Exception
8+
{
9+
public function __construct($message, $code = 0, Exception $previous = null)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
}
13+
14+
public function __toString()
15+
{
16+
return __CLASS__ . ": [{$this->code}]: {$this->message}" . PHP_EOL;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Sms77\Api\Exception;
4+
5+
use Exception;
6+
7+
class InvalidRequiredArgumentException extends Exception
8+
{
9+
public function __construct($message, $code = 0, Exception $previous = null)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
}
13+
14+
public function __toString()
15+
{
16+
return __CLASS__ . ": [{$this->code}]: {$this->message}" . PHP_EOL;
17+
}
18+
}

src/Validator/BaseValidator.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,46 @@
22

33
namespace Sms77\Api\Validator;
44

5-
use Exception;
5+
use Sms77\Api\Exception\InvalidOptionalArgumentException;
6+
use Sms77\Api\Exception\InvalidRequiredArgumentException;
67

78
class BaseValidator
89
{
910
/* @var array $parameters */
1011
protected $parameters;
1112

13+
protected $allowedTypes = ["direct", "economy"];
14+
1215
function __construct(array $parameters)
1316
{
1417
$this->parameters = $parameters;
1518

1619
if (!isset($parameters["p"])) {
17-
throw new Exception("The required parameter p is missing.");
20+
throw new InvalidRequiredArgumentException("p is missing.");
1821
}
1922
}
2023

2124
protected function isValidBool($data)
2225
{
2326
return 1 == $data || 0 == $data;
2427
}
28+
29+
protected function throwOnOptionalBadType()
30+
{
31+
$types = ["direct", "economy"];
32+
33+
$type = isset($this->parameters["type"]) ? $this->parameters["type"] : null;
34+
35+
if (null !== $type && !in_array($type, $types)) {
36+
throw new InvalidOptionalArgumentException("type has invalid value $type. Allowed values are: " . join(",", $types) . ".");
37+
}
38+
}
39+
40+
protected function isValidUnixTimestamp($timestamp)
41+
{
42+
/*https://stackoverflow.com/questions/2524680/check-whether-the-string-is-a-unix-timestamp*/
43+
return ((string)(int)$timestamp === $timestamp)
44+
&& ($timestamp <= PHP_INT_MAX)
45+
&& ($timestamp >= ~PHP_INT_MAX);
46+
}
2547
}

src/Validator/ContactsValidator.php

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

33
namespace Sms77\Api\Validator;
44

5-
use Exception;
5+
use Sms77\Api\Exception\InvalidOptionalArgumentException;
6+
use Sms77\Api\Exception\InvalidRequiredArgumentException;
67

78
class ContactsValidator extends BaseValidator implements ValidatorInterface
89
{
@@ -24,7 +25,7 @@ function action()
2425
$actions = ["read", "write", "del"];
2526

2627
if (!in_array($action, $actions)) {
27-
throw new Exception("Unknown action $action.");
28+
throw new InvalidRequiredArgumentException("Unknown action $action.");
2829
}
2930
}
3031

@@ -34,7 +35,7 @@ function json()
3435

3536
if (null !== $json) {
3637
if (!$this->isValidBool($json)) {
37-
throw new Exception("The parameter json can be either 1 or 0.");
38+
throw new InvalidOptionalArgumentException("json can be either 1 or 0.");
3839
}
3940
}
4041
}

src/Validator/LookupValidator.php

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

33
namespace Sms77\Api\Validator;
44

5-
use Exception;
5+
use Sms77\Api\Exception\InvalidOptionalArgumentException;
6+
use Sms77\Api\Exception\InvalidRequiredArgumentException;
67

78
class LookupValidator extends BaseValidator implements ValidatorInterface
89
{
@@ -26,11 +27,11 @@ function json()
2627
$type = isset($this->parameters["type"]) ? $this->parameters["type"] : null;
2728

2829
if ("mnp" !== $type) {
29-
throw new Exception("The parameter json may only be set if type is set to mnp.");
30+
throw new InvalidOptionalArgumentException("json may only be set if type is set to mnp.");
3031
}
3132

3233
if (!$this->isValidBool($json)) {
33-
throw new Exception("The parameter json can be either 1 or 0.");
34+
throw new InvalidOptionalArgumentException("json can be either 1 or 0.");
3435
}
3536
}
3637
}
@@ -40,7 +41,7 @@ function number()
4041
$number = isset($this->parameters["number"]) ? $this->parameters["number"] : null;
4142

4243
if (!isset($this->parameters["number"]) || !strlen($number)) {
43-
throw new Exception("Required parameter number is missing.");
44+
throw new InvalidRequiredArgumentException("number is missing.");
4445
}
4546
}
4647

@@ -49,7 +50,7 @@ function type()
4950
$type = isset($this->parameters["type"]) ? $this->parameters["type"] : null;
5051

5152
if (!in_array($type, ["cnam", "format", "hlr", "mnp"])) {
52-
throw new Exception("Invalid required parameter type: $type.");
53+
throw new InvalidRequiredArgumentException("type seems to have an invalid value: $type.");
5354
}
5455
}
5556
}

src/Validator/PricingValidator.php

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

33
namespace Sms77\Api\Validator;
44

5-
use Exception;
5+
use Sms77\Api\Exception\InvalidOptionalArgumentException;
66

77
class PricingValidator extends BaseValidator implements ValidatorInterface
88
{
@@ -23,7 +23,7 @@ function country()
2323
$country = isset($this->parameters["country"]) ? $this->parameters["country"] : null;
2424

2525
if (!strlen($country)) {
26-
throw new Exception("Invalid optional parameter country: $country.");
26+
throw new InvalidOptionalArgumentException("country seems to be invalid: $country.");
2727
}
2828
}
2929

@@ -32,16 +32,12 @@ function format()
3232
$format = isset($this->parameters["format"]) ? $this->parameters["format"] : null;
3333

3434
if (!in_array($format, ["csv", "json"])) {
35-
throw new Exception("Invalid optional parameter format: $format.");
35+
throw new InvalidOptionalArgumentException("format seems to be invalid: $format.");
3636
}
3737
}
3838

3939
function type()
4040
{
41-
$type = isset($this->parameters["type"]) ? $this->parameters["type"] : null;
42-
43-
if (!in_array($type, ["direct", "economy"])) {
44-
throw new Exception("Invalid optional parameter type: $type.");
45-
}
41+
$this->throwOnOptionalBadType();
4642
}
4743
}

0 commit comments

Comments
 (0)