diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4235211 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +vendor/ +.idea/ +.vs/ +.vscode/ +.DS_Store +composer.lock +debug.log \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b028952 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Fernando Zueet + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..48e518a --- /dev/null +++ b/README.md @@ -0,0 +1,778 @@ +# FZ PHP SANITIZE + +Sanitize php values. + +
+
+
+ +## Documentation + +- [Requirements](#requirements) +- [Installation](#installation) +- [Mode of use Array](#mode-of-use-array) +- [Mode of use Individual](#mode-of-use-individual) +- [Mode of use Laravel](#mode-of-use-laravel) +- [Custom filter](#custom-filter) +- [Filter striptags](#striptags) +- [Filter cnpj](#cnpj) +- [Filter cpf](#cpf) +- [Filter numeric](#numeric) +- [Filter alphanumeric](#alphanumeric) +- [Filter alpha](#alpha) +- [Filter url](#url) +- [Filter email](#email) +- [Filter strtolower](#strtolower) +- [Filter strtoupper](#strtoupper) +- [Filter ucwords](#ucwords) +- [Filter ucfirst](#ucfirst) +- [Filter lcfirst](#lcfirst) +- [Filter rtrim](#rtrim) +- [Filter ltrim](#ltrim) +- [Filter trim](#trim) +- [Filter date](#date) +- [Filter type](#type) +- [Filter numberFormat](#numberFormat) +- [Filter pregReplace](#pregReplace) +- [Filter filterVar](#filterVar) +- [Contributing](#contributing) +- [Security](#security) +- [Credits](#credits) +- [License](#license) + +
+
+
+ +## Requirements + +- PHP 7.3 or superior +- Composer + +
+
+
+ +## Installation + +Install this package with composer: + +```bash +composer require fernandozueet/php-sanitize +``` + +
+
+
+ +## Mode of use Array + +```php +use FzPhpSanitize\Sanitize; + +//values array +$data = [ + 'title' => 'Test Test é 123', + 'content' => "teste OK", + 'test' => "value test", + 'date' => "01/06/1987", + 'sub' => [ + "sub1" => " TEST " + ], +]; + +//rules sanitize +$rules = [ + 'title' => [Sanitize::strtolower(), Sanitize::alpha(true), Sanitize::strtoupper(), Sanitize::rtrim()], + 'content' => [Sanitize::stripTags('') ], + 'date' => [Sanitize::date('Y-m-d')], + 'sub.sub1' => [Sanitize::strtolower(), Sanitize::trim()], +]; + +//sanitize values +$values = Sanitize::clear($data, $rules); +``` +Output: + +```json +{ + "title": "TEST TEST", + "content": "teste OK", + "teste": "value test", + "date": "1987-06-01", + "sub": { + "sub1": "test" + } +} +``` + +
+
+
+ +## Mode of use Individual + +```php +use FzPhpSanitize\Sanitize; + +//sanitize +$value = Sanitize::cpf()->clean('43740999055'); +``` +Output: + +```json +437.409.990-55 +``` + +
+
+
+ +## Mode of use Laravel + +Laravel 8 or superior + +```php + [Sanitize::strtolower(), Sanitize::alpha(true), Sanitize::strtoupper(), Sanitize::rtrim()], + 'content' => [Sanitize::stripTags('') ], + ]; + + $this->merge(Sanitize::clear($this->input(), $rules)); + } + + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return true; + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return []; + } + +} +``` + +
+
+
+ +## Custom filter + +1- Create class filter + +```php +MyFilter.php + +options[0] ?? null) : ""; + } + +} +``` +2- Create a function in another pho file to call the filter + +```php +MySanitizes.php + +")->clean("
Link

Hello world!

"); +``` +Output: + +```json +Link Hello world! +``` + + +
+
+
+ +## Filters + +
+
+
+ +## striptags + +Strip HTML and PHP tags from a string. + +`striptags(array|string $allowable_tags = "")` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::striptags("")->clean("Link

Hello world!

"); +``` +Output: +```php +Link Hello world! +``` + +
+
+
+ +## cnpj + +Format the cnpj format number. + +`cnpj()` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::cnpj()->clean("54465939000150"); +``` +Output: + +```php +54.465.939/0001-50 +``` + +
+
+
+ +## cpf + +Format the cpf format number. + +`cpf()` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::cpf()->clean("43740999055"); +``` +Output: + +```php +437.409.990-55 +``` + +
+
+
+ +## numeric + +Numbers. + +`numeric()` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::numeric()->clean("asdfg123456"); +``` +Output: + +```php +123456 +``` + +
+
+
+ +## alphanumeric + +Letters from a to z and numbers. + +`alphanumeric(bool $spaces = false)` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::alphanumeric()->clean("!@#asdfg123456"); + +$value2 = Sanitize::alphanumeric(true)->clean("!@#asdfg 123 456"); +``` + +Output: + +```php +//value +asdfg123456 + +//value2 +asdfg 123 456 +``` + +
+
+
+ +## alpha + +Letters from a to z. + +`alpha(bool $spaces = false)` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::alpha()->clean("123456asdfg*&("); + +$value2 = Sanitize::alpha(true)->clean("123456asd dfg*&("); +``` + +Output: + +```php +//value +asdfg + +//value2 +asd dfg +``` + +
+
+
+ +## url + +filter_var FILTER_SANITIZE_URL + +`url()` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::url()->clean("http://php.net/manual/en/function.htmlentities.phpçù"); +``` + +Output: + +```php +http://php.net/manual/en/function.htmlentities.php +``` + +
+
+
+ +## email + +filter_var FILTER_SANITIZE_EMAIL + +`email()` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::email()->clean("çótest@test.com"); +``` + +Output: + +```php +test@test.com +``` + +
+
+
+ +## strtolower + +Make a string lowercase. + +`strtolower()` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::strtolower()->clean("FERNANDO ZUEET"); +``` + +Output: + +```php +fernando zueet +``` + +
+
+
+ +## strtoupper + +Make a string uppercase. + +`strtoupper()` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::strtoupper()->clean("fernando zueet"); +``` + +Output: + +```php +FERNANDO ZUEET +``` + +
+
+
+ +## ucwords + +Uppercase the first character of each word in a string. + +`ucwords(string $delimiters = " \t\r\n\f\v")` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::ucwords()->clean("fernando zueet"); +``` + +Output: + +```php +Fernando Zueet +``` + +
+
+
+ +## ucfirst + +Make a string's first character uppercase. + +`ucfirst()` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::ucfirst()->clean("fernando zueet"); +``` + +Output: + +```php +Fernando zueet +``` + +
+
+
+ +## lcfirst + +Make a string's first character lowercase. + +`lcfirst()` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::lcfirst()->clean("Fernando zueet"); +``` + +Output: + +```php +fernando zueet +``` + +
+
+
+ +## rtrim + +Removes blanks (or other characters) from the beginning of the string. + +`rtrim(string $charlist = " \t\n\r\0\x0B")` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::rtrim()->clean("fernando zueet "); +``` + +Output: + +```php +fernando zueet +``` + +
+
+
+ +## ltrim + +Removes blanks (or other characters) from the beginning of the string. + +`ltrim(string $charlist = " \t\n\r\0\x0B")` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::ltrim()->clean(" fernando zueet"); +``` + +Output: + +```php +fernando zueet +``` + +
+
+
+ +## trim + +Removing space at the beginning and end of a string. + +`trim(string $charlist = " \t\n\r\0\x0B")` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::trim()->clean(" fernando zueet "); +``` + +Output: + +```php +fernando zueet +``` + +
+
+
+ +## date + +Date format. + +`date(string $format = 'Y-m-d')` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::date("Y-m-d")->clean("01/06/1987"); +``` + +Output: + +```php +1987-06-01 +``` + +
+
+
+ +## type + +Format a types. + +`type(string $type)` + +$type: string bool int float array object + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::type('string')->clean(10); + +$value2 = Sanitize::type('bool')->clean('true'); + +$value3 = Sanitize::type('int')->clean('1234'); + +$value4 = Sanitize::type('float')->clean('100,5'); +``` + +Output: + +```php +//value +'10' + +//value2 +true + +//value3 +1234 + +//value4 +100.5 +``` + +
+
+
+ +## numberFormat + +Format a number with grouped thousands. + +`numberFormat(int $decimals = 0, string $decimalpoint = '.', string $separator = ',')` + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::numberFormat(2, ',', '.')->clean("1000"); +``` + +Output: + +```php +1.000,00 +``` + +
+
+
+ + +## pregReplace + +Perform a regular expression search and replace. + +`pregReplace($pattern, $replacement)` + +http://php.net/manual/en/function.preg-replace.php + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::pregReplace('/[^A-Za-z]/', '')->clean("1234asdfg"); +``` + +Output: + +```php +asdfg +``` + +
+
+
+ +## filterVar + +Filters a variable with a specified filter. + +`filterVar(int $filter = FILTER_DEFAULT, $options = null)` + +http://php.net/manual/en/function.filter-var.php + +```php +use FzPhpSanitize\Sanitize; + +$value = Sanitize::filterVar(FILTER_SANITIZE_EMAIL)->clean("çótest@test.com"); +``` + +```php +test@test.com +``` + +
+
+
+ + +## Contributing + +Please see [CONTRIBUTING](https://github.com/FernandoZueet/php-sanitize/graphs/contributors) for details. + +## Security + +If you discover security related issues, please email fernandozueet@hotmail.com instead of using the issue tracker. + +## Credits + +- [Fernando Zueet](https://github.com/FernandoZueet) + +## License + +The FZ Php Sanitize is licensed under the MIT license. See [License File](LICENSE.md) for more information. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..47826b6 --- /dev/null +++ b/composer.json @@ -0,0 +1,29 @@ +{ + "name": "fernandozueet/php-sanitize", + "type": "library", + "description": "Sanitize php values", + "keywords": ["sanitize", "sanitize array", "sanitize string", "php sanitize", "php sanitize array"], + "license": "MIT", + "authors": [{ + "name": "Fernando Zueet", + "email": "fernandozueet@hotmail.com", + "homepage": "https://github.com/FernandoZueet" + }], + "require": { + "php": ">=7.2", + "laravel/helpers": "^1.3" + }, + "require-dev": { + "phpunit/phpunit": "9.3" + }, + "autoload": { + "psr-4": { + "FzPhpSanitize\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..e704b93 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,23 @@ + + + + + ./tests/ + + + + + + + + + \ No newline at end of file diff --git a/src/Contracts/Filter.php b/src/Contracts/Filter.php new file mode 100644 index 0000000..4b392dc --- /dev/null +++ b/src/Contracts/Filter.php @@ -0,0 +1,25 @@ + + */ + +namespace FzPhpSanitize\Contracts; + +interface Filter +{ + /** + * Sanitize value + * + * @param mixed $value + * @return mixed + */ + public function clean($value); + +} \ No newline at end of file diff --git a/src/Filters/Alpha.php b/src/Filters/Alpha.php new file mode 100644 index 0000000..6c95805 --- /dev/null +++ b/src/Filters/Alpha.php @@ -0,0 +1,33 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Alpha extends Filters implements Filter +{ + /** + * Filter preg_replace. + * Perform a regular expression search and replace. + * + * @param string $value + * @return string + */ + public function clean($value) + { + $space = $this->options[0] ? "\s" : ""; + + return is_string($value) ? preg_replace("/[^[:alpha:]$space]+/", '', $value) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Alphanumeric.php b/src/Filters/Alphanumeric.php new file mode 100644 index 0000000..b683b2c --- /dev/null +++ b/src/Filters/Alphanumeric.php @@ -0,0 +1,33 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Alphanumeric extends Filters implements Filter +{ + /** + * Filter preg_replace. + * Perform a regular expression search and replace. + * + * @param string $value + * @return string + */ + public function clean($value) + { + $space = $this->options[0] ? "\s" : ""; + + return is_string($value) ? preg_replace("/[^[:alnum:]$space]/", '', $value) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Cnpj.php b/src/Filters/Cnpj.php new file mode 100644 index 0000000..aa9954d --- /dev/null +++ b/src/Filters/Cnpj.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Cnpj extends Filters implements Filter +{ + /** + * Filter preg_replace. + * Perform a regular expression search and replace. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? preg_replace('/^([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{2})$/', '$1.$2.$3/$4-$5', $value) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Cpf.php b/src/Filters/Cpf.php new file mode 100644 index 0000000..3e58804 --- /dev/null +++ b/src/Filters/Cpf.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Cpf extends Filters implements Filter +{ + /** + * Filter preg_replace. + * Perform a regular expression search and replace. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? preg_replace('/^([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{2})$/', '$1.$2.$3-$4', $value) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Date.php b/src/Filters/Date.php new file mode 100644 index 0000000..7be0121 --- /dev/null +++ b/src/Filters/Date.php @@ -0,0 +1,41 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Date extends Filters implements Filter +{ + /** + * Filter date. + * Date format + * + * @param string $value + * @return string + */ + public function clean($value) + { + if(!is_string($value)) { + return $value; + } + + try { + $data = new \DateTime(date(str_replace("/", "-", $value))); + return $data->format($this->options[0]); + + } catch (\Exception $e) { + return $value; + } + } + +} \ No newline at end of file diff --git a/src/Filters/Email.php b/src/Filters/Email.php new file mode 100644 index 0000000..bfe79e7 --- /dev/null +++ b/src/Filters/Email.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Email extends Filters implements Filter +{ + /** + * Filter email. + * Filter Sanitize email. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? filter_var($value, FILTER_SANITIZE_EMAIL) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/FilterVar.php b/src/Filters/FilterVar.php new file mode 100644 index 0000000..4d51bd4 --- /dev/null +++ b/src/Filters/FilterVar.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class FilterVar extends Filters implements Filter +{ + /** + * Filter var. + * Filters a variable with a specified filter. + * + * @param string $value + * @return mixed + */ + public function clean($value) + { + return filter_var($value, $this->options[0], $this->options[1] ?? null); + } + +} \ No newline at end of file diff --git a/src/Filters/Filters.php b/src/Filters/Filters.php new file mode 100644 index 0000000..0eb9cfd --- /dev/null +++ b/src/Filters/Filters.php @@ -0,0 +1,36 @@ + + */ + +namespace FzPhpSanitize\Filters; + +abstract class Filters +{ + /** + * Filter options + * + * @var array + */ + public $options = []; + + /** + * Class instance + * + * @param array ...$options + */ + public function __construct(...$options) + { + $this->options = $options; + + return $this; + } + +} \ No newline at end of file diff --git a/src/Filters/Lcfirst.php b/src/Filters/Lcfirst.php new file mode 100644 index 0000000..7ff311b --- /dev/null +++ b/src/Filters/Lcfirst.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Lcfirst extends Filters implements Filter +{ + /** + * Filter lcfirst. + * Make a string's first character lowercase. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? lcfirst($value) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Ltrim.php b/src/Filters/Ltrim.php new file mode 100644 index 0000000..f61478b --- /dev/null +++ b/src/Filters/Ltrim.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Ltrim extends Filters implements Filter +{ + /** + * Filter ltrim. + * Removes blanks (or other characters) from the beginning of the string. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? ltrim($value, $this->options[0]) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/NumberFormat.php b/src/Filters/NumberFormat.php new file mode 100644 index 0000000..bb268b6 --- /dev/null +++ b/src/Filters/NumberFormat.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class NumberFormat extends Filters implements Filter +{ + /** + * Filter number format. + * Format a number with grouped thousands. + * + * @param float $value + * @return string + */ + public function clean($value) + { + return number_format($value, $this->options[0], $this->options[1], $this->options[2]); + } + +} \ No newline at end of file diff --git a/src/Filters/Numeric.php b/src/Filters/Numeric.php new file mode 100644 index 0000000..c7b0df9 --- /dev/null +++ b/src/Filters/Numeric.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Numeric extends Filters implements Filter +{ + /** + * Filter preg_replace. + * Perform a regular expression search and replace. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? preg_replace('/[^0-9]/', '', $value) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/PregReplace.php b/src/Filters/PregReplace.php new file mode 100644 index 0000000..cb336bb --- /dev/null +++ b/src/Filters/PregReplace.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class PregReplace extends Filters implements Filter +{ + /** + * Filter preg_replace. + * Perform a regular expression search and replace. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return preg_replace($this->options[0], $this->options[1], $value); + } + +} \ No newline at end of file diff --git a/src/Filters/Rtrim.php b/src/Filters/Rtrim.php new file mode 100644 index 0000000..13ac621 --- /dev/null +++ b/src/Filters/Rtrim.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Rtrim extends Filters implements Filter +{ + /** + * Filter rtrim. + * Removes blanks (or other characters) from the end of the string. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? rtrim($value, $this->options[0]) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Striptags.php b/src/Filters/Striptags.php new file mode 100644 index 0000000..fe30bf3 --- /dev/null +++ b/src/Filters/Striptags.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Striptags extends Filters implements Filter +{ + /** + * Filter strip tags. + * Strip HTML and PHP tags from a string. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? strip_tags($value, $this->options[0] ?? null) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Strtolower.php b/src/Filters/Strtolower.php new file mode 100644 index 0000000..4569c0e --- /dev/null +++ b/src/Filters/Strtolower.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Strtolower extends Filters implements Filter +{ + /** + * Filter strtolower. + * Make a string lowercase. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? strtolower($value) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Strtoupper.php b/src/Filters/Strtoupper.php new file mode 100644 index 0000000..be96441 --- /dev/null +++ b/src/Filters/Strtoupper.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Strtoupper extends Filters implements Filter +{ + /** + * Filter strtoupper. + * Make a string uppercase. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? strtoupper($value) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Trim.php b/src/Filters/Trim.php new file mode 100644 index 0000000..576c7f0 --- /dev/null +++ b/src/Filters/Trim.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Trim extends Filters implements Filter +{ + /** + * Filter trim. + * Removing space at the beginning and end of a string. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? trim($value, $this->options[0]) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Type.php b/src/Filters/Type.php new file mode 100644 index 0000000..1cd5d69 --- /dev/null +++ b/src/Filters/Type.php @@ -0,0 +1,60 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Type extends Filters implements Filter +{ + /** + * Types values + * + * @param mixed $value + * @return mixed + */ + public function clean($value) + { + if($this->options[0] == 'string') { + return (string) $value; + } + + if($this->options[0] == 'bool') { + if($value === 'true') { + return true; + } + if($value === 'false') { + return false; + } + return (bool) $value; + } + + if($this->options[0] == 'int') { + return (int) $value; + } + + if($this->options[0] == 'float') { + return (float) $value; + } + + if($this->options[0] == 'array') { + return is_array($value) || is_object($value) ? (array) $value : ""; + } + + if($this->options[0] == 'object') { + return is_array($value) || is_object($value) ? (object) $value : ""; + } + + return ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Ucfirst.php b/src/Filters/Ucfirst.php new file mode 100644 index 0000000..02a6f34 --- /dev/null +++ b/src/Filters/Ucfirst.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Ucfirst extends Filters implements Filter +{ + /** + * Filter ucfirst. + * Make a string's first character uppercase. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? ucfirst($value) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Ucwords.php b/src/Filters/Ucwords.php new file mode 100644 index 0000000..52c388e --- /dev/null +++ b/src/Filters/Ucwords.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Ucwords extends Filters implements Filter +{ + /** + * Filter ucwords. + * Uppercase the first character of each word in a string. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? ucwords($value, $this->options[0]) : ""; + } + +} \ No newline at end of file diff --git a/src/Filters/Url.php b/src/Filters/Url.php new file mode 100644 index 0000000..c3a1594 --- /dev/null +++ b/src/Filters/Url.php @@ -0,0 +1,31 @@ + + */ + +namespace FzPhpSanitize\Filters; + +use FzPhpSanitize\Contracts\Filter; + +class Url extends Filters implements Filter +{ + /** + * Filter url. + * Filter Sanitize url. + * + * @param string $value + * @return string + */ + public function clean($value) + { + return is_string($value) ? filter_var($value, FILTER_SANITIZE_URL) : ""; + } + +} \ No newline at end of file diff --git a/src/Sanitize.php b/src/Sanitize.php new file mode 100644 index 0000000..3e9c497 --- /dev/null +++ b/src/Sanitize.php @@ -0,0 +1,344 @@ + + */ + +namespace FzPhpSanitize; + +use FzPhpSanitize\Filters\Cpf; +use FzPhpSanitize\Filters\Cnpj; +use FzPhpSanitize\Filters\Date; +use FzPhpSanitize\Filters\Trim; +use FzPhpSanitize\Filters\Alpha; +use FzPhpSanitize\Filters\Email; +use FzPhpSanitize\Filters\Ltrim; +use FzPhpSanitize\Filters\Rtrim; +use FzPhpSanitize\Filters\Lcfirst; +use FzPhpSanitize\Filters\Numeric; +use FzPhpSanitize\Filters\Ucfirst; +use FzPhpSanitize\Filters\Ucwords; +use FzPhpSanitize\Filters\FilterVar; +use FzPhpSanitize\Filters\Striptags; +use FzPhpSanitize\Filters\Strtolower; +use FzPhpSanitize\Filters\Strtoupper; +use FzPhpSanitize\Filters\PregReplace; +use FzPhpSanitize\Filters\Alphanumeric; +use FzPhpSanitize\Filters\NumberFormat; +use FzPhpSanitize\Filters\Type; +use FzPhpSanitize\Filters\Url; + +class Sanitize +{ + /** + * Sanitize array values + * + * @param array $data + * @param array $rules + * @return array + */ + public static function clear(array $data, array $rules): array + { + foreach ($rules as $key => $value) { + foreach ($value as $key2 => $value2) { + data_set($data, $key, $value2->clean(data_get($data, $key))); + } + } + + return $data; + } + + //------------------------------------------------------------ + //FILTERS + //------------------------------------------------------------ + + /** + * Filter strtolower. + * Make a string lowercase. + * + * @link http://php.net/manual/en/function.strtolower.php + * + * @return Strtolower + */ + public static function strtolower(): Strtolower + { + return new Strtolower(); + } + + /** + * Filter strtoupper. + * Make a string uppercase. + * + * @link http://php.net/manual/en/function.strtoupper.php + * + * @return Strtoupper + */ + public static function strtoupper(): Strtoupper + { + return new Strtoupper(); + } + + /** + * Filter ucwords. + * Uppercase the first character of each word in a string. + * + * @link http://php.net/manual/en/function.ucwords.php + * + * @param string $delimiters + * @return Ucwords + */ + public static function ucwords(string $delimiters = " \t\r\n\f\v"): Ucwords + { + return new Ucwords($delimiters); + } + + /** + * Filter ucfirst. + * Make a string's first character uppercase. + * + * @link http://php.net/manual/en/function.ucfirst.php + * + * @return Ucfirst + */ + public static function ucfirst(): Ucfirst + { + return new Ucfirst(); + } + + /** + * Filter lcfirst. + * Make a string's first character lowercase. + * + * @link http://php.net/manual/en/function.lcfirst.php + * + * @return Lcfirst + */ + public static function lcfirst(): Lcfirst + { + return new Lcfirst(); + } + + /** + * Filter strip_tags. + * Strip HTML and PHP tags from a string. + * + * @link http://php.net/manual/en/function.strip-tags.php + * + * @param array|string $allowable_tags + * @return Striptags + */ + public static function striptags($allowable_tags = ""): Striptags + { + return new Striptags($allowable_tags); + } + + /** + * Filter preg_replace. + * Perform a regular expression search and replace. + * + * @link http://php.net/manual/en/function.preg-replace.php + * + * @param mixed $patterns + * @param mixed $replacements + * @return PregReplace + */ + public static function pregReplace($pattern, $replacement): PregReplace + { + return new PregReplace($pattern, $replacement); + } + + /** + * Filter cnpj. + * + * @link http://php.net/manual/en/function.preg-replace.php + * + * @return Cnpj + */ + public static function cnpj(): Cnpj + { + return new Cnpj(); + } + + /** + * Filter cpf. + * + * @link http://php.net/manual/en/function.preg-replace.php + * + * @return Cpf + */ + public static function cpf(): Cpf + { + return new Cpf(); + } + + /** + * Filter alpha. + * Letters from a to z. + * + * @link http://php.net/manual/en/function.preg-replace.php + * + * @param bool $spaces + * @return Alpha + */ + public static function alpha(bool $spaces = false): Alpha + { + return new Alpha($spaces); + } + + /** + * Filter alphanumeric. + * Letters from a to z and numbers. + * + * @link http://php.net/manual/en/function.preg-replace.php + * + * @param bool $spaces + * @return Alphanumeric + */ + public static function alphanumeric(bool $spaces = false): Alphanumeric + { + return new Alphanumeric($spaces); + } + + /** + * Filter numeric. + * + * @link http://php.net/manual/en/function.preg-replace.php + * + * @return Numeric + */ + public static function numeric(): Numeric + { + return new Numeric(); + } + + /** + * Filter trim. + * Removing space at the beginning and end of a string. + * + * @link http://php.net/manual/en/function.trim.php + * + * @param string $charlist + * @return Trim + */ + public static function trim(string $charlist = " \t\n\r\0\x0B"): Trim + { + return new Trim($charlist); + } + + /** + * Filter ltrim. + * Removes blanks (or other characters) from the beginning of the string. + * + * @link http://php.net/manual/en/function.ltrim.php + * + * @param string $charlist + * @return Ltrim + */ + public static function ltrim(string $charlist = " \t\n\r\0\x0B"): Ltrim + { + return new Ltrim($charlist); + } + + /** + * Filter rtrim. + * Removes blanks (or other characters) from the end of the string. + * + * @link http://php.net/manual/en/function.rtrim.php + * + * @param string $charlist + * @return Rtrim + */ + public static function rtrim(string $charlist = " \t\n\r\0\x0B"): Rtrim + { + return new Rtrim($charlist); + } + + /** + * Filter var. + * Filters a variable with a specified filter. + * + * @link http://php.net/manual/en/function.filter-var.php + * + * @param integer $filter + * @param mixed $options + * @return FilterVar + */ + public static function filterVar(int $filter = FILTER_DEFAULT, $options = null): FilterVar + { + return new FilterVar($filter, $options); + } + + /** + * Filter email. + * Filter Sanitize email. + * + * @link http://php.net/manual/en/function.filter-var.php + * + * @return Email + */ + public static function email(): Email + { + return new Email(); + } + + /** + * Filter url. + * Filter Sanitize url. + * + * @link http://php.net/manual/en/function.filter-var.php + * + * @return Url + */ + public static function url(): Url + { + return new Url(); + } + + /** + * Filter date. + * Date format. + * + * @link https://php.net/manual/en/class.datetime.php + * + * @param string $format + * @return Date + */ + public static function date(string $format = 'Y-m-d'): Date + { + return new Date($format); + } + + /** + * Filter number format. + * Format a number with grouped thousands. + * + * @link http://php.net/manual/en/function.number-format.php + * + * @param int $decimals + * @param string $decimalapoint + * @param string $separator + * @return NumberFormat + */ + public static function numberFormat(int $decimals = 0, string $decimalpoint = '.', string $separator = ','): NumberFormat + { + return new NumberFormat($decimals, $decimalpoint, $separator); + } + + /** + * Types values + * + * @param string $type string bool int float array object + * @return Type + */ + public static function type(string $type): Type + { + return new Type($type); + } + +} \ No newline at end of file diff --git a/tests/Filters/AlphaTest.php b/tests/Filters/AlphaTest.php new file mode 100644 index 0000000..edb1167 --- /dev/null +++ b/tests/Filters/AlphaTest.php @@ -0,0 +1,34 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class AlphaTest extends TestCase +{ + /** + * Alpha format + * + * @return void + */ + public function testAlphaSanitizer() + { + $this->assertEquals('asdfg', Sanitize::alpha()->clean('123456asdfg*&(')); + + $this->assertEquals('as dfg', Sanitize::alpha(true)->clean('123456as dfg*&(')); + + $this->assertEquals('', Sanitize::alpha()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/AlphanumericTest.php b/tests/Filters/AlphanumericTest.php new file mode 100644 index 0000000..3e6edcf --- /dev/null +++ b/tests/Filters/AlphanumericTest.php @@ -0,0 +1,34 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class AlphanumericTest extends TestCase +{ + /** + * Alphanumeric format + * + * @return void + */ + public function testAlphanumericSanitizer() + { + $this->assertEquals('asdfg123456', Sanitize::alphanumeric()->clean('!@#asdfg123456')); + + $this->assertEquals('asdfg 123 456', Sanitize::alphanumeric(true)->clean('!@#asdfg 123 456')); + + $this->assertEquals('', Sanitize::alphanumeric()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/CnpjTest.php b/tests/Filters/CnpjTest.php new file mode 100644 index 0000000..d0f6a55 --- /dev/null +++ b/tests/Filters/CnpjTest.php @@ -0,0 +1,32 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class CnpjTest extends TestCase +{ + /** + * Cnpj format + * + * @return void + */ + public function testCnpjSanitizer() + { + $this->assertEquals('54.465.939/0001-50', Sanitize::cnpj()->clean('54465939000150')); + + $this->assertEquals('', Sanitize::cnpj()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/CpfTest.php b/tests/Filters/CpfTest.php new file mode 100644 index 0000000..17680aa --- /dev/null +++ b/tests/Filters/CpfTest.php @@ -0,0 +1,32 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class CpfTest extends TestCase +{ + /** + * Cpf filter + * + * @return void + */ + public function testCpfSanitizer() + { + $this->assertEquals('437.409.990-55', Sanitize::cpf()->clean('43740999055')); + + $this->assertEquals('', Sanitize::cpf()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/DateTest.php b/tests/Filters/DateTest.php new file mode 100644 index 0000000..90393ca --- /dev/null +++ b/tests/Filters/DateTest.php @@ -0,0 +1,53 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class DateTest extends TestCase +{ + public function values() : array + { + return [ + ["Y-m-d", "01/06/1987", "1987-06-01"], + ["d-m-Y", "1987-06-01", "01-06-1987"], + ["Y", "1987-06-01", "1987"], + ["Y", "01/06/1987", "1987"], + ["Y-m-d H:i:s", "01/06/1987 09:04:56", "1987-06-01 09:04:56"], + ["Y-m-d", "01/06/1987 09:04:56", "1987-06-01"], + ["H:i:s", "01/06/1987 09:04:56", "09:04:56"], + ["Y-m-d", "", date("Y-m-d")], + ["Y-m-d", "dfsafads", "dfsafads"], + ["Y-m-d", "01/20/1987", "01/20/1987"], + ["Y-m-d", 10, 10], + ]; + } + + /** + * Filter date. + * Date format. + * + * @dataProvider values + * @param string $format + * @param string $value + * @param string $expected + * + * @return void + */ + public function testDateSanitizer(string $format, $value, $expected) + { + $this->assertEquals($expected, Sanitize::date($format)->clean($value)); + } + +} \ No newline at end of file diff --git a/tests/Filters/EmailTest.php b/tests/Filters/EmailTest.php new file mode 100644 index 0000000..ce33a04 --- /dev/null +++ b/tests/Filters/EmailTest.php @@ -0,0 +1,32 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class EmailTest extends TestCase +{ + /** + * Filter email. + * + * @return void + */ + public function testEmailSanitizer() + { + $this->assertEquals('test@test.com', Sanitize::email()->clean('çótest@test.com')); + + $this->assertEquals('', Sanitize::email()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/FilterVarTest.php b/tests/Filters/FilterVarTest.php new file mode 100644 index 0000000..4993dbb --- /dev/null +++ b/tests/Filters/FilterVarTest.php @@ -0,0 +1,31 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class FilterVarTest extends TestCase +{ + /** + * Filter var. + * Filters a variable with a specified filter. + * + * @return void + */ + public function testFilterVarSanitizer() + { + $this->assertEquals('test@test.com', Sanitize::filterVar(FILTER_SANITIZE_EMAIL)->clean('çótest@test.com')); + } + +} \ No newline at end of file diff --git a/tests/Filters/LcfirstTest.php b/tests/Filters/LcfirstTest.php new file mode 100644 index 0000000..6e36f44 --- /dev/null +++ b/tests/Filters/LcfirstTest.php @@ -0,0 +1,33 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class LcfirstTest extends TestCase +{ + /** + * Filter lcfirst. + * Make a string's first character lowercase. + * + * @return void + */ + public function testLcfirstSanitizer() + { + $this->assertEquals('fernando zueet', Sanitize::lcfirst()->clean('Fernando zueet')); + + $this->assertEquals("", Sanitize::lcfirst()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/LtrimTest.php b/tests/Filters/LtrimTest.php new file mode 100644 index 0000000..0908441 --- /dev/null +++ b/tests/Filters/LtrimTest.php @@ -0,0 +1,33 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class LtrimTest extends TestCase +{ + /** + * Filter ltrim. + * Removes blanks (or other characters) from the beginning of the string. + * + * @return void + */ + public function testLtrimSanitizer() + { + $this->assertEquals('fernando zueet', Sanitize::ltrim()->clean(' fernando zueet')); + + $this->assertEquals('', Sanitize::ltrim()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/NumberFormatTest.php b/tests/Filters/NumberFormatTest.php new file mode 100644 index 0000000..1f1a9a0 --- /dev/null +++ b/tests/Filters/NumberFormatTest.php @@ -0,0 +1,31 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class NumberFormatTest extends TestCase +{ + /** + * Filter number format. + * Format a number with grouped thousands. + * + * @return void + */ + public function testNumberFormatSanitizer() + { + $this->assertEquals('1.000,00', Sanitize::numberFormat(2, ',', '.')->clean('1000')); + } + +} \ No newline at end of file diff --git a/tests/Filters/NumericTest.php b/tests/Filters/NumericTest.php new file mode 100644 index 0000000..b446fe5 --- /dev/null +++ b/tests/Filters/NumericTest.php @@ -0,0 +1,32 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class NumericTest extends TestCase +{ + /** + * Numeric format + * + * @return void + */ + public function testNumericSanitizer() + { + $this->assertEquals('123456', Sanitize::numeric()->clean('asdfg123456')); + + $this->assertEquals('', Sanitize::numeric()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/PregReplaceTest.php b/tests/Filters/PregReplaceTest.php new file mode 100644 index 0000000..db1c73a --- /dev/null +++ b/tests/Filters/PregReplaceTest.php @@ -0,0 +1,30 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class PregReplaceTest extends TestCase +{ + /** + * Make a string lowercase + * + * @return void + */ + public function testPregReplaceSanitizer() + { + $this->assertEquals('asdfg', Sanitize::pregReplace('/[^A-Za-z]/', '')->clean('1234asdfg')); + } + +} \ No newline at end of file diff --git a/tests/Filters/RtrimTest.php b/tests/Filters/RtrimTest.php new file mode 100644 index 0000000..d5ea3be --- /dev/null +++ b/tests/Filters/RtrimTest.php @@ -0,0 +1,33 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class RtrimTest extends TestCase +{ + /** + * Filter rtrim. + * Removes blanks (or other characters) from the end of the string. + * + * @return void + */ + public function testRtrimSanitizer() + { + $this->assertEquals('fernando zueet', Sanitize::rtrim()->clean('fernando zueet ')); + + $this->assertEquals('', Sanitize::rtrim()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/StriptagsTest.php b/tests/Filters/StriptagsTest.php new file mode 100644 index 0000000..214de67 --- /dev/null +++ b/tests/Filters/StriptagsTest.php @@ -0,0 +1,44 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class StriptagsTest extends TestCase +{ + public function values() : array + { + return [ + ["Link

Hello world!

", "", "Link Hello world!"], + ["Link

Hello world!

", "", "Link Hello world!"], + ["Link

Hello world!

", ['',''], "Link Hello world!"], + [10, "", ""], + ]; + } + + /** + * Strip HTML and PHP tags from a string. + * + * @dataProvider values + * @param string $value + * @param array|string $tags + * @param string $expected + * @return void + */ + public function testStriptagsSanitizer($value, $tags, string $expected) + { + $this->assertEquals($expected, Sanitize::striptags($tags)->clean($value)); + } + +} \ No newline at end of file diff --git a/tests/Filters/StrtolowerTest.php b/tests/Filters/StrtolowerTest.php new file mode 100644 index 0000000..06e0471 --- /dev/null +++ b/tests/Filters/StrtolowerTest.php @@ -0,0 +1,33 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class StrtolowerTest extends TestCase +{ + /** + * Filter strtolower. + * Make a string lowercase + * + * @return void + */ + public function testStrtolowerSanitizer() + { + $this->assertEquals('fernando zueet', Sanitize::strtolower()->clean('FERNANDO ZUEET')); + + $this->assertEquals('', Sanitize::strtolower()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/StrtoupperTest.php b/tests/Filters/StrtoupperTest.php new file mode 100644 index 0000000..773cceb --- /dev/null +++ b/tests/Filters/StrtoupperTest.php @@ -0,0 +1,33 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class StrtoupperTest extends TestCase +{ + /** + * Filter strtoupper. + * Make a string uppercase. + * + * @return void + */ + public function testStrtoupperSanitizer() + { + $this->assertEquals('FERNANDO ZUEET', Sanitize::strtoupper()->clean('fernando zueet')); + + $this->assertEquals('', Sanitize::strtoupper()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/TrimTest.php b/tests/Filters/TrimTest.php new file mode 100644 index 0000000..5c049f6 --- /dev/null +++ b/tests/Filters/TrimTest.php @@ -0,0 +1,33 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class TrimTest extends TestCase +{ + /** + * Filter trim. + * Removing space at the beginning and end of a string. + * + * @return void + */ + public function testTrimSanitizer() + { + $this->assertEquals('fernando zueet', Sanitize::trim()->clean(' fernando zueet ')); + + $this->assertEquals('', Sanitize::trim()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/TypeTest.php b/tests/Filters/TypeTest.php new file mode 100644 index 0000000..c32135c --- /dev/null +++ b/tests/Filters/TypeTest.php @@ -0,0 +1,78 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; +use stdClass; + +class TypeTest extends TestCase +{ + public function values() : array + { + $object = (object) [ + 'level1' => [ + 'level2' => 'value' + ] + ]; + + return [ + //string + [10, "string", "10"], + [true, "string", "1"], + [false, "string", ""], + ["test", "string", "test"], + + //bool + ["true", "bool", true], + ["false", "bool", false], + ["test test", "bool", true], + [1, "bool", true], + [0, "bool", false], + + //int + ["1234", "int", 1234], + [true, "int", 1], + [false, "int", 0], + ['test test', "int", 0], + [123, "int", 123], + + //float + ["100.5", "float", 100.5], + [100.5, "float", 100.5], + + //array + [['test' => 'value test'], "array", ['test' => 'value test']], + [$object, "array", ['level1' => ['level2' => 'value']]], + + //object + [$object, "object", $object], + [['level1' => ['level2' => 'value']], "object", $object], + ]; + } + + /** + * Filter types values. + * + * @dataProvider values + * @param mixed $value + * @param string $type + * @param mixed $expected + * @return void + */ + public function testTypeSanitizer($value, string $type, $expected) + { + $this->assertEquals($expected, Sanitize::type($type)->clean($value)); + } + +} \ No newline at end of file diff --git a/tests/Filters/UcfirstTest.php b/tests/Filters/UcfirstTest.php new file mode 100644 index 0000000..eb182d9 --- /dev/null +++ b/tests/Filters/UcfirstTest.php @@ -0,0 +1,33 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class UcfirstTest extends TestCase +{ + /** + * Filter ucfirst. + * Make a string's first character uppercase. + * + * @return void + */ + public function testUcfirstSanitizer() + { + $this->assertEquals('Fernando zueet', Sanitize::ucfirst()->clean('fernando zueet')); + + $this->assertEquals('', Sanitize::ucfirst()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/UcwordsTest.php b/tests/Filters/UcwordsTest.php new file mode 100644 index 0000000..e4d8b04 --- /dev/null +++ b/tests/Filters/UcwordsTest.php @@ -0,0 +1,33 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class UcwordsTest extends TestCase +{ + /** + * Filter ucwords. + * Uppercase the first character of each word in a string. + * + * @return void + */ + public function testUcwordsSanitizer() + { + $this->assertEquals('Fernando Zueet', Sanitize::ucwords()->clean('fernando zueet')); + + $this->assertEquals('', Sanitize::ucwords()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/Filters/UrlTest.php b/tests/Filters/UrlTest.php new file mode 100644 index 0000000..9d9a3ca --- /dev/null +++ b/tests/Filters/UrlTest.php @@ -0,0 +1,32 @@ + + */ + +namespace Tests\Filters; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class UrlTest extends TestCase +{ + /** + * Filter url. + * + * @return void + */ + public function testUrlSanitizer() + { + $this->assertEquals('http://php.net/manual/en/function.htmlentities.php', Sanitize::url()->clean('http://php.net/manual/en/function.htmlentities.phpçù')); + + $this->assertEquals('', Sanitize::url()->clean(10)); + } + +} \ No newline at end of file diff --git a/tests/FiltersTest.php b/tests/FiltersTest.php new file mode 100644 index 0000000..5b07565 --- /dev/null +++ b/tests/FiltersTest.php @@ -0,0 +1,55 @@ + + */ + +namespace Tests; + +use FzPhpSanitize\Sanitize; +use PHPUnit\Framework\TestCase; + +class FiltersTest extends TestCase +{ + /** + * Array sanitize + */ + public function testFilterArraySanitize() + { + $data = [ + 'title' => 'Test Test é 123', + 'content' => "teste OK", + 'test' => "value test", + 'date' => "01/06/1987", + 'sub' => [ + "sub1" => " TEST " + ], + ]; + + $rules = [ + 'title' => [Sanitize::strtolower(), Sanitize::alpha(true), Sanitize::strtoupper(), Sanitize::rtrim()], + 'content' => [Sanitize::stripTags('') ], + 'date' => [Sanitize::date('Y-m-d')], + 'sub.sub1' => [Sanitize::strtolower(), Sanitize::trim()], + ]; + + $expected = [ + 'title' => "TEST TEST", + 'content' => "teste OK", + 'test' => "value test", + 'date' => "1987-06-01", + 'sub' => [ + 'sub1' => 'test' + ], + ]; + + $this->assertEquals($expected, Sanitize::clear($data, $rules)); + } + +} \ No newline at end of file