PHP code example of jcergolj / request-input-transformer-for-laravel

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

    

jcergolj / request-input-transformer-for-laravel example snippets


protected function prepareForValidation(): void
{
    $this->merge([
        'email' => Str::of($this->email)->trim()->lower()->toString(),
        'first_name' => Str::of($this->first_name)->trim()->ucfirst()->toString(),
        'last_name' => Str::of($this->last_name)->trim()->ucfirst()->toString(),
        //...
    ]);
}

// Modifying the 'email' field
'email' => [new Trim(), new Lowercase()],

new AddFullName(),
new RemoveItemIfEmpty(),

'members.*.email' => [new Trim()],

namespace App\Http\Modifiers;

class Trim
{
    public function handle($value, $next)
    {
        return $next(trim($value));
    }
}



namespace App\Http\Transformers;

class FullNameTransformer
{
    public function handle($request, $next)
    {
        $request->merge([
            'full_name' => "{$request->first_name} {$request->last_name}",
        ]);

        return $next($request);
    }
}



use App\Http\Modifiers\Trim;
use App\Http\Modifiers\toLowercase;
use App\Http\Transformers\FullNameTransformer;
use Illuminate\Foundation\Http\FormRequest;
use Jcergolj\RequestPreprocessor\Facades\RequestPreprocessorFacade;

class MyFormRequest extends FormRequest
{
    protected function prepareForValidation()
    {
        RequestPreprocessorFacade::make($this, [
            'email'      => [new Trim(), new toLowercase(), // other modifiers],
            'first_name' => [new Trim()],
            'last_name'  => [new Trim()],
        ],
        [
            new FullNameTransformer(),
            /* other transformers */
        ])->apply();
    }
}

class MyFormRequest extends FormRequest
{
    public function modifiers(): array
    {
        return [
            'email' => [new Trim()],
        ];
    }

    public function transformers(): array
    {
        return [
            new FullNameTransformer(),
        ];
    }

    protected function prepareForValidation()
    {
        RequestPreprocessorFacade::make($this, $this->modifiers(), $this->transformers())->apply();
    }
}


public function test_my_form_request_defines_expected_modifiers()
{
    $request = new MyFormRequest();

    $this->assertEquals(
        [new Trim()],
        $request->modifiers()['email']
    );
}

use Jcergolj\RequestPreprocessor\Facades\RequestPreprocessorFacade;
use Tests\TestCase;

class MyFormRequestTest extends TestCase
{
    public function test_prepareForValidation_calls_preprocessor()
    {
        // Arrange: mock the facade
        RequestPreprocessorFacade::shouldReceive('make')
            ->once()
            ->withArgs(function ($request, $modifiers, $transformers) {
                // Optionally assert $request is the FormRequest instance
                // and $modifiers contain expected keys/classes
                return $request instanceof \App\Http\Requests\MyFormRequest
                    && isset($modifiers['email']);
            })
            ->andReturnSelf();

        RequestPreprocessorFacade::shouldReceive('apply')
            ->once()
            ->andReturnNull();

        // Act: instantiate and call prepareForValidation
        $request = new \App\Http\Requests\MyFormRequest();

        $reflection = new \ReflectionMethod($request, 'prepareForValidation');
        $reflection->setAccessible(true);
        $reflection->invoke($request);
    }
}
bash
php artisan make:modifier TrimModifier
php artisan make:transformer FullNameTransformer