2017 © Pedro Peláez
 

library php-sanitizer

PHP data sanitizer

image

gustavonecore/php-sanitizer

PHP data sanitizer

  • Monday, November 27, 2017
  • by gustavonecore
  • Repository
  • 2 Watchers
  • 3 Stars
  • 23 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 3 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

GCore Sanitizer

PHP sanitizer and validator

This is a library to sanitize your input from any source, using a predefined template of native types., (*1)

Why another sanitizer? Well, this is because I really want to maintain my toolset pretty small, and not depending on big libraries/frameworks., (*2)

Requirements

  • PHP >= v7.0

Install it with composer

composer require gustavonecore/php-sanitizer

Example of usage

Template definition First, you must define your template, (*3)

    $filter = new Gcore\Sanitizer\Template\TemplateSanitizer([
        'first_name' => 'string',
        'dob' => 'int',
        'numbers' => 'int[]',
        'test' => 'int',
        'email' => 'email',
        'email_wrong' => 'email',
        'double' => 'double',
        'boolean' => 'bool',
        'datetime' => 'datetime',
        "persons[]" => [
            'name' => 'string',
            'eyes' => 'int',
            'address' => 'string[]',
            'commits[]' => [
                'hash' => 'string',
                'n_comments' => 'int',
                'users' => 'string[]'
            ],
        ],
    ]);

Required fields You can force the sanitization process to require fields, you just need to include a ! at the end of the rule, e.g:, (*4)

    $filter = new Gcore\Sanitizer\Template\TemplateSanitizer([
        'email' => 'email!',
        'phone' => 'string!',
        'first_name' => 'string',
    ]);

If required fields is invalid If the input have required fileds with invalid data (null) the library will throw a Gcore\Sanitizer\Template\RequiredFieldsException, (*5)

Sanitize! After that, you are good to sanitize any input, (*6)

    // This will be your inut body from an user
    $input = [
        'first_name' => 'Gustavo',
        'dob' => '10',
        'numbers' => [1,2,3,'4','5'],
        'foo' => [1,2,4],
        'email' => 'gustavo.uach@gmail.com',
        'email_wrong' => 'gustavo.uachgmail.com',
        'double' => '7876',
        'boolean' => true,
        'datetime' => '2017-11-11 13:50:10',
        'persons' => [
            [
                'name' => 'jhon',
                'eyes' => 2,
                'address' => ['foo', 'bar', 'text'],
                'commits' => [
                    'hash' => '2221321n3kj12n3kj12n32j1',
                    'n_comments' => 900,
                    'users' => ['jhon', 'doe'],
                ],
            ],
                    [
                'name' => 'albert',
                'eyes' => 'wrong int here',
                'address' => ['a', 'b', 1],
                'commits' => null,
            ],
        ]
    ];

    // All your data is clean now! awesome!
    $cleanOutput = $filter->sanitize($input);

    print_r($cleanOutput);

Clean Output, (*7)

Output of the previous call, (*8)

    php examples/index.php
    Array
    (
        [first_name] => Gustavo
        [dob] => 10
        [numbers] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
                [4] => 5
            )

        [test] =>
        [email] => gustavo.uach@gmail.com
        [email_wrong] =>
        [double] => 7876
        [boolean] => 1
        [datetime] => DateTimeImmutable Object
            (
                [date] => 2017-11-11 13:50:10.000000
                [timezone_type] => 3
                [timezone] => America/Santiago
            )

        [persons] => Array
            (
                [0] => Array
                    (
                        [name] => jhon
                        [eyes] => 2
                        [address] => Array
                            (
                                [0] => foo
                                [1] => bar
                                [2] => text
                            )

                        [commits] => Array
                            (
                                [hash] => 2221321n3kj12n3kj12n32j1
                                [n_comments] => 900
                                [users] => Array
                                    (
                                        [0] => jhon
                                        [1] => doe
                                    )

                            )

                    )

                [1] => Array
                    (
                        [name] => albert
                        [eyes] =>
                        [address] => Array
                            (
                                [0] => a
                                [1] => b
                                [2] => 1
                            )

                        [commits] =>
                    )

            )

    )

Notes, (*9)

  • Not defined keys in the template will be ignored by the sanitizer.
  • If a value doesn't match the desired type, will be returned as null

TODO, (*10)

  • [x] Add unit tests. #1 (In progress)
  • [x] Add required fields.
  • [x] Create a new template method to define required fields.
    • [x] Improve this method to allow nested fields
  • [ ] Modularize a bit more the Strategy selector to allow extending the library with new types of data.

The Versions

27/11 2017

dev-master

9999999-dev

PHP data sanitizer

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Gustavo Delgado R.