diff --git a/README.md b/README.md index 100f9a4..56d5a4e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,18 @@ Often, validating your request is not enough. The request sanitizer allows you t manipulate your form data before passing it to the validator. You can start using it in a matter of minutes and it is fully compatible with Laravel's `FormRequest` object. +## Table of Contents + + * [How to use](#how-to-use) + * [Installing](#installing) + * [Usage](#usage) + * [Predefined Sanitizers](#predefined-sanitizers) + + [FilterVars usage](#filtervars-usage) + * [Writing your own Sanitizer](#writing-your-own-sanitizer) + * [Testing](#testing) + * [Credits](#credits) + * [License](#license) + ## How to use Syntax is similar to the way `rules` are added to a [Form Request](https://laravel.com/docs/master/validation#form-request-validation). @@ -50,8 +62,27 @@ property of your form request. - [`Capitalize`](./src/Sanitizers/Capitalize.php) - capitalizes the first character of a string - [`Uppercase`](./src/Sanitizers/Uppercase.php) - converts a string to uppercase - [`Lowercase`](./src/Sanitizers/Lowercase.php) - converts a string to lowercasse +- [`FilterVars`](./src/Sanitizers/FilterVars.php) - simple php filter_vars sanitizer - Contributions are appreciated! +### FilterVars usage +The FilterVars sanitizer acts as a wrapper of the default PHP `filter_var` function. +It accepts the same (optional) parameters as the original function. +Both parameters can be either an `array` or `string` type: +```php + { + protected $sanitizers = [ + 'last_name' => [ + FilterVars::class => [ + 'filter' => FILTER_SANITIZE_STRING, + 'options' => FILTER_FLAG_STRIP_BACKTICK + ] + ] + ]; + } +``` +For more information on filter_vars please refer to https://www.php.net/manual/en/function.filter-var.php. + ## Writing your own Sanitizer Writing your own sanitizer can be done by implementing the `Sanitizer` interface, which requires only diff --git a/src/Sanitizers/FilterVars.php b/src/Sanitizers/FilterVars.php new file mode 100644 index 0000000..64df752 --- /dev/null +++ b/src/Sanitizers/FilterVars.php @@ -0,0 +1,27 @@ +filter = $filter; + $this->options = $options; + } + + /** + * @param $input + * @return string + */ + public function sanitize($input) + { + return filter_var($input, $this->filter, $this->options); + } + +} diff --git a/tests/Sanitizers/CapitalizeTest.php b/tests/Sanitizers/CapitalizeTest.php new file mode 100644 index 0000000..39c7e8c --- /dev/null +++ b/tests/Sanitizers/CapitalizeTest.php @@ -0,0 +1,16 @@ +sanitize('test'); + $this->assertEquals('Test', $output); + } +} diff --git a/tests/Sanitizers/FilterVarsTest.php b/tests/Sanitizers/FilterVarsTest.php new file mode 100644 index 0000000..3f3202d --- /dev/null +++ b/tests/Sanitizers/FilterVarsTest.php @@ -0,0 +1,34 @@ +sanitize(""); + $this->assertEquals('malicious code', $output); + } + + public function test_apply_default_filter_when_no_params_have_been_provided_in_filter_vars() + { + $sanitizer = new FilterVars(); + $output = $sanitizer->sanitize("no filter applied"); + $this->assertEquals('no filter applied', $output); + } + + public function test_pass_filter_options_to_filter_vars() + { + $filter = FILTER_SANITIZE_NUMBER_FLOAT; + $options = FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND; + $sanitizer = new FilterVars($filter, $options); + $output = $sanitizer->sanitize("442.34,34notallowed"); + $this->assertEquals("442.34,34", $output); + } +} \ No newline at end of file diff --git a/tests/Sanitizers/LowercaseTest.php b/tests/Sanitizers/LowercaseTest.php new file mode 100644 index 0000000..90c4768 --- /dev/null +++ b/tests/Sanitizers/LowercaseTest.php @@ -0,0 +1,16 @@ +sanitize('TEST'); + $this->assertEquals('test', $output); + } +} diff --git a/tests/Sanitizers/RemoveNonNumericTest.php b/tests/Sanitizers/RemoveNonNumericTest.php new file mode 100644 index 0000000..0ea3aff --- /dev/null +++ b/tests/Sanitizers/RemoveNonNumericTest.php @@ -0,0 +1,16 @@ +sanitize('test1234-134AC~'); + $this->assertEquals('1234134', $output); + } +} diff --git a/tests/Sanitizers/SanizerTest.php b/tests/Sanitizers/SanizerTest.php deleted file mode 100644 index 15909e5..0000000 --- a/tests/Sanitizers/SanizerTest.php +++ /dev/null @@ -1,56 +0,0 @@ -sanitize('test'); - $this->assertEquals('TEST', $output); - } - - public function test_lowercase_sanitizer() - { - $sanitizer = new Lowercase(); - $output = $sanitizer->sanitize('TEST'); - $this->assertEquals('test', $output); - } - - public function test_capitalize_sanitizer() - { - $sanitizer = new Capitalize(); - $output = $sanitizer->sanitize('test'); - $this->assertEquals('Test', $output); - } - - public function test_trim_sanitizer() - { - $sanitizer = new Trim(); - $output = $sanitizer->sanitize('test '); - $this->assertEquals('test', $output); - } - - public function test_trim_duplicate_spaces_sanitizer() - { - $sanitizer = new TrimDuplicateSpaces(); - $output = $sanitizer->sanitize('test '); - $this->assertEquals('test ', $output); - } - - public function test_remove_non_numeric_sanitizer() - { - $sanitizer = new RemoveNonNumeric(); - $output = $sanitizer->sanitize('test1234-134AC~'); - $this->assertEquals('1234134', $output); - } -} \ No newline at end of file diff --git a/tests/Sanitizers/TrimDuplicateSpacesTest.php b/tests/Sanitizers/TrimDuplicateSpacesTest.php new file mode 100644 index 0000000..f04f5dc --- /dev/null +++ b/tests/Sanitizers/TrimDuplicateSpacesTest.php @@ -0,0 +1,16 @@ +sanitize('test '); + $this->assertEquals('test ', $output); + } +} diff --git a/tests/Sanitizers/TrimTest.php b/tests/Sanitizers/TrimTest.php new file mode 100644 index 0000000..ed18d82 --- /dev/null +++ b/tests/Sanitizers/TrimTest.php @@ -0,0 +1,16 @@ +sanitize('test '); + $this->assertEquals('test', $output); + } +} diff --git a/tests/Sanitizers/UppercaseTest.php b/tests/Sanitizers/UppercaseTest.php new file mode 100644 index 0000000..d3747c2 --- /dev/null +++ b/tests/Sanitizers/UppercaseTest.php @@ -0,0 +1,16 @@ +sanitize('test'); + $this->assertEquals('TEST', $output); + } +}