2017 © Pedro Peláez
 

library sanitizer

Simple and stand-alone sanitizer library with no dependencies.

image

truongwp/sanitizer

Simple and stand-alone sanitizer library with no dependencies.

  • Thursday, June 1, 2017
  • by truongwp
  • Repository
  • 1 Watchers
  • 2 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Sanitizer

Sanitizer is a simple and stand-alone PHP sanitizer library with no dependencies., (*1)

Installation

Uses Composer to install and update:, (*2)

composer require "truongwp/sanitizer=*"

Sanitizer require PHP >= 5.3, (*3)

Usage

<?php
$sanitizer = new Truongwp\Sanitizer\Sanitizer();

$input = array(
    'name'     => '  Foo bar ',
    'age'      => ' 24 ',
);

$rules = array(
    'name'     => 'trim|strtolower|ucwords',
    'age'      => 'intval',
);

$output = $sanitizer->sanitize($input, $rules);

The $output:, (*4)

array(
    'name'     => 'Foo Bar',
    'age'      => 24,
);

Multiple rules can be passed as string delimiter by | or use an array:, (*5)

<?php
$rules = array(
    'name'     => array('trim', 'strtolower', 'ucwords'),
    'age'      => 'intval',
);

By default, rule name is PHP function. So you can easily add a custom function to sanitize., (*6)

<?php
function trim_slasses($value) {
    return trim($value, '/');
}

$sanitizer = new Truongwp\Sanitizer\Sanitizer();

$input = array(
    'name' => '//foo',
);
$rules = array(
    'name' => 'trim_slasses',
);
$output = $sanitizer->sanitize($input, $rules);

The result:, (*7)

array(
    'name' => 'foo',
)

If you want to pass additional parameters to sanitizer function, you can append them to rule name and delimited by :., (*8)

<?php
function prefix_suffix($value, $prefix = '', $suffix = '') {
    return $prefix . $value . $suffix;
}

$sanitizer = new Truongwp\Sanitizer\Sanitizer();

$input = array(
    'name' => 'foo',
);
$rules = array(
    'name' => 'prefix_suffix:prefix_:_suffix',
);
$output = $sanitizer->sanitize($input, $rules);

The result:, (*9)

array(
    'name' => 'prefix_foo_suffix',
)

You can also add custom sanitizer class by using SanitizerRegistry., (*10)

<?php
class DateFormatSanitizer implements Truongwp\Sanitizer\Contracts\RuleSanitizer
{
    /**
     * Sanitize value.
     *
     * @param mixed $value Value need to sanitize.
     * @return mixed
     */
    public function sanitize($value)
    {
        $args = func_get_args();
        $format = empty($args[1]) ? 'Y-m-d' : $args[1];

        $timestamp = strtotime($value);
        return date($format, $timestamp);
    }
}

// Register rule sanitizers.
Truongwp\Sanitizer\Registries\SanitizerRegistry::set('date_format', new DateFormatSanitizer());

$sanitizer = new Truongwp\Sanitizer\Sanitizer();

$input = array(
    'day' => '05/30/2017',
);
$rules = array(
    'name' => 'date_format:Y-m-d',
);
$output = $sanitizer->sanitize($input, $rules);

The result:, (*11)

array(
    'day' => '2017-05-30',
)

Contributing

Contributor: @truongwp, (*12)

Bug reports or Pull requests are welcome., (*13)

The Versions

01/06 2017

dev-master

9999999-dev

Simple and stand-alone sanitizer library with no dependencies.

  Sources   Download

MIT

The Requires

  • php >= 5.3.0

 

The Development Requires

php sanitize sanitizer sanitization

31/05 2017

0.1.0

0.1.0.0

Simple and stand-alone sanitizer library with no dependencies.

  Sources   Download

MIT

The Requires

  • php >= 5.3.0

 

The Development Requires

php sanitize sanitizer sanitization