-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ffe496d
Showing
40 changed files
with
3,673 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# Simple Input Sanitizer with auto Type-Casting | ||
|
||
The most sanitizer loop your filters and apply it on input-data. `This helper:` | ||
|
||
- loop your inputs-data and search for filter and apply it. | ||
- If no filter was found or not defined, auto Type-Casting will be apply on inputs-data. | ||
- You can defined keys, witch will returns from inputs-data. | ||
|
||
## Installation | ||
|
||
Run `composer command`: | ||
|
||
``` | ||
composer require brunogab/inputhelper: "dev-master" | ||
``` | ||
|
||
or add to your composer.json and run `composer update`: | ||
|
||
```json | ||
{ | ||
"require": { | ||
"brunogab/inputhelper": "dev-master" | ||
} | ||
} | ||
``` | ||
|
||
## usage and basic example in `example.php` | ||
|
||
> NOTICE:<br> | ||
> Some value cannot be decided between bool-type and interer/string type ("1", 1, 0, on, off..)<br> | ||
> Bool value check has a higher priority `by auto type-casting`:<br> | ||
> | ||
> - `1 is bool-true (not integer)` | ||
> - `0 is bool-false (not integer)` | ||
> - `on is bool-true (not string)` | ||
> - `off is bool-false (not integer)` | ||
> - `null is bool-false (not empty)` | ||
> - `empty is bool-false (not empty)` | ||
- Inputs must be Array | ||
- Filters can be String or Array or Closure or empty | ||
- Keys can be be String or Array or empty | ||
|
||
```php | ||
use Brunogab\InputHelper\InputHelper; | ||
|
||
/** Input Data */ | ||
$inputs = [ | ||
'col_a' => ' ColA ', | ||
'col_b' => ' COlB', | ||
'col_c' => ' colc* ', | ||
'col_d' => ' colD ', | ||
'col_int' => ' 22 ', //given as string with space | ||
'col_float' => ' 184.5 ', //given as string with space | ||
|
||
'col_bool_1' => '1', | ||
'col_bool_1_1' => 1, | ||
'col_bool_true' => 'true', | ||
'col_bool_on' => 'on', | ||
'col_bool_yes' => 'yes', | ||
'col_bool_0' => '0', | ||
'col_bool_0_0' => 0, | ||
'col_bool_false' => 'false', | ||
'col_bool_off' => 'off', | ||
'col_bool_no' => 'no', | ||
'col_bool_null' => null, | ||
'col_bool_empty' => '', | ||
]; | ||
``` | ||
|
||
- all these Filters Styles are valid: | ||
|
||
```php | ||
/** Filters */ | ||
$filters = [ | ||
'col_a' => ['trim', 'lower'], //array style | ||
'col_b' => 'trim|upper', //sting with a pipe style | ||
'col_c' => ['trim', 'lower', function ($value) { //closure in array style | ||
return str_replace('*', '+ replaced by closure', $value); | ||
}], | ||
'col_d' => function ($value) { //closure | ||
$value = trim($value); | ||
return ucfirst($value); | ||
}, | ||
'col_int' => 'int', | ||
'col_float' => 'dummy' //dummy is not valid filter -> auto type-casting | ||
//Notice: bool has no filter defined -> auto type-casting | ||
]; | ||
|
||
/** Filter can be String -> apply for all inputs-data */ | ||
$filters = 'trim'; | ||
|
||
/** Keys to return */ | ||
$keys = [ | ||
'col_a', | ||
'col_b', | ||
'asd', //not valid keys -> will be ignored | ||
]; | ||
|
||
/** Keys can be string*/ | ||
$keys = 'col_b'; | ||
|
||
$inputhelper = new InputHelper; | ||
$result = $inputhelper->run($inputs, $filters, $keys); | ||
|
||
//Result without Keys: | ||
array (size=18) | ||
'col_a' => string 'cola' (length=4) | ||
'col_b' => string 'COLB' (length=4) | ||
'col_c' => string 'colc+ replaced by closure' (length=25) | ||
'col_d' => string 'ColD' (length=4) | ||
'col_int' => int 22 | ||
'col_float' => float 184.5 | ||
'col_bool_1' => boolean true | ||
'col_bool_1_1' => boolean true | ||
'col_bool_true' => boolean true | ||
'col_bool_on' => boolean true | ||
'col_bool_yes' => boolean true | ||
'col_bool_0' => boolean false | ||
'col_bool_0_0' => boolean false | ||
'col_bool_false' => boolean false | ||
'col_bool_off' => boolean false | ||
'col_bool_no' => boolean false | ||
'col_bool_null' => boolean false | ||
'col_bool_empty' => boolean false | ||
``` | ||
|
||
## Custom Sanitizers | ||
|
||
You can write your custom Code and save into src/Filters/`Yourcodename`Filter.php | ||
|
||
```php | ||
namespace Brunogab\InputHelper\Filters; | ||
|
||
class YourcodenameFilter | ||
{ | ||
public function do($val) | ||
{ | ||
//custom logic | ||
return is_bool($val) ? 'Y' : 'N'; | ||
} | ||
} | ||
//then | ||
$filters = [ | ||
//simply use your custom filter | ||
'col_a' => 'yourcodename', | ||
]; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "brunogab/inputhelper", | ||
"description": "simple helper for sanitize and auto type-casting input data for php", | ||
"keywords": ["input filter", "custom input filter", "sanitation", "auto type-casting"], | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Brunner Gabor", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Brunogab\\InputHelper\\": "src/" | ||
} | ||
}, | ||
"minimum-stability": "dev" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Code Coverage for C:\wamp64\www\github-public\inputhelper\src\Filters\BoolFilter.php</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link href="../_css/bootstrap.min.css" rel="stylesheet" type="text/css"> | ||
<link href="../_css/octicons.css" rel="stylesheet" type="text/css"> | ||
<link href="../_css/style.css" rel="stylesheet" type="text/css"> | ||
<link href="../_css/custom.css" rel="stylesheet" type="text/css"> | ||
</head> | ||
<body> | ||
<header> | ||
<div class="container-fluid"> | ||
<div class="row"> | ||
<div class="col-md-12"> | ||
<nav aria-label="breadcrumb"> | ||
<ol class="breadcrumb"> | ||
<li class="breadcrumb-item"><a href="../index.html">C:\wamp64\www\github-public\inputhelper\src</a></li> | ||
<li class="breadcrumb-item"><a href="index.html">Filters</a></li> | ||
<li class="breadcrumb-item active">BoolFilter.php</li> | ||
|
||
</ol> | ||
</nav> | ||
</div> | ||
</div> | ||
</div> | ||
</header> | ||
<div class="container-fluid"> | ||
<div class="table-responsive"> | ||
<table class="table table-bordered"> | ||
<thead> | ||
<tr> | ||
<td> </td> | ||
<td colspan="10"><div align="center"><strong>Code Coverage</strong></div></td> | ||
</tr> | ||
<tr> | ||
<td> </td> | ||
<td colspan="3"><div align="center"><strong>Classes and Traits</strong></div></td> | ||
<td colspan="4"><div align="center"><strong>Functions and Methods</strong></div></td> | ||
<td colspan="3"><div align="center"><strong>Lines</strong></div></td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td class="success">Total</td> | ||
<td class="success big"> <div class="progress"> | ||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%"> | ||
<span class="sr-only">100.00% covered (success)</span> | ||
</div> | ||
</div> | ||
</td> | ||
<td class="success small"><div align="right">100.00%</div></td> | ||
<td class="success small"><div align="right">1 / 1</div></td> | ||
<td class="success big"> <div class="progress"> | ||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%"> | ||
<span class="sr-only">100.00% covered (success)</span> | ||
</div> | ||
</div> | ||
</td> | ||
<td class="success small"><div align="right">100.00%</div></td> | ||
<td class="success small"><div align="right">1 / 1</div></td> | ||
<td class="success small"><abbr title="Change Risk Anti-Patterns (CRAP) Index">CRAP</abbr></td> | ||
<td class="success big"> <div class="progress"> | ||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%"> | ||
<span class="sr-only">100.00% covered (success)</span> | ||
</div> | ||
</div> | ||
</td> | ||
<td class="success small"><div align="right">100.00%</div></td> | ||
<td class="success small"><div align="right">1 / 1</div></td> | ||
</tr> | ||
|
||
<tr> | ||
<td class="success"><abbr title="Brunogab\InputHelper\Filters\BoolFilter">BoolFilter</abbr></td> | ||
<td class="success big"> <div class="progress"> | ||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%"> | ||
<span class="sr-only">100.00% covered (success)</span> | ||
</div> | ||
</div> | ||
</td> | ||
<td class="success small"><div align="right">100.00%</div></td> | ||
<td class="success small"><div align="right">1 / 1</div></td> | ||
<td class="success big"> <div class="progress"> | ||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%"> | ||
<span class="sr-only">100.00% covered (success)</span> | ||
</div> | ||
</div> | ||
</td> | ||
<td class="success small"><div align="right">100.00%</div></td> | ||
<td class="success small"><div align="right">1 / 1</div></td> | ||
<td class="success small">1</td> | ||
<td class="success big"> <div class="progress"> | ||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%"> | ||
<span class="sr-only">100.00% covered (success)</span> | ||
</div> | ||
</div> | ||
</td> | ||
<td class="success small"><div align="right">100.00%</div></td> | ||
<td class="success small"><div align="right">1 / 1</div></td> | ||
</tr> | ||
|
||
<tr> | ||
<td class="success" colspan="4"> <a href="#11"><abbr title="do($val)">do</abbr></a></td> | ||
<td class="success big"> <div class="progress"> | ||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%"> | ||
<span class="sr-only">100.00% covered (success)</span> | ||
</div> | ||
</div> | ||
</td> | ||
<td class="success small"><div align="right">100.00%</div></td> | ||
<td class="success small"><div align="right">1 / 1</div></td> | ||
<td class="success small">1</td> | ||
<td class="success big"> <div class="progress"> | ||
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="100.00" aria-valuemin="0" aria-valuemax="100" style="width: 100.00%"> | ||
<span class="sr-only">100.00% covered (success)</span> | ||
</div> | ||
</div> | ||
</td> | ||
<td class="success small"><div align="right">100.00%</div></td> | ||
<td class="success small"><div align="right">1 / 1</div></td> | ||
</tr> | ||
|
||
|
||
</tbody> | ||
</table> | ||
</div> | ||
<table id="code" class="table table-borderless table-condensed"> | ||
<tbody> | ||
<tr><td><div align="right"><a name="1"></a><a href="#1">1</a></div></td><td class="codeLine"><span class="default"><?php</span></td></tr> | ||
<tr><td><div align="right"><a name="2"></a><a href="#2">2</a></div></td><td class="codeLine"></td></tr> | ||
<tr><td><div align="right"><a name="3"></a><a href="#3">3</a></div></td><td class="codeLine"><span class="keyword">namespace</span><span class="default"> </span><span class="default">Brunogab</span><span class="default">\</span><span class="default">InputHelper</span><span class="default">\</span><span class="default">Filters</span><span class="keyword">;</span></td></tr> | ||
<tr><td><div align="right"><a name="4"></a><a href="#4">4</a></div></td><td class="codeLine"></td></tr> | ||
<tr><td><div align="right"><a name="5"></a><a href="#5">5</a></div></td><td class="codeLine"><span class="keyword">class</span><span class="default"> </span><span class="default">BoolFilter</span></td></tr> | ||
<tr><td><div align="right"><a name="6"></a><a href="#6">6</a></div></td><td class="codeLine"><span class="keyword">{</span></td></tr> | ||
<tr><td><div align="right"><a name="7"></a><a href="#7">7</a></div></td><td class="codeLine"><span class="default"> </span><span class="comment">/**</span></td></tr> | ||
<tr><td><div align="right"><a name="8"></a><a href="#8">8</a></div></td><td class="codeLine"><span class="comment"> * @param string $val</span></td></tr> | ||
<tr><td><div align="right"><a name="9"></a><a href="#9">9</a></div></td><td class="codeLine"><span class="comment"> * @return boolean</span></td></tr> | ||
<tr><td><div align="right"><a name="10"></a><a href="#10">10</a></div></td><td class="codeLine"><span class="comment"> */</span></td></tr> | ||
<tr><td><div align="right"><a name="11"></a><a href="#11">11</a></div></td><td class="codeLine"><span class="default"> </span><span class="keyword">public</span><span class="default"> </span><span class="keyword">function</span><span class="default"> </span><span class="default">do</span><span class="keyword">(</span><span class="default">$val</span><span class="keyword">)</span></td></tr> | ||
<tr><td><div align="right"><a name="12"></a><a href="#12">12</a></div></td><td class="codeLine"><span class="default"> </span><span class="keyword">{</span></td></tr> | ||
<tr class="covered-by-large-tests popin"><td data-title="1 test covers line 13" data-content="<ul><li class="covered-by-large-tests">InputTest::test_inputs__filter_is_empty</li></ul>" data-placement="top" data-html="true"><div align="right"><a name="13"></a><a href="#13">13</a></div></td><td class="codeLine"><span class="default"> </span><span class="keyword">return</span><span class="default"> </span><span class="default">filter_var</span><span class="keyword">(</span><span class="default">$val</span><span class="keyword">,</span><span class="default"> </span><span class="default">FILTER_VALIDATE_BOOLEAN</span><span class="keyword">,</span><span class="default"> </span><span class="default">FILTER_NULL_ON_FAILURE</span><span class="keyword">)</span><span class="keyword">;</span></td></tr> | ||
<tr class="warning"><td><div align="right"><a name="14"></a><a href="#14">14</a></div></td><td class="codeLine"><span class="default"> </span><span class="keyword">}</span></td></tr> | ||
<tr><td><div align="right"><a name="15"></a><a href="#15">15</a></div></td><td class="codeLine"><span class="keyword">}</span></td></tr> | ||
|
||
</tbody> | ||
</table> | ||
<footer> | ||
<hr/> | ||
<h4>Legend</h4> | ||
<p> | ||
<span class="success"><strong>Executed</strong></span> | ||
<span class="danger"><strong>Not Executed</strong></span> | ||
<span class="warning"><strong>Dead Code</strong></span> | ||
</p> | ||
<p> | ||
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 8.0.0</a> using <a href="https://secure.php.net/" target="_top">PHP 7.4.2</a> with <a href="https://xdebug.org/">Xdebug 2.9.2</a> and <a href="https://phpunit.de/">PHPUnit 9.0.1</a> at Sun Mar 8 14:28:17 UTC 2020.</small> | ||
</p> | ||
<a title="Back to the top" id="toplink" href="#"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="16" viewBox="0 0 12 16"><path fill-rule="evenodd" d="M12 11L6 5l-6 6h12z"/></svg> | ||
</a> | ||
</footer> | ||
</div> | ||
<script src="../_js/jquery.min.js" type="text/javascript"></script> | ||
<script src="../_js/popper.min.js" type="text/javascript"></script> | ||
<script src="../_js/bootstrap.min.js" type="text/javascript"></script> | ||
<script src="../_js/file.js" type="text/javascript"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.