1.0.0
1.0.0.0Sanitor is a thin wrapper around PHPs sanitization functions filter_…
MIT
The Requires
- php >=5.4.0
The Development Requires
- php >=5.6.0
- ext-xdebug >=2.2.1
- phpunit/phpunit ^5.1
- phpunit/phpunit-skeleton-generator ^2.0
filter sanitization
Wallogit.com
2017 © Pedro Peláez
Sanitor is a thin wrapper around PHPs sanitization functions filter_var, filter_input and filter_has_var
Sanitor is a thin wrapper around PHP's sanitization functions filter_var, filter_input and filter_has_var., (*1)
Latest stable version: 1.1.1, (*3)
Sanitor works with PHP 5.4, 5.5., 5.6 and 7.0., (*4)
The package can be installed via composer:, (*5)
composer require broeser/sanitor, (*6)
IMPORTANT: Sanitor does only sanitization – never try to use it as a validation filter. It will not work as expected., (*7)
<?php
/*
* Example 1: Using filter()
*/
$value = 'mail@benedictroeser.de';
$sanitizer = new Sanitor\Sanitizer(FILTER_SANITIZE_EMAIL);
$sanitizedValue = $sanitizer->filter($value);
/*
* Example 2: Using filterPost()
*/
$sanitizer = new Sanitor\Sanitizer(FILTER_SANITIZE_EMAIL);
$email = $sanitizer->filterPost('email');
The constructor takes the filter as first argument and, optionally, flags as second argument. The FILTER_NULL_ON_FAILURE-flag, that is used internally is always set by default, so you don't have to set it., (*8)
List of important public methods of Sanitizer:, (*9)
If something went wrong while trying to filter, a SanitizationException is thrown. If anything else fails (e.g. a parameter was given in a different format than expected, a normal \Exception is thrown., (*10)
While usefulness might be debateable, you can change the filter and flags of an existing Sanitizer with the setSanitizeFilter(), setSanitizeFlags() and addSanitizeFlag()-methods., (*11)
If you'd like to sanitize objects, just let their class implement SanitizableInterface and use the SanitizableTrait within them. You have to implement getRawValue() to return the "raw", unfiltered value of your object and getSanitizer() to return the Sanitizer-class that shall be used to filter this value:, (*12)
<?php
/*
* Example 3: Using SanitizableInterface and SanitizableTrait
*/
class Email implements Sanitor\SanitizableInterface {
use Sanitor\SanitizableTrait;
public function getRawValue() {
return 'mail@benedictroeser.de';
}
public function getSanitizer() {
return new Sanitor\Sanitizer(FILTER_SANITIZE_EMAIL);
}
}
$myEmail = new Email();
$myFilteredEmail = $myEmail->getFilteredValue();
In case you prefer extending an abstract class, you can use AbstractSanitizable. That class (partly) implements SanitizableInterface and uses SanitizableTrait. It already contains a getSanitizer()-method returning $this->sanitizer, make sure to set it somewhere or override the method., (*13)
Yes, please!, (*14)
See CONTRIBUTING.md for details and/or open an issue with your questions., (*15)
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms., (*16)
It is a pun on sanitization / sane / janitor. Probably not a good one, though., (*17)
Sanitor is a thin wrapper around PHPs sanitization functions filter_…
MIT
filter sanitization