A Laravel package for filtering and sanitizing request variables with a chainable, customizable filter system.
- Chain multiple filters (e.g.,
trim,uppercase,cast) in a single call. - Built-in filters for common tasks (e.g.,
strip_tags,escape,format_date). - Support for custom filters via configuration.
- Seamless integration with Laravel's service container and facade system.
- PHP: 8.3 or higher
- Laravel: 10.x, 11.x or 12.x
- Composer: Required for installation
Install the package via Composer:
composer require aporat/laravel-filter-varThe service provider (FilterVarServiceProvider) is automatically registered via Laravel’s package discovery. If you’ve disabled auto-discovery, add it manually to config/app.php:
'providers' => [
// ...
\Aporat\FilterVar\FilterVarServiceProvider::class,
],Optionally, register the facade for cleaner syntax:
'aliases' => [
// ...
'FilterVar' => Aporat\FilterVar\Laravel\Facades\FilterVar::class,
],Publish the configuration file to customize filters:
php artisan vendor:publish --provider="Aporat\FilterVar\Laravel\FilterVarServiceProvider" --tag="config"This copies config/filter-var.php to your Laravel config directory.
Filter and sanitize a request variable using the facade:
use Aporat\FilterVar\Laravel\Facades\FilterVar;
$userAgent = FilterVar::filterValue('cast:string|trim|strip_tags|escape', $request->header('User-Agent'));This:
- Casts the input to a string.
- Trims whitespace.
- Removes HTML/PHP tags.
- Escapes special HTML characters.
| Filter | Description | Example Input | Example Output |
|---|---|---|---|
capitalize |
Capitalizes words (title case) | hello world |
Hello World |
cast:<type> |
Casts to a type (e.g., int, string, bool) |
123.45 (cast:int) |
123 |
digit |
Extracts digits only | abc123xyz |
123 |
escape |
Escapes HTML special characters | <p>Hello &</p> |
<p>Hello &</p> |
filter_if |
Conditional check on array key/value | ['key' => 'val'] |
true/false |
format_date |
Reformats a date string | 2023-01-15 |
15/01/2023 |
lowercase |
Converts to lowercase | HELLO |
hello |
strip_tags |
Removes HTML/PHP tags | <b>Hello</b> |
Hello |
trim |
Trims whitespace | hello |
hello |
uppercase |
Converts to uppercase | hello |
HELLO |
validate_email |
Validates email format | [email protected] |
[email protected] |
validate_url |
Validates URL format | https://example.com |
https://example.com |
cast_to_boolean |
Casts input to boolean | true, false |
true, false |
sanitize_number_int |
Keeps only digits | abc123 |
123 |
sanitize_number_float |
Keeps digits and decimals | abc12.3xyz |
12.3 |
remove_whitespace |
Removes all whitespace | a b c |
abc |
slugify |
Converts string into URL-friendly slug | Hello World! |
hello-world |
normal_string |
Strips tags and keeps A-Z, 0-9, space, -:_. |
<script>alert(1)</script> |
alert1 |
Chain multiple filters using the | separator:
$result = FilterVar::filterValue('trim|uppercase|cast:string', ' hello world ');
// Returns: "HELLO WORLD"Add custom filters by editing config/filter-var.php:
return [
'custom_filters' => [
'media_real_id' => \App\Filters\MediaRealId::class,
],
];Define the custom filter class:
namespace App\Filters;
use Aporat\FilterVar\Contracts\Filter;
class MediaRealId implements Filter
{
public function apply(mixed $value, array $options = []): string
{
$value = (string) $value;
return str_contains($value, '_') ? explode('_', $value, 2)[0] : $value;
}
}Use it:
$result = FilterVar::filterValue('media_real_id', '11111_22222');
// Returns: "11111"Resolve from the container:
$result = app('filter-var')->filterValue('trim', ' hello ');
// Returns: "hello"Run the test suite:
composer testGenerate coverage reports:
composer test-coverageContributions are welcome! Please:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/amazing-feature). - Commit your changes (
git commit -m "Add amazing feature"). - Push to the branch (
git push origin feature/amazing-feature). - Open a pull request.
See CONTRIBUTING.md for details.
This package is open-sourced under the MIT License. See the License File for more information.
- Issues: GitHub Issues
- Source: GitHub Repository