PHP code example of arondeparon / laravel-request-sanitizer

1. Go to this page and download the library: Download arondeparon/laravel-request-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/ */

    

arondeparon / laravel-request-sanitizer example snippets


class StoreCustomerInformationRequest extends FormRequest
{
     use SanitizesInputs;
     
     protected $sanitizers = [
        'lastname' => [
            Capitalize::class,
        ],
        'mobile_phone' => [
            RemoveNonNumeric::class
        ],
     ];
}

class StoreUsersRequest extends FormRequest
{
    use SanitizesInputs;
    
    protected $sanitizers = [
        // Apply to all email fields in the users array
        'users.*.email' => [
            Lowercase::class,
            TrimDuplicateSpaces::class
        ],
        
        // Apply to all name fields in the users array
        'users.*.name' => [
            Capitalize::class
        ],
        
        // Multiple wildcards for deeply nested structures
        'departments.*.employees.*.email' => [
            Lowercase::class
        ]
    ];
}

$request = [
    'users' => [
        ['email' => '[email protected]', 'name' => 'john doe'],
        ['email' => '[email protected]', 'name' => 'jane smith']
    ],
    'departments' => [
        'sales' => [
            'employees' => [
                ['email' => '[email protected]'],
                ['email' => '[email protected]']
            ]
        ]
    ]
];

$sanitized = [
    'users' => [
        ['email' => '[email protected]', 'name' => 'John Doe'],
        ['email' => '[email protected]', 'name' => 'Jane Smith']
    ],
    'departments' => [
        'sales' => [
            'employees' => [
                ['email' => '[email protected]'],
                ['email' => '[email protected]']
            ]
        ]
    ]
];

 {
    protected $sanitizers = [
        'last_name' => [
            FilterVars::class => [
                'filter' => FILTER_SANITIZE_STRING,
                'options' => FILTER_FLAG_STRIP_BACKTICK
            ]
        ]
    ];
 }

interface Sanitizer
 {
     public function sanitize($input);
 }