Skip to content

Commit 8c638f7

Browse files
committed
Use FilterException in FIlterer
1 parent 321c71b commit 8c638f7

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/Filterer.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Throwable;
7+
use TraderInteractive\Exceptions\FilterException;
78

89
/**
910
* Class to filter an array of input.
@@ -233,7 +234,7 @@ public static function registerAlias($alias, callable $filter, bool $overwrite =
233234
*
234235
* @return array the filtered $values
235236
*
236-
* @throws Exception if any member of $values fails filtering
237+
* @throws FilterException if any member of $values fails filtering
237238
*/
238239
public static function ofScalars(array $values, array $filters) : array
239240
{
@@ -244,7 +245,7 @@ public static function ofScalars(array $values, array $filters) : array
244245

245246
list($status, $result, $error) = self::filter($wrappedFilters, $values);
246247
if (!$status) {
247-
throw new Exception($error);
248+
throw new FilterException($error);
248249
}
249250

250251
return $result;
@@ -282,7 +283,7 @@ public static function ofArrays(array $values, array $spec) : array
282283
}
283284

284285
if (!empty($errors)) {
285-
throw new Exception(implode("\n", $errors));
286+
throw new FilterException(implode("\n", $errors));
286287
}
287288

288289
return $results;
@@ -298,13 +299,13 @@ public static function ofArrays(array $values, array $spec) : array
298299
*
299300
* @return array the filtered $value
300301
*
301-
* @throws Exception if $value fails filtering
302+
* @throws FilterException if $value fails filtering
302303
*/
303304
public static function ofArray(array $value, array $spec) : array
304305
{
305306
list($status, $result, $error) = self::filter($spec, $value);
306307
if (!$status) {
307-
throw new Exception($error);
308+
throw new FilterException($error);
308309
}
309310

310311
return $result;

tests/FiltererTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use InvalidArgumentException;
77
use PHPUnit\Framework\TestCase;
88
use StdClass;
9+
use TraderInteractive\Exceptions\FilterException;
910

1011
/**
1112
* @coversDefaultClass \TraderInteractive\Filterer
@@ -488,7 +489,7 @@ public function ofScalarsFail()
488489
try {
489490
Filterer::ofScalars(['1', [], new \StdClass], [['string']]);
490491
$this->fail();
491-
} catch (Exception $e) {
492+
} catch (FilterException $e) {
492493
$expected = <<<TXT
493494
Field '1' with value 'array (
494495
)' failed filtering, message 'Value 'array (
@@ -549,7 +550,7 @@ public function ofArraysFail()
549550
['key' => [['string']]]
550551
);
551552
$this->fail();
552-
} catch (Exception $e) {
553+
} catch (FilterException $e) {
553554
$expected = <<<TXT
554555
Field 'key' with value 'stdClass::__set_state(array(
555556
))' failed filtering, message 'Value 'stdClass::__set_state(array(
@@ -606,7 +607,7 @@ public function ofArrayRequiredFail()
606607
try {
607608
Filterer::ofArray(['key1' => '1'], ['key1' => [['uint']], 'key2' => ['required' => true, ['uint']]]);
608609
$this->fail();
609-
} catch (Exception $e) {
610+
} catch (FilterException $e) {
610611
$expected = "Field 'key2' was required and not present";
611612
$this->assertSame($expected, $e->getMessage());
612613
}
@@ -621,7 +622,7 @@ public function ofArrayUnknown()
621622
try {
622623
Filterer::ofArray(['key' => '1'], ['key2' => [['uint']]]);
623624
$this->fail();
624-
} catch (Exception $e) {
625+
} catch (FilterException $e) {
625626
$expected = "Field 'key' with value '1' is unknown";
626627
$this->assertSame($expected, $e->getMessage());
627628
}

0 commit comments

Comments
 (0)