raditzfarhan / yii2-sanitizer
Yii2 Sanitizer is an easy way to sanitize or filter your inputs.
Installs: 23
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-11-15 19:21:44 UTC
README
Yii2 Sanitizer is an easy way to sanitize or filter your inputs for application that uses Yii2 framework.
Installation
The preferred way to install this extension is through composer.
Either run
composer require raditzfarhan/yii2-sanitizer "^1.0.0"
or add
"raditzfarhan/yii2-sanitizer": "^1.0.0"
to the require section of your composer.json
file.
Release Changes
NOTE: Refer the CHANGE LOG for details on changes to various releases.
Usage
Add this to the component section of your main config.
'components' => [ ... 'sanitizer' => [ 'class' => 'raditzfarhan\Yii2Sanitizer\Sanitize', ], ... ],
Examples
Filters an array on inputs
// data to be filtered $data = [ 'name' => ' Farhan"', 'address' => '<p>No 1, Residence ABC</p>', 'postcode' => '81221A', 'points' => '-152.1711B', 'status' => 'C1', ]; // create a filters corresponding to the input data array $filters = [ 'name' => ['trim', 'escape'], 'address' => ['trim', 'escape', 'cast:string'], 'postcode' => ['digit'], 'points' => ['digit'], 'status' => ['cast:int'], ]; // call sanitize function to filter an array of inputs $filtered_data = Yii::$app->sanitizer->sanitize($data, $filters); var_dump($filtered_data);
Results in:
[ 'name' => 'Farhan\\\"' 'address' => 'No 1, Residence ABC' 'postcode' => 81221 'points' => -152.1711 'status' => '1' ]
Usage is fairly simple. Your filter array needs to match the data array with the filters as the array value. You can combine the filters and it will be executed in order from left to right.
Filter single value
// call filter function to filter a single value. You can add filter type as the second argument. $filtered_data = Yii::$app->sanitizer->filter('<p>No 1, Residence ABC</p>', ['trim', 'cast:string']); echo $filtered_data;
Results in:
No 1, Residence ABC
The first argument would be the value that needs filtering. The second argument would be the filters that needs to be applied.
Available Filters
Here are the filters that you can use:
License
Yii2 Sanitizer is released under the MIT license