2017 © Pedro Peláez
 

library filtry

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

image

mjarestad/filtry

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  • Wednesday, January 25, 2017
  • by mjarestad
  • Repository
  • 2 Watchers
  • 14 Stars
  • 20,797 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 9 Versions
  • 11 % Grown

The README.md

Filtry

Latest Stable Version Build Status License, (*1)

A package to filter and sanitize input data in Laravel 4/5 or as a standalone package. This is perfect to use with the Laravel 4 Validation class to filter data before validation. You can easily extend and create your own custom filters., (*2)

Requirements

php version > 5.6, (*3)

Installation

composer require mjarestad/filtry

Laravel

Add the ServiceProvider to the providers array in app/config/app.php, (*4)

Laravel 5:
Mjarestad\Filtry\FiltryServiceProviderLaravel5::class,

Laravel 4:
'Mjarestad\Filtry\FiltryServiceProvider',

Add the Facade to the aliases array in app/config/app.php, (*5)

'Filtry'  => 'Mjarestad\Filtry\Facades\Filtry',

Usage

Laravel 5

Form Requests

Extend your Form Request Validation classes with the provided Filtry Request to filter input data before validation., (*6)

<?php

use Mjarestad\Filtry\Http\Requests\Request;

class StorePostRequest extends Request
{
    public function rules()
    {
        return [
            'author' => 'required',
            'slug'   => 'required',
        ];
    }

    public function filters()
    {
        return [
            'author' => 'trim|ucwords',
            'slug'   => 'trim|replace:%,percent|slug',
        ];
    }
}

Laravel 4

Add a the filters property to your Eloquent Model or anywhere else you prefer., (*7)

<?php

class Post extends Eloquent {

    public static $filters = array(
        'author' => 'trim|ucwords',
        'slug'   => 'trim|replace:%,percent|slug'
    );

    public static $rules = array(
        'author' => 'required',
        'slug'   => 'required'
    );
}

In your controller or service call Filtry::make() and provide the data to filter and your filters array., (*8)

<?php

$filtry = Filtry::make(Input::all(), Post::$filters);

To get the filtered value use $filtry->getFiltered(), (*9)

<?php

$validator = Validator::make($filtry->getFiltered(), Post::$rules);

To get the unfiltered values, use:, (*10)

<?php

$filtry->getOld();

Every method can be used to filter a single value., (*11)

<?php

Filtry::trim('some string');
Filtry::slug('some string');
Filtry::snakeCase('some string');

Standalone

<?php

$filters = [
    'author' => 'trim|ucwords',
    'slug'   => 'trim|replace:%,percent|slug',
];

$data = [
    'author' => 'John Doe',
    'slug'   => 'My post title',
];

$filtry = new Mjarestad\Filtry\Filtry;
$filtry->make($data, $filters);
$filteredData = $filtry->getFiltered();

Create custom filters

Laravel

Extend with custom filters to use in Filtry::make() or as dynamic methods., (*12)

<?php

Filtry::extend('my_custom_filter', function ($data) {
    return str_replace('-', '_', $data);
});

Call the extended filter dynamically, (*13)

<?php

Filtry::myCustomFilter('some-custom-string');

Optional parameters

You can define optional parameters for your filter., (*14)

<?php

Filtry::extend('custom_filter', function ($data, $param1, $param2) {
    return $data . ($param1 + $param2);
});

And then add the parameters in your request:, (*15)

<?php

use Mjarestad\Filtry\Http\Requests\Request;

class StorePostRequest extends Request
{
    public function rules()
    {
        return [
            'author' => 'required',
        ];
    }

    public function filters()
    {
        return [
            'author' => 'custom_filter:1,2',
        ];
    }
}

It will concatenate 3 to your author attribute., (*16)

Standalone

<?php

$filtry = new Mjarestad\Filtry\Filtry;

$filtry->extend('my_custom_filter', function ($data) {
    return str_replace('-', '_', $data);
});

$filtry->myCustomFilter('some-custom-string');

Available filters

Core PHP filters

  • trim
  • ltrim
  • rtrim
  • lower (strtolower)
  • upper (strtoupper)
  • ucfirst
  • ucwords
  • stripslashes
  • replace:search,replace (str_replace)

Custom filters

  • xss_clean - clean string with htmlspecialchars
  • strip_whitespaces - strip all white spaces
  • strip_dashes - strip all dashes
  • slug - makes string url-friendly
  • prep_url - adds http:// if not present

Laravel filters

  • snake_case
  • camel_case
  • studly_case

Theese filters can still be used in a non-Laravel application., (*17)

The Versions

25/01 2017

dev-master

9999999-dev

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Mattias Jarestad

laravel filter

30/06 2016

v1.1.3

1.1.3.0

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Mattias Jarestad

laravel filter

23/06 2016

v1.1.2

1.1.2.0

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Mattias Jarestad

laravel filter

02/05 2016

v1.1.1

1.1.1.0

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Mattias Jarestad

laravel filter

29/04 2016

v1.1.0

1.1.0.0

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Mattias Jarestad

laravel filter

29/04 2016

v1.0.3

1.0.3.0

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Mattias Jarestad

laravel filter

29/04 2016

v1.0.2

1.0.2.0

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Mattias Jarestad

laravel filter

28/04 2016

v1.0.1

1.0.1.0

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Mattias Jarestad

laravel filter

28/04 2016

v1.0.0

1.0.0.0

Filter and sanitize input data in Laravel 4/5 or as a standalone package.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Mattias Jarestad

laravel filter