Skip to content

Laravel package for filtering and sanitizing request variables

License

Notifications You must be signed in to change notification settings

aporat/laravel-filter-var

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Filter Var

Latest Stable Version Latest Dev Version Monthly Downloads Codecov Laravel Version License

A Laravel package for filtering and sanitizing request variables with a chainable, customizable filter system.

Features

  • 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.

Requirements

  • PHP: 8.2 or higher
  • Laravel: 10.x or 11.x
  • Composer: Required for installation

Installation

Install the package via Composer:

composer require aporat/laravel-filter-var

The 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\Laravel\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.

Usage

Basic Filtering

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.

Available Filters

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

Chaining Filters

Chain multiple filters using the | separator:

$result = FilterVar::filterValue('trim|uppercase|cast:string', '  hello world  ');
// Returns: "HELLO WORLD"

Custom Filters

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"

Using Without Facade

Resolve from the container:

$result = app('filter-var')->filterValue('trim', '  hello  ');
// Returns: "hello"

Testing

Run the test suite:

composer test

Generate coverage reports:

composer test-coverage

Contributing

Contributions are welcome! Please:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/amazing-feature).
  3. Commit your changes (git commit -m "Add amazing feature").
  4. Push to the branch (git push origin feature/amazing-feature).
  5. Open a pull request.

See CONTRIBUTING.md for details.

License

This package is open-sourced under the MIT License. See the License File for more information.

Support

About

Laravel package for filtering and sanitizing request variables

Resources

License

Stars

Watchers

Forks

Sponsor this project

Contributors 3

  •  
  •  
  •  

Languages