2017 © Pedro Peláez
 

library sanity

Array Sanitizers Package.

image

creativitykills/sanity

Array Sanitizers Package.

  • Tuesday, March 10, 2015
  • by CreativityKills
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PHP Array Sanitizer

Build Status, (*1)

This package is a Sanitizer for PHP arrays. It's best used to sanitize request inputs like input from $_POST and/or $_GET., (*2)

Usage

Step 1: Install via Composer

composer require 'creativitykills/sanity'

Step 2: Add the Service Provider

Open config/app.php and, to your "providers" array at the bottom, add:, (*3)

"CreativityKills\Sanity\SanityServiceProvider"

Sanitize Requests

Out of the box you can start running requests through the sanitizer., (*4)

<?php

// Possibly input from $_POST or $_GET global array
$someArray = ['name' => ' JOHN DOE ', 'email' => ' JOHN@DOE.COM '];

// Rules to validate against
$sanitizerRules = ['name' => 'ucwords|trim', 'email' => 'strtolower|trim'];

$sanitizer = new \CreativityKills\Sanity\Sanitizer;

$someArray = $sanitizer->sanitize($someArray, $sanitizerRules);

// array(
//    'name'  => 'John Doe',
//    'email' => 'john@doe.com'
// )
var_dump($someArray);

Custom Sanitizers

The sweet aspect of Sanity is extending the Sanity class. You can create a custom extension of the class., (*5)

<?php

use CreativityKills\Sanity\Sanitizer;

class UserSanitizer extends Sanitizer {

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

    public function sanitizeRemoveExcessWhiteSpaces($value)
    {
        return preg_replace('/\s+/', ' ', $value)
    }
}

Notice the custom remove_excess_white_spaces sanitizer is called from the method sanitizeRemoveExcessWhiteSpaces. All snake cased sanitizer rules are converted to camel cases., (*6)

Now we can call our custom sanitizer from our application like so:, (*7)

<?php

// Possibly input from $_POST or $_GET global array
$someArray = ['name' => ' JOHN   DOE ', 'email' => ' JOHN@DOE.COM '];

$someArray = (new UserSanitizer)->sanitize($someArray);

// array(
//    'name'  => 'John Doe',
//    'email' => 'john@doe.com'
// )
var_dump($someArray);

The Versions

10/03 2015

dev-master

9999999-dev

Array Sanitizers Package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Neo Ighodaro

laravel array creativitykills sanitizers

10/03 2015

1.0.0

1.0.0.0

Array Sanitizers Package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Neo Ighodaro

laravel array creativitykills sanitizers