PHP code example of lukzgois / sanitizer

1. Go to this page and download the library: Download lukzgois/sanitizer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lukzgois / sanitizer example snippets



    
use Lukzgois\Sanitizer\Sanitizer;

class SanitizeRequest extends Sanitizer {

    public function rules()
    {
        return [
            'first_name' => 'trim|ucword',
            'last_name' => 'trim|ucwords'
        ];
    }
}



$data = ['name' => '  john doe '];
$sanitizer = new SanitizeRequest();
$sanitizedData = $sanitizer->sanitize($data);
var_dump($sanitizedData); // ['name' => 'John Doe']

    $sanitizer->sanitize($data, ['name' => 'strtoupper']);



use Lukzgois\Sanitizer\Sanitizer;

class SanitizeRequest extends Sanitizer {

    public function rules()
    {
        return [
            'phone' => 'phone',
        ];
    }
    
    public function sanitizePhone($value)
    {
        return str_replace('-','', $value);
    }
}



use Lukzgois\Sanitizer\Sanitizer;

class SanitizeRequest extends Sanitizer {

    public function rules()
    {
        return [
            'first_name' => '\App\Sanitizers\NameSanitizer', // sanitize()
            'last_name' => '\App\Sanitizers\NameSanitizer@lastName' // lastName()
        ];
    }
}



use Lukzgois\Sanitizer\Sanitizer;

class SanitizeRequest extends Sanitizer {

    public function rules()
    {
        return [
            'first_name' => '\App\Sanitizers\MinValueSanitizer:100', // sanitize()
        ];
    }

}

class \App\Sanitizers\MinValueSanitizer {

    public function sanitize($value, $min)
    {
        return $value < $min ? $min : $value;
    }

}



use Lukzgois\Sanitizer\Sanitizer;

class SanitizeRequest extends Sanitizer {

    public function rules()
    {
        return [
            'name' => 'trim|ucwords',
            'company' => 'default:1'
        ];
    }

}
    



$data = ['name' => '  john doe '];
$sanitizer = new SanitizeRequest();
$sanitizedData = $sanitizer->sanitize($data);
var_dump($sanitizedData); // ['name' => 'John Doe', 'company' => 1]



use Lukzgois\Sanitizer\Sanitizer;

class SanitizeRequest extends Sanitizer {

    public function rules()
    {
        return [
            'age' => 'cast:integer',
        ];
    }

}
    



$data = ['age' => '25'];
$sanitizer = new SanitizeRequest();
$sanitizedData = $sanitizer->sanitize($data);
var_dump($sanitizedData); // ['age' => (int)25]


    public function rules()
    {
        return [
            'age' => 'cast:integer:false',
        ];
    }
    

'Lukzgois\Sanitizer\Laravel\SanitizerServiceProvider',

 namespace App\Sanitizers;

use Lukzgois\Sanitizer\Sanitizer;

class CreateUserSanitizer extends Sanitizer {

    public function rules()
    {
        return [
        ];
    }

}

app/config/app.php
shell
php artisan make:sanitizer CreateUserSanitizer