Skip to content

Commit 1946931

Browse files
committed
Throw custom exception
1 parent 9502132 commit 1946931

19 files changed

Lines changed: 128 additions & 117 deletions

src/Filter/Arrays.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ final class Arrays
2525
*
2626
* @throws \InvalidArgumentException if $minCount was not an int
2727
* @throws \InvalidArgumentException if $maxCount was not an int
28-
* @throws \Exception if $value is not an array
29-
* @throws \Exception if $value count is less than $minCount
30-
* @throws \Exception if $value count is greater than $maxCount
28+
* @throws Exception if $value is not an array
29+
* @throws Exception if $value count is less than $minCount
30+
* @throws Exception if $value count is greater than $maxCount
3131
*/
3232
public static function filter($value, $minCount = 1, $maxCount = PHP_INT_MAX)
3333
{
@@ -40,22 +40,22 @@ public static function filter($value, $minCount = 1, $maxCount = PHP_INT_MAX)
4040
}
4141

4242
if (!is_array($value)) {
43-
throw new \Exception("Value '" . trim(var_export($value, true), "'") . "' is not an array");
43+
throw new Exception("Value '" . trim(var_export($value, true), "'") . "' is not an array");
4444
}
4545

4646
//optimization for default case
4747
if ($minCount === 1 && empty($value)) {
48-
throw new \Exception('$value count of 0 is less than 1');
48+
throw new Exception('$value count of 0 is less than 1');
4949
}
5050

5151
$count = count($value);
5252

5353
if ($count < $minCount) {
54-
throw new \Exception("\$value count of {$count} is less than {$minCount}");
54+
throw new Exception("\$value count of {$count} is less than {$minCount}");
5555
}
5656

5757
if ($count > $maxCount) {
58-
throw new \Exception("\$value count of {$count} is greater than {$maxCount}");
58+
throw new Exception("\$value count of {$count} is greater than {$maxCount}");
5959
}
6060

6161
return $value;
@@ -74,7 +74,7 @@ public static function filter($value, $minCount = 1, $maxCount = PHP_INT_MAX)
7474
*
7575
* @see in_array()
7676
* @throws \InvalidArgumentException if $strict was not a bool
77-
* @throws \Exception if $value is not in array $haystack
77+
* @throws Exception if $value is not in array $haystack
7878
*/
7979
public static function in($value, array $haystack, $strict = true)
8080
{
@@ -83,7 +83,7 @@ public static function in($value, array $haystack, $strict = true)
8383
}
8484

8585
if (!in_array($value, $haystack, $strict)) {
86-
throw new \Exception(
86+
throw new Exception(
8787
"Value '" . trim(var_export($value, true), "'") . "' is not in array " . var_export($haystack, true)
8888
);
8989
}
@@ -101,7 +101,7 @@ public static function in($value, array $haystack, $strict = true)
101101
*
102102
* @return array the filtered $values
103103
*
104-
* @throws \Exception if any member of $values fails filtering
104+
* @throws Exception if any member of $values fails filtering
105105
*/
106106
public static function ofScalars(array $values, array $filters)
107107
{
@@ -112,7 +112,7 @@ public static function ofScalars(array $values, array $filters)
112112

113113
list($status, $result, $error) = Filterer::filter($wrappedFilters, $values);
114114
if (!$status) {
115-
throw new \Exception($error);
115+
throw new Exception($error);
116116
}
117117

118118
return $result;
@@ -128,7 +128,7 @@ public static function ofScalars(array $values, array $filters)
128128
*
129129
* @return array the filtered $values
130130
*
131-
* @throws \Exception if any member of $values fails filtering
131+
* @throws Exception if any member of $values fails filtering
132132
*/
133133
public static function ofArrays(array $values, array $spec)
134134
{
@@ -150,7 +150,7 @@ public static function ofArrays(array $values, array $spec)
150150
}
151151

152152
if (!empty($errors)) {
153-
throw new \Exception(implode("\n", $errors));
153+
throw new Exception(implode("\n", $errors));
154154
}
155155

156156
return $results;
@@ -166,13 +166,13 @@ public static function ofArrays(array $values, array $spec)
166166
*
167167
* @return array the filtered $value
168168
*
169-
* @throws \Exception if $value fails filtering
169+
* @throws Exception if $value fails filtering
170170
*/
171171
public static function ofArray(array $value, array $spec)
172172
{
173173
list($status, $result, $error) = Filterer::filter($spec, $value);
174174
if (!$status) {
175-
throw new \Exception($error);
175+
throw new Exception($error);
176176
}
177177

178178
return $result;

src/Filter/Booleans.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ final class Booleans
2525
* @return bool|null the filtered $value
2626
*
2727
* @throws \InvalidArgumentException if $allowNull is not a boolean
28-
* @throws \Exception if $value is not a string
29-
* @throws \Exception if $value is not 'true' or 'false' disregarding case and whitespace
28+
* @throws Exception if $value is not a string
29+
* @throws Exception if $value is not 'true' or 'false' disregarding case and whitespace
3030
*/
3131
public static function filter(
3232
$value,
@@ -47,7 +47,7 @@ public static function filter(
4747
}
4848

4949
if (!is_string($value)) {
50-
throw new \Exception('"' . var_export($value, true) . '" $value is not a string');
50+
throw new Exception('"' . var_export($value, true) . '" $value is not a string');
5151
}
5252

5353
$value = trim($value);
@@ -62,7 +62,7 @@ public static function filter(
6262
return false;
6363
}
6464

65-
throw new \Exception(
65+
throw new Exception(
6666
sprintf(
6767
"%s is not '%s' disregarding case and whitespace",
6868
$value,
@@ -80,12 +80,12 @@ public static function filter(
8080
*
8181
* @return mixed
8282
*
83-
* @throws \Exception Thrown if $value is not a boolean
83+
* @throws Exception Thrown if $value is not a boolean
8484
*/
8585
public static function convert($value, $true = 'true', $false = 'false')
8686
{
8787
if ($value !== false && $value !== true) {
88-
throw new \Exception('$value was not a bool');
88+
throw new Exception('$value was not a bool');
8989
}
9090

9191
return $value ? $true : $false;

src/Filter/DateTime.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DateTime
1717
* @return \DateTime
1818
*
1919
* @throws \InvalidArgumentException Thrown if $allowNull was not a boolean value.
20-
* @throws \Exception if the value did not pass validation.
20+
* @throws Exception if the value did not pass validation.
2121
*/
2222
public static function filter($value, $allowNull = false, \DateTimeZone $timezone = null)
2323
{
@@ -38,7 +38,7 @@ public static function filter($value, $allowNull = false, \DateTimeZone $timezon
3838
}
3939

4040
if (!is_string($value) || trim($value) == '') {
41-
throw new \Exception('$value is not a non-empty string');
41+
throw new Exception('$value is not a non-empty string');
4242
}
4343

4444
return new \DateTime($value, $timezone);

src/Filter/DateTimeZone.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class DateTimeZone
1515
* @return \DateTimeZone
1616
*
1717
* @throws \InvalidArgumentException Thrown if $allowNull was not a boolean value.
18-
* @throws \Exception if the value did not pass validation.
18+
* @throws Exception if the value did not pass validation.
1919
*/
2020
public static function filter($value, $allowNull = false)
2121
{
@@ -32,9 +32,13 @@ public static function filter($value, $allowNull = false)
3232
}
3333

3434
if (!is_string($value) || trim($value) == '') {
35-
throw new \Exception('$value not a non-empty string');
35+
throw new Exception('$value not a non-empty string');
3636
}
3737

38-
return new \DateTimeZone($value);
38+
try {
39+
return new \DateTimeZone($value);
40+
} catch (\Exception $e) {
41+
throw new Exception($e->getMessage(), $e->getCode(), $e);
42+
}
3943
}
4044
}

src/Filter/Email.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ final class Email
1919
*
2020
* @return string The passed in $value.
2121
*
22-
* @throws \Exception if the value did not pass validation.
22+
* @throws Exception if the value did not pass validation.
2323
*/
2424
public static function filter($value)
2525
{
2626
if (!is_string($value)) {
27-
throw new \Exception("Value '" . var_export($value, true) . "' is not a string");
27+
throw new Exception("Value '" . var_export($value, true) . "' is not a string");
2828
}
2929

3030
$filteredEmail = filter_var($value, FILTER_VALIDATE_EMAIL);
3131
if ($filteredEmail === false) {
32-
throw new \Exception("Value '{$value}' is not a valid email");
32+
throw new Exception("Value '{$value}' is not a valid email");
3333
}
3434

3535
return $filteredEmail;

src/Filter/Exception.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace DominionEnterprises\Filter;
4+
5+
class Exception extends \Exception
6+
{
7+
}

src/Filter/Floats.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ final class Floats
2828
* @throws \InvalidArgumentException if $minValue is not null and not a float
2929
* @throws \InvalidArgumentException if $maxValue is not null and not a float
3030
* @throws \InvalidArgumentException if $castInts is not a boolean
31-
* @throws \Exception if $value does not pass is_numeric
32-
* @throws \Exception if $value is hex format
33-
* @throws \Exception if $value is not a string or float
34-
* @throws \Exception if $value overflow or underflows
35-
* @throws \Exception if $value is less than $minValue
36-
* @throws \Exception if $value is greater than $maxValue
31+
* @throws Exception if $value does not pass is_numeric
32+
* @throws Exception if $value is hex format
33+
* @throws Exception if $value is not a string or float
34+
* @throws Exception if $value overflow or underflows
35+
* @throws Exception if $value is less than $minValue
36+
* @throws Exception if $value is greater than $maxValue
3737
*/
3838
public static function filter($value, $allowNull = false, $minValue = null, $maxValue = null, $castInts = false)
3939
{
@@ -66,31 +66,31 @@ public static function filter($value, $allowNull = false, $minValue = null, $max
6666
$value = trim($value);
6767

6868
if (!is_numeric($value)) {
69-
throw new \Exception("{$value} does not pass is_numeric");
69+
throw new Exception("{$value} does not pass is_numeric");
7070
}
7171

7272
$value = strtolower($value);
7373

7474
//This is the only case (that we know of) where is_numeric does not return correctly castable float
7575
if (strpos($value, 'x') !== false) {
76-
throw new \Exception("{$value} is hex format");
76+
throw new Exception("{$value} is hex format");
7777
}
7878

7979
$valueFloat = (float)$value;
8080
} else {
81-
throw new \Exception('"' . var_export($value, true) . '" $value is not a string');
81+
throw new Exception('"' . var_export($value, true) . '" $value is not a string');
8282
}
8383

8484
if (is_infinite($valueFloat)) {
85-
throw new \Exception("{$value} overflow");
85+
throw new Exception("{$value} overflow");
8686
}
8787

8888
if ($minValue !== null && $valueFloat < $minValue) {
89-
throw new \Exception("{$valueFloat} is less than {$minValue}");
89+
throw new Exception("{$valueFloat} is less than {$minValue}");
9090
}
9191

9292
if ($maxValue !== null && $valueFloat > $maxValue) {
93-
throw new \Exception("{$valueFloat} is greater than {$maxValue}");
93+
throw new Exception("{$valueFloat} is greater than {$maxValue}");
9494
}
9595

9696
return $valueFloat;

src/Filter/Ints.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ final class Ints
2828
* @throws \InvalidArgumentException if $allowNull is not a boolean
2929
* @throws \InvalidArgumentException if $minValue is not null and not an int
3030
* @throws \InvalidArgumentException if $maxValue is not an int
31-
* @throws \Exception if $value string length is zero
32-
* @throws \Exception if $value does not contain all digits, optionally prepended by a '+' or '-' and optionally
31+
* @throws Exception if $value string length is zero
32+
* @throws Exception if $value does not contain all digits, optionally prepended by a '+' or '-' and optionally
3333
* surrounded by whitespace
34-
* @throws \Exception if $value was greater than a max int of PHP_INT_MAX
35-
* @throws \Exception if $value was less than a min int of ~PHP_INT_MAX
36-
* @throws \Exception if $value is not a string
37-
* @throws \Exception if $value is less than $minValue
38-
* @throws \Exception if $value is greater than $maxValue
34+
* @throws Exception if $value was greater than a max int of PHP_INT_MAX
35+
* @throws Exception if $value was less than a min int of ~PHP_INT_MAX
36+
* @throws Exception if $value is not a string
37+
* @throws Exception if $value is less than $minValue
38+
* @throws Exception if $value is greater than $maxValue
3939
*/
4040
public static function filter($value, $allowNull = false, $minValue = null, $maxValue = PHP_INT_MAX)
4141
{
@@ -62,7 +62,7 @@ public static function filter($value, $allowNull = false, $minValue = null, $max
6262
$value = trim($value);
6363

6464
if (strlen($value) === 0) {
65-
throw new \Exception('$value string length is zero');
65+
throw new Exception('$value string length is zero');
6666
}
6767

6868
$stringToCheckDigits = $value;
@@ -72,7 +72,7 @@ public static function filter($value, $allowNull = false, $minValue = null, $max
7272
}
7373

7474
if (!ctype_digit($stringToCheckDigits)) {
75-
throw new \Exception(
75+
throw new Exception(
7676
"{$value} does not contain all digits, optionally prepended by a '+' or '-' and optionally "
7777
. "surrounded by whitespace"
7878
);
@@ -83,24 +83,24 @@ public static function filter($value, $allowNull = false, $minValue = null, $max
8383
$casted = (int)$value;
8484

8585
if ($casted === PHP_INT_MAX && $value !== (string)PHP_INT_MAX) {
86-
throw new \Exception("{$value} was greater than a max int of " . PHP_INT_MAX);
86+
throw new Exception("{$value} was greater than a max int of " . PHP_INT_MAX);
8787
}
8888

8989
if ($casted === $phpIntMin && $value !== (string)$phpIntMin) {
90-
throw new \Exception("{$value} was less than a min int of {$phpIntMin}");
90+
throw new Exception("{$value} was less than a min int of {$phpIntMin}");
9191
}
9292

9393
$valueInt = $casted;
9494
} else {
95-
throw new \Exception('"' . var_export($value, true) . '" $value is not a string');
95+
throw new Exception('"' . var_export($value, true) . '" $value is not a string');
9696
}
9797

9898
if ($minValue !== null && $valueInt < $minValue) {
99-
throw new \Exception("{$valueInt} is less than {$minValue}");
99+
throw new Exception("{$valueInt} is less than {$minValue}");
100100
}
101101

102102
if ($valueInt > $maxValue) {
103-
throw new \Exception("{$valueInt} is greater than {$maxValue}");
103+
throw new Exception("{$valueInt} is greater than {$maxValue}");
104104
}
105105

106106
return $valueInt;

src/Filter/Strings.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class Strings
2424
* @param int $maxLength Maximum length to allow for $value.
2525
* @return string|null The passed in $value.
2626
*
27-
* @throws \Exception if the value did not pass validation.
27+
* @throws Exception if the value did not pass validation.
2828
* @throws \InvalidArgumentException if one of the parameters was not correctly typed.
2929
*/
3030
public static function filter($value, $allowNull = false, $minLength = 1, $maxLength = PHP_INT_MAX)
@@ -54,13 +54,13 @@ public static function filter($value, $allowNull = false, $minLength = 1, $maxLe
5454
}
5555

5656
if (!is_string($value)) {
57-
throw new \Exception("Value '" . var_export($value, true) . "' is not a string");
57+
throw new Exception("Value '" . var_export($value, true) . "' is not a string");
5858
}
5959

6060
$valueLength = strlen($value);
6161

6262
if ($valueLength < $minLength || $valueLength > $maxLength) {
63-
throw new \Exception(
63+
throw new Exception(
6464
sprintf(
6565
"Value '%s' with length '%d' is less than '%d' or greater than '%d'",
6666
$value,
@@ -83,13 +83,13 @@ public static function filter($value, $allowNull = false, $minLength = 1, $maxLe
8383
* @param string $delimiter The non-empty delimiter to explode on.
8484
* @return array The exploded values.
8585
*
86-
* @throws \Exception if the value is not a string.
86+
* @throws Exception if the value is not a string.
8787
* @throws \InvalidArgumentException if the delimiter does not pass validation.
8888
*/
8989
public static function explode($value, $delimiter = ',')
9090
{
9191
if (!is_string($value)) {
92-
throw new \Exception("Value '" . var_export($value, true) . "' is not a string");
92+
throw new Exception("Value '" . var_export($value, true) . "' is not a string");
9393
}
9494

9595
if (!is_string($delimiter) || empty($delimiter)) {

0 commit comments

Comments
 (0)