shetabit/transform-request

Transform laravel requests

Maintainers

Package info

github.com/shetabit/transform-request

pkg:composer/shetabit/transform-request

Transparency log

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v2.0 2026-08-17 07:43 UTC

This package is auto-updated.

Last update: 2026-08-17 07:45:25 UTC


README

Transform laravel requests

Software License Latest Version on Packagist Total Downloads on Packagist Tests Code Style Static Analysis Code Coverage

you can normalize or change request data structure with transformers.

This package supports PHP 8.4+ and Laravel 12 and 13.

lets normalize our data in transformers and let controllers to be much more cleaner and smaller.

List of contents

Install

Via Composer

$ composer require shetabit/transform-request

How to use

Create a new data transformer

we use transformers to transform request data.

you can run the below command in your console in order to create a new data transformer named TestTransformer.

$ composer php artisan make:transformer TestTransformer

all transformers will be created in App\Http\Transformers path.

Transformer example:

in all transformers, the transform method will transform your data into your ideal one. for example we can write the below code in it:

namespace App\Http\Transformers;

use Shetabit\Transformer\Contracts\TransformerInterface;

class TestTransformer implements TransformerInterface
{
    /**
     * transform given data
     *
     * @param array $data
     * @return array
     */
    public function transform(array $data) : array {
        /*
            input data :		
            [
                'n' => 'mahdi',
                'f' => 'khanzadi'
            ]

            transformed data:
            [
                'name' => 'mahdi',
                'family' => 'khanzadi',
                'username' => 'mahdikhanzadi'
            ]
        */

        return [
            'name' => $data['n'] ?? null,
            'family' => $data['f'] ?? null,
            'username' => ($data['n'] ?? null).($data['f'] ?? null)
        ];
    }
}

Transform requests

we can use a transformer to transform requests like the below:

namespace App\Http\Controllers;

use App\Http\Transformers\TestTransformer;
use Illuminate\Http\Request;

class TestController extends Controller
{
    public function __invoke(Request $request) {
        $data = $request->transform()->get(new TestTransformer());
        
        print_r($data)
    }
}

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email khanzadimahdi@gmail.com instead of using the issue tracker.

Credits

Testing

Every pull request and every push to master is checked by GitHub Actions: the test suite runs on PHP 8.4 and 8.5 against Laravel 12 and 13 (both the lowest and the highest supported dependencies), the coding style is checked with PHP_CodeSniffer, the sources are analysed with PHPStan and the code coverage is measured.

The feature tests send real requests through a Laravel application built by Orchestra Testbench, and the make:transformer command is run and its output checked.

composer install

composer test           # run the test suite
composer test-coverage  # run the test suite and report code coverage
composer check-style    # check the coding style
composer fix-style      # fix the coding style where possible
composer analyse        # run static analysis
composer ci             # run all of the checks above

If you would rather not install PHP on your machine, the shipped Dockerfile and Makefile run everything inside a container:

make test              # run the test suite
make coverage          # run the test suite and report code coverage
make check-style       # check the coding style
make fix-style         # fix the coding style where possible
make analyse           # run static analysis
make ci                # run all of the checks above
make shell             # open a shell inside the container
make help              # list every available target

Another PHP version can be used with make test PHP_VERSION=8.5.

License

The MIT License (MIT). Please see License File for more information.