1. Go to this page and download the library: Download tobento/service-sanitizer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Tobento\Service\Sanitizer\Sanitizer;
use Tobento\Service\Sanitizer\SanitizerInterface;
use Tobento\Service\Sanitizer\FiltersInterface;
class CustomDefaultFilters implements FiltersInterface
{
/**
* Add the filters to the sanitizer.
*
* @param SanitizerInterface $sanitizer
* @return void
*/
public function addFilters(SanitizerInterface $sanitizer): void
{
$sanitizer->addFilter('cast', new \Tobento\Service\Sanitizer\Filter\Cast());
}
}
$sanitizer = new Sanitizer(new CustomDefaultFilters());
use Tobento\Service\Sanitizer\Sanitizer;
use Tobento\Service\Sanitizer\FilterInterface;
$sanitizer = new Sanitizer();
// By a callable.
$sanitizer->addFilter('trim', function(mixed $value, array $parameters): mixed
{
return is_string($value) ? trim($value) : $value;
});
// By a class implementing the FilterInterface.
class TrimFilter implements FilterInterface
{
/**
* Apply the filter.
*
* @param mixed $value The value to sanitize
* @param array $parameters The parameters set on the sanitation 'filter:foo:bar'
*
* @throws FilterException If filter cannot handle sanitation
*
* @return mixed The sanitized value
*/
public function apply(mixed $value, array $parameters = []): mixed
{
return is_string($value) ? trim($value) : $value;
}
}
$sanitizer->addFilter('trim', new TrimFilter());
use Tobento\Service\Sanitizer\Sanitizer;
$sanitizer = new Sanitizer();
$sanitized = $sanitizer->sanitize(
[
'country' => 'CH',
'phone' => '+41 76 123 45 67',
],
[
// filter phone only if country value is "CH"
'phone' => 'filterIf:country:CH|digit',
],
returnSanitizedOnly: true,
);
var_dump($sanitized);
// array(1) { ["phone"]=> string(11) "41761234567" }
use Tobento\Service\Sanitizer\Sanitizer;
use Tobento\Service\Sanitizer\Filter\FilterIf;
use Tobento\Service\Collection\Collection;
// Filter only if the attributes defined are present.
class FilterIfPresent extends FilterIf
{
/**
* Apply the filter.
*
* @param mixed $value The value to sanitize
* @param array $parameters The parameters set on the sanitation 'filter:foo:bar'
*
* @throws FilterException If filter cannot handle sanitation
*
* @return mixed The sanitized value
*/
public function apply(mixed $value, array $parameters = []): mixed
{
// extract value and data.
[$value, $data] = $value;
if (! $data instanceof Collection)
{
return false;
}
return $data->has($parameters);
}
}
$sanitizer = new Sanitizer();
$sanitizer->addFilter('filterIfPresent', new FilterIfPresent());
$sanitized = $sanitizer->sanitize(
[
'country' => 'CH',
'locale' => 'de-CH',
'phone' => '+41 76 123 45 67',
],
[
// filter phone only if country and locale is present.
'phone' => 'filterIfPresent:country:locale|digit',
]
);
/*Array
(
[country] => CH
[locale] => de-CH
[phone] => 41761234567
)*/
use Tobento\Service\Sanitizer\Sanitizer;
use Tobento\Service\Sanitizer\FiltersParserInterface;
use Tobento\Service\Sanitizer\ParsedFilter;
class CustomParser implements FiltersParserInterface
{
/**
* Parses the filters.
*
* @param string|array
* @return array The parsed filters [ParsedFilter, ...]
*/
public function parse(string|array $filters): array
{
// do your parsing strategy
$parsedFilters = [];
return $parsedFilters;
}
}
$sanitizer = new Sanitizer(filtersParser: new CustomParser());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.