Code Monkey home page Code Monkey logo

sanitizer's Introduction

sanitizer

GitHub release (latest by date) GitHub Workflow Status

Sanitization library for PHP and the Laravel framework.

Installation

composer require elegantweb/sanitizer

Usage

use Elegant\Sanitizer\Sanitizer;

$data = [
    'name' => ' sina ',
    'birthdate' => '06/25/1980',
    'email' => '[email protected]',
    'json' => '{"name":"value"}',
];

$filters = [
    'name' => 'trim|capitalize',
    'birthdate' => 'trim|format_date:"m/d/Y","F j, Y"',
    'email' => ['trim', 'lowercase'],
    'json' => 'cast:array',
];

$sanitizer = new Sanitizer($data, $filters);

var_dump($sanitizer->sanitize());

Will result in:

[
    'name' => 'Sina',
    'birthdate' => 'June 25, 1980',
    'email' => '[email protected]',
    'jsom' => ['name' => 'value'],
];

Laravel

In Laravel, you can use the Sanitizer through the Facade:

$newData = \Sanitizer::make($data, $filters)->sanitize();

You may also Sanitize input in your own FormRequests by using the SanitizesInput trait, and adding a filters method that returns the filters that you want applied to the input.

namespace App\Http\Requests;

use Elegant\Sanitizer\Laravel\SanitizesInput;

class MyAwesomeRequest extends Request
{
    use SanitizesInput;
    
    public function filters()
    {
        return [
            'name' => 'trim|capitalize',
        ];
    }
}

Available Filters

The following filters are available out of the box:

Filter Description
trim Trims a string
escape Escapes HTML and special chars using php's filter_var
lowercase Converts the given string to all lowercase
uppercase Converts the given string to all uppercase
capitalize Capitalize a string
cast Casts a variable into the given type. Options are: integer, float, string, boolean, object, array and Laravel Collection.
format_date Always takes two arguments, the date's given format and the target format, following DateTime notation.
strip_tags Strip HTML and PHP tags using php's strip_tags
digit Get only digit characters from the string

Custom Filters

It is possible to use a closure or name of a class that implements Elegant\Sanitizer\Contracts\Filter interface.

class RemoveStringsFilter implements \Elegant\Sanitizer\Contracts\Filter
{
    public function apply($value, array $options = [])
    {
        return str_replace($options, '', $value);
    }
}

$filters = [
    'remove_strings' => RemoveStringsFilter::class,
    'password' => fn ($value, array $options = []) => sha1($value),
];

$sanitize = new Sanitizer($data, $filters);

Laravel

You can easily extend the Sanitizer library by adding your own custom filters, just like you would the Validator library in Laravel, by calling extend from a ServiceProvider like so:

\Sanitizer::extend($filterName, $closureOrClassName);

Inspiration

sanitizer's People

Contributors

korridor avatar onlime avatar sharifzadesina avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.