Skip to content

Commit 047bc08

Browse files
authored
Merge pull request #92 from chadicus/fea/throw-on-error
Add FilterOptions::THROW_ON_ERROR
2 parents 408c7c9 + e2e492f commit 047bc08

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"require-dev": {
4242
"php-coveralls/php-coveralls": "^2.0",
4343
"phpunit/phpunit": "^6.0",
44-
"squizlabs/php_codesniffer": "^3.2"
44+
"squizlabs/php_codesniffer": "^3.2",
45+
"symfony/yaml": "^3.4"
4546
},
4647
"autoload": {
4748
"psr-4": { "TraderInteractive\\": "src/" }

src/FilterOptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ final class FilterOptions
2828
* @var string
2929
*/
3030
const USES = 'uses';
31+
32+
/**
33+
* @var string
34+
*/
35+
const THROW_ON_ERROR = 'throwOnError';
3136
}

src/Filterer.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ final class Filterer implements FiltererInterface
6161
*/
6262
const RESPONSE_TYPE_FILTER = FilterResponse::class;
6363

64+
/**
65+
* @var string
66+
*/
67+
const INVALID_THROW_ON_ERROR_VALUE_ERROR_FORMAT = (
68+
FilterOptions::THROW_ON_ERROR . " for field '%s' was not a boolean value"
69+
);
70+
6471
/**
6572
* @var array
6673
*/
@@ -139,6 +146,7 @@ public function execute(array $input) : FilterResponse
139146
$filters = $this->specification[$field];
140147
self::assertFiltersIsAnArray($filters, $field);
141148
$customError = self::validateCustomError($filters, $field);
149+
$throwOnError = self::validateThrowOnError($filters, $field);
142150
unset($filters[FilterOptions::IS_REQUIRED]);//doesn't matter if required since we have this one
143151
unset($filters[FilterOptions::DEFAULT_VALUE]);//doesn't matter if there is a default since we have a value
144152
$conflicts = self::extractConflicts($filters, $field, $conflicts);
@@ -162,6 +170,10 @@ public function execute(array $input) : FilterResponse
162170
$this->addUsedInputToFilter($uses, $filteredInput, $field, $filter);
163171
$input = call_user_func_array($function, $filter);
164172
} catch (Exception $exception) {
173+
if ($throwOnError) {
174+
throw $exception;
175+
}
176+
165177
$errors = self::handleCustomError($field, $input, $exception, $errors, $customError);
166178
continue 2;//next field
167179
}
@@ -602,6 +614,24 @@ private static function assertFilterIsArray($filter, string $field)
602614
}
603615
}
604616

617+
private static function validateThrowOnError(array &$filters, string $field) : bool
618+
{
619+
if (!array_key_exists(FilterOptions::THROW_ON_ERROR, $filters)) {
620+
return false;
621+
}
622+
623+
$throwOnError = $filters[FilterOptions::THROW_ON_ERROR];
624+
if ($throwOnError !== true && $throwOnError !== false) {
625+
throw new InvalidArgumentException(
626+
sprintf(self::INVALID_THROW_ON_ERROR_VALUE_ERROR_FORMAT, $field)
627+
);
628+
}
629+
630+
unset($filters[FilterOptions::THROW_ON_ERROR]);
631+
632+
return $throwOnError;
633+
}
634+
605635
private static function validateCustomError(array &$filters, string $field)
606636
{
607637
$customError = null;

tests/FiltererTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use InvalidArgumentException;
77
use PHPUnit\Framework\TestCase;
8+
use RuntimeException;
89
use stdClass;
910
use Throwable;
1011
use TraderInteractive\Exceptions\FilterException;
@@ -380,6 +381,47 @@ function (int $input, int $fieldOneValue) : int {
380381
];
381382
}
382383

384+
/**
385+
* @test
386+
* @covers ::execute
387+
*/
388+
public function executeThrowsOnError()
389+
{
390+
$exception = new RuntimeException('the error');
391+
$this->expectException(RuntimeException::class);
392+
$this->expectExceptionMessage($exception->getMessage());
393+
$filter = function () use ($exception) {
394+
throw $exception;
395+
};
396+
397+
$specification = [
398+
'id' => [
399+
FilterOptions::THROW_ON_ERROR => true,
400+
[$filter],
401+
],
402+
];
403+
$filterer = new Filterer($specification);
404+
$filterer->execute(['id' => 1]);
405+
}
406+
407+
/**
408+
* @test
409+
* @covers ::execute
410+
*/
411+
public function executeValidatesThrowsOnError()
412+
{
413+
$this->expectException(InvalidArgumentException::class);
414+
$this->expectExceptionMessage(sprintf(Filterer::INVALID_THROW_ON_ERROR_VALUE_ERROR_FORMAT, 'id'));
415+
$specification = [
416+
'id' => [
417+
FilterOptions::THROW_ON_ERROR => 'abc',
418+
['uint'],
419+
],
420+
];
421+
$filterer = new Filterer($specification);
422+
$filterer->execute(['id' => 1]);
423+
}
424+
383425
/**
384426
* @test
385427
* @covers ::filter

0 commit comments

Comments
 (0)