daylerees / sanitizer

Sanitize data using a number of mutation methods.
259 stars 22 forks source link

Support for wildcard (*) in the sanitization rules #22

Open sumitpore opened 5 years ago

sumitpore commented 5 years ago

This pull request aims to add a support for wildcards in the sanitization rules.

Adding a support for wildcards helps in performing sanitization on dynamic fields. Each star corresponds to one level of hierarchy.

Example of how it works:

$s = new Sanitizer;
$s->register('reverse', function($field) { return strrev($field); });

$d = [
    'users' =>[
        [
            'id' => 123,
            'name' => 'Sumit',
        ],
        [
            'id' => 456,
            'name' => 'David',
            'company' => 'abc'
        ]
    ]
];

$s->sanitize([
    'users.*.name' => 'reverse',
    'users.*.id' => 'reverse',
    '*.*.company' => 'strtoupper'
], $d);

The final data after sanitization will look like this

[
    'users' =>[
        [
            'id' => 321,
            'name' => 'timuS',
        ],
        [
            'id' => 654,
            'name' => 'divaD',
            'company' => 'ABC'
        ]
    ]
]