Git Product home page Git Product logo

sanitizer's Introduction

Travis Status Github Release Packagist License Packagist Downloads Github Issues Tips

Sanitizer Package

Sanitizers can be used to standardize data to ease validation, or provide data consistency.

Basic Usage

First construct a rules array.

$rules = [
    'name'      => 'trim',
    'email'     => 'trim|strtolower'
];

Rules can contain either callable functions, or the name of a sanitizer binding (more later). You can use either a pipe | or an array to specify multiple sanitization rules.

The sanitizer can be executed in the following fashion.

$sanitizer = new Sanitizer;
$sanitizer->sanitize($rules, $data);

Here's a full example.

// Construct rules array.
$rules = [
    'name'      => 'trim',
    'email'     => 'trim|strtolower'
];

// Data array to be sanitized.
$data = [
    'name' => ' Dayle ',
    'email' => ' [email protected]'
];

// Construct a new sanitizer.
$sanitizer = new Sanitizer;

// Execute the sanitizer.
$sanitizer->sanitize($rules, $data);

Here's the content of $data after execution.

[
    'name' => 'Dayle',
    'email' => '[email protected]'
]

Using the Laravel facade, the syntax can be made a little cleaner.

Sanitizer::sanitize($rules, $data);

Sanitize a single value like so.

$rules = 'trim|strtolower';
$data = '  Dayle';

Sanitizer::sanitizeValue($rules, $data);

Here is the value returned.

dayle

Custom Sanitization Rules

Sanitizers can be added multiple ways.

Using a Closure.

Sanitizer::register('reverse', function ($field) {
    return strrev($field);
});

Using a Callback.

Sanitizer::register('reverse', [new ClassHere, 'method']);

Using a class/method pair.

Sanitizer::register('reverse', 'Namespace\Class\Here@method');

The class will be resolved through an instance of the Illuminate IoC container, if no method is provided then sanitize() is assumed.

Installation

The Sanitizer package can be used stand-alone or with the Laravel Framework.

Stand-alone

First include the sanitizer package.

"daylerees/sanitizer": "dev-master"

Now simply use the Sanitizer class.

use Rees\Sanitizer\Sanitizer;

With Laravel

Include the Service Provider class within the app/config/app.php file.

'providers' => array(
    ...
    'Rees\Sanitizer\SanitizerServiceProvider'
)

Now simply add the facade alias.

'aliases' => array(
    ...
    'Sanitizer' => 'Rees\Sanitizer\Facade'
)

sanitizer's People

Contributors

daylerees avatar jotafurtado avatar joshhornby avatar noman2000 avatar schnoop avatar curranjensen avatar luisdalmolin avatar neilcrookes avatar timothyloyer avatar

Stargazers

Lungoupao Khongsai avatar

Watchers

James Cloos avatar  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.