Skip to content

Commit

Permalink
Merge pull request #1 from dealnews/next
Browse files Browse the repository at this point in the history
Handle null returned from filter_input_array
  • Loading branch information
brianlmoon authored Mar 15, 2024
2 parents 16f3887 + afc32de commit 135c403
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,15 @@ public function var(mixed $value, int $filter = FILTER_DEFAULT, array|int $optio
* Wrapper for filter_input_array
*
* @see https://www.php.net/manual/en/function.filter-input-array.php
*
* @codeCoverageIgnore
*/
public function inputArray(int $type, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null {
return $this->varArray(filter_input_array($type), $options, $add_empty);
return $this->varArray(filter_input_array($type) ?? [], $options, $add_empty);
}

/**
* Wrapper for filter_input
*
* @see https://www.php.net/manual/en/function.filter-input.php
*
* @codeCoverageIgnore
*/
public function input(int $type, string $var_name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed {
return $this->var(filter_input($type, $var_name), $filter, $options);
Expand Down
23 changes: 23 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@

class FilterTest extends \PHPUnit\Framework\TestCase {

/**
* Test the input methods that they don't throw errors
*/
public function testInput() {
$f = Filter::init();
$value = $f->input(INPUT_POST, 'foo', FILTER_VALIDATE_INT);
$this->assertFalse($value);
}

/**
* Test the input methods that they don't throw errors
*/
public function testInputArray() {
$f = Filter::init();
$value = $f->inputArray(INPUT_POST, ['foo' => FILTER_VALIDATE_INT]);
$this->assertSame(
[
'foo' => null
],
$value
);
}

/**
* @dataProvider varArrayData
*/
Expand Down

0 comments on commit 135c403

Please sign in to comment.