PHPackages                             konsulting/laravel-transformer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. konsulting/laravel-transformer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

konsulting/laravel-transformer
==============================

A simple way to transform data, with helpers for transforming data in Laravel applications.

0.7.0(11mo ago)124.5k↓67.9%1MITPHPPHP ^8.0

Since Feb 6Pushed 4mo ago2 watchersCompare

[ Source](https://github.com/konsulting/laravel-transformer)[ Packagist](https://packagist.org/packages/konsulting/laravel-transformer)[ RSS](/packages/konsulting-laravel-transformer/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (5)Versions (23)Used By (1)

Transformer
===========

[](#transformer)

[![Build Status](https://camo.githubusercontent.com/4bcb24ab077d24ca14c6b0cac15473c4a91378a152d125228787332b567e83fc/68747470733a2f2f7472617669732d63692e6f72672f6b6f6e73756c74696e672f6c61726176656c2d7472616e73666f726d65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/konsulting/laravel-transformer)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/136d418020f5f48f23f3e226f72a1bc9dd40d1316fe31a89645ae5f19c1e06fb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b6f6e73756c74696e672f6c61726176656c2d7472616e73666f726d65722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/konsulting/laravel-transformer/?branch=master)

*A simple way to transform data, with helpers for transforming data in [Laravel](https://laravel.com) applications.*

Installation
------------

[](#installation)

- Install Transformer using composer: `composer require konsulting/laravel-transformer`

### Transformer in a Laravel application

[](#transformer-in-a-laravel-application)

- Transformer uses `Illuminate\Support\Collection` and `Illuminate\Support\Arr`, and requires a couple of extensions to these. The extensions are available in the `konsulting/laravel-extend-collections` package.
- You'll need both the `CollectionsServiceProvider` from that package, and `TransformerServiceProvider` in your `config/app.php`.

If you are using Laravel 5.5, they will auto-register. However, if you have chosen not to auto-register, or are using an earlier version, add the following to `config/app.php`

```
'providers' => [
    // Other service providers...

    Konsulting\Laravel\CollectionsServiceProvider::class,
    Konsulting\Laravel\Transformer\TransformerServiceProvider::class,
],
```

- Optionally, add the Transformer Facade to `config/app.php`

```
'aliases' => [
    // Other aliases...

    'Transformer' => Konsulting\Laravel\Transformer\TransformerFacade::class,
],
```

- Optionally publish the config file, and adjust the rule packs you want to use. `php artisan vendor:publish --provider=Konsulting\\Laravel\\Transformer\\TransformerServiceProvider --tag=config`

### Transformer outside a Laravel application

[](#transformer-outside-a-laravel-application)

- Transformer uses `Illuminate\Support\Collection` and `Illuminate\Support\Arr`. Outside a Laravel application, it will use `tighten/collect` (an extraction of Collection &amp;&amp; Arr from Laravel's Illuminate\\Support) to get these dependencies.
- Transform also requires a couple of extensions to these. The extensions are available in the `konsulting/laravel-extend-collections` package. You'll need to register the extensions manually.
- You will need to build up your Transformer manually for use in your application.

```
    // Basic example

    use Konsulting\Laravel\Transformer\Transformer;
    use Konsulting\Laravel\Transformer\RulePacks\CoreRulePack;
    use Konsulting\Laravel\Transformer\RulePacks\CarbonRulePack;

    require __DIR__ . '/../vendor/autoload.php';

    // Extend Illuminate\Support\Arr and Illuminate\Support\Collection
    \Konsulting\Laravel\load_collection_extensions();

    // Build up Transformer
    $transformer = new Transformer([CoreRulePack::class, CarbonRulePack::class]);

    // Transformer now available to use, see Usage
```

Usage
-----

[](#usage)

Transformer uses `RulePacks` to provide transformation functionality. RulePacks can be added to the Transformer during construction, or after with the `addRulePack` or `addRulePacks` methods.

A set of rules can be passed in during construction (useful when applying the same rules to different sets of data) or rules can be passed in at the point when performing transformation.

To transform data, the `transform` method is used. It accepts an array (or collection) of data to transform, and optionally rules to apply.

- Rules are presented in a similar manner to the [Laravel Validator](https://laravel.com/docs/5.4/validation). They provide functionality to handle nested data, and follow the same string format.
- Arrays of rules are indexed by a field expression and provide a `|` (pipe) delimited list of rules to apply.
- Rules may be provided a set of parameters in CSV format. Field expressions may use `*` as a wildcard to match elements at that depth and `**` as a special case to match everything.
- Rule sequences are built up in the order they are provided.

```
    // using the $transformer built up earlier

    $rules = [
        '*' => 'drop_if_empty',
        'name' => 'trim',
        'contacts.*.name' => 'trim|uppercase'
    ];

    $data = [
        0 => '',
        'name' => '   Keoghan Litchfield   ',
        'contacts' => [
            ['name' => 'Robin'],
            ['name' => 'Roger'],
            ['name' => ''],
        ],
    ];

    $result = $transformer->transform($data, $rules);

    //    Outputs [
    //        'name' => 'Keoghan Litchfield',
    //        'contacts' => [
    //            ['name' => 'ROBIN'],
    //            ['name' => 'ROGER'],
    //        ],
    //    ];
```

### Transform helper

[](#transform-helper)

There is also a helper class `Transform`, which facilitates the easy transformation of a single value by one or more rules. `Transform` receives an instance of `Transformer` via its constructor, which provides the transformation logic and determines which rules are available. Using the instance of `Transformer` built up previously:

```
use Konsulting\Laravel\Transformer\Transform;

$transform = new Transform($transformer);
```

Rules may be called as methods on the `Transform` object, with the value to be transformed passed in as the first argument and any rule parameters as subsequent arguments.

```
$transform->trim(' Some string to be trimmed   ');  // Outputs 'Some string to be trimmed'

$transform->regexReplace('testing', 'e', 'oa');     // Outputs 'toasting'
```

Alternatively, rules may be passed via the `withRule()` and `withRules()` methods (for singular and multiple rules respectively). Rule parameters are passed either as separate arguments, or as an array.

```
// Single rule
$transform->withRule('  test  ', 'trim');                           // Outputs 'test'

// Single rule with parameters passed as separate arguments
$transform->withRule('test', 'regex_replace', 'e', 'oa');           // Outputs 'toast'

// Singe rule with parameters passed as an array
$transform->withRule('test', 'regex_replace', ['e', 'oa']);         // Outputs 'toast' as well

// Multiple rules passed as a sequential array
$transform->withRules('  test  ', ['trim', 'uppercase']);           // Outputs 'TEST'

// Multiple rules and parameters passed as an assocative array: [$rule => [$param1, $param2], $rule2 => []...]
$transform->withRules('--test--', [                                 // Outputs 'TOAST'
    'trim'          => ['-'],
    'regex_replace' => ['e', 'oa'],
    'uppercase'     => [],
]);
```

#### Fluent API

[](#fluent-api)

Rules may also be called fluently: the input value is set with the `input()` method, and the result is obtained with `get()`. Any number of rule methods may be chained between these.

```
$transform->input(' hello ')
    ->trim()
    ->regexReplace('hello', 'world')
    ->uppercase()
    ->get();

// Outputs 'WORLD'
```

When the fluent API is used, the value is not passed as an argument to the rule methods (as it has already been set via `input()`). As such, all arguments passed to rule methods are treated as rule parameters.

`withRule()` and `withRules()` may be used to fluently declare rules with or without parameters:

```
$transform->input($input)
    ->withRule('trim')
    ->uppercase()
    ->get();

$transform->input($input)
    ->lowercase()
    ->withRules(['trim', 'uppercase'])
    ->get();
```

### Available Rules

[](#available-rules)

We provide a couple of rule packs for use, it is easy to extend the rules available by creating your own Rule Pack. Rule Packs are loaded in the declared order, methods in later packs will override packs loaded earlier.

Parameter names are denoted by `` and optional parameters by `[]`.

#### Core Rule Pack

[](#core-rule-pack)

##### Cleaning up

[](#cleaning-up)

- `null_if_empty`
- `null_if_empty_string`
- `return_null_if_empty` – equivalent to `null_if_empty|bail_if_null`.
- `return_null_if_empty_string` – equivalent to `null_if_empty_string|bail_if_null`.
- `bail_if_null`
- `drop_if_null`
- `drop_if_empty`
- `drop_if_empty_string`
- `trim:[]` – performs default PHP trim() if no characters supplied.

##### Casting

[](#casting)

- `string` - convert to string, an array is transformed to a CSV or returns ‘’ for items that cannot be represented as a string.
- `boolean`
- `array`
- `collection` - convert to `Illuminate\Support\Collection`
- `json`
- `float`
- `integer`
- `date_time:[timezone]`
- `date_time_immutable:[timezone]`

##### String manipulation

[](#string-manipulation)

- `uppercase`
- `lowercase`

##### Regex and string replace

[](#regex-and-string-replace)

- `replace:,`
- `regex_replace:,`
- `numeric`
- `alpha`
- `alpha_dash`
- `alpha_num`
- `alpha_num_dash`

#### Carbon Rule Pack

[](#carbon-rule-pack)

- `carbon`
- `date_format` - parameter for the format required.

#### Related Fields Rule Pack

[](#related-fields-rule-pack)

- `null_with:`
- `drop_with:`
- `bail_with:`
- `null_without:`
- `drop without:`
- `bail_without:`

#### Number Rule Pack

[](#number-rule-pack)

- `clamp:,` - constrain a number between two values

***Note:** key is a dot notation key for another field in the dataset*

### Laravel Helpers

[](#laravel-helpers)

We use Laravel frequently, so have a couple of extras added here.

#### Facade

[](#facade)

Use the facade to gain easy access to the Transformer wherever you are.

```
\Transformer::transform($data, $rules);
```

#### Request Macro `transform`

[](#request-macro-transform)

The Service Provider adds the transform Macro to the `Illuminate\Http\Request` class. This makes it simple to invoke the transformation on a request at any point. The method passes the request object back to allow chaining.

```
// Example Controller

namespace App\Http\Controllers;

use App\ContactRequest;
use Illuminate\Http\Request;

class ContactRequestsController
{
    // ...

    public function store(Request $request)
    {
        $request->transform([
            'name' => 'trim|uppercase',
            'message' => 'trim',
        ]);

        $this->validate($request, [
            'name' => 'required',
        ]);

        return ContactRequest::create(
            $request->only('name', 'message')
        );
    }
}
```

#### TransformingRequest Trait

[](#transformingrequest-trait)

This trait makes the form request transform the data before validation occurs (which is upon it being resolved by the container).

Rules for transformation are provided in the `transformRules` method.

Using the TransformingRequest Trait

```
// Form Request

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Konsulting\Laravel\Transformer\TransformingRequest;

class ContactFormRequest extends FormRequest
{
    use TransformingRequest;

	  // ... other methods including rules()

    public function transformRules()
    {
        return [
            'name' => 'trim|uppercase',
            'message' => 'trim',
        ];
    }
```

```
// Controller

namespace App\Http\Controllers;

use App\ContactRequest;
use App\Http\Requests\ContactFormRequests;

class ContactRequestsController
{
    // ...

    public function store(ContactFormRequest $request)
    {
        return ContactRequest::create(
            $request->only('name', 'message')
        );
    }
}
```

### Middleware

[](#middleware)

The `TransformRequest` middleware applies transformations to requests according to configured rules. These rules are specified in the `middleware_rules` key of the config file as detailed in [Usage](#usage).

To register the middleware for use in your project, add the following line to your project's `App/Http/Kernel.php`:

```
'transform_data' => \Konsulting\Laravel\Transformer\Middleware\TransformRequest::class
```

The default middleware rules state that every field should be trimmed of whitespace and nulled if empty:

```
'middleware_rules' => [
    '**' => 'trim|return_null_if_empty',
]
```

Rules need not be applied to all fields; specific fields may be targeted within the middleware if required:

```
'middleware_rules' => [
    'postcode'  => 'uppercase',
    'email'     => 'lowercase',
]
```

With the above configuration, the postcode and email fields of every request sent through the middleware will be affected, but all other fields will be left unchanged.

Multiple transformer middlewares may be useful in a project: to achieve this, copy `laravel-transformer/src/Middleware/TransformRequest.php` to your project's `App/Http/Middleware` directory, and rename/edit as necessary. Each new middleware will have to be registered in the kernel.

Contributing
------------

[](#contributing)

Contributions are welcome and will be fully credited. We will accept contributions by Pull Request.

Please:

- Use the PSR-2 Coding Standard.
- Add tests, if you’re not sure how, please ask.
- Document changes in behaviour, including readme.md.

Testing
-------

[](#testing)

We use [PHPUnit](https://phpunit.de) and the excellent [orchestral/testbench](https://github.com/orchestral/testbench).

Run tests using PHPUnit: `vendor/bin/phpunit`

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance65

Regular maintenance activity

Popularity28

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~173 days

Recently: every ~710 days

Total

20

Last Release

137d ago

PHP version history (4 changes)0.1.0PHP ^7.0

0.6.0PHP ^7.0||^8.0

0.7.0PHP ^8.0

0.8.x-devPHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4703657?v=4)[Konsulting](/maintainers/konsulting)[@konsulting](https://github.com/konsulting)

![](https://www.gravatar.com/avatar/bae802ba5a70c050135022ed88fc558d2ac76ace8e5b67ce176f683d1a0afc1b?d=identicon)[rdarcy1](/maintainers/rdarcy1)

---

Top Contributors

[![rdarcy1](https://avatars.githubusercontent.com/u/15962421?v=4)](https://github.com/rdarcy1 "rdarcy1 (33 commits)")[![Keoghan](https://avatars.githubusercontent.com/u/6714599?v=4)](https://github.com/Keoghan "Keoghan (32 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (2 commits)")[![jocoDomino](https://avatars.githubusercontent.com/u/26391067?v=4)](https://github.com/jocoDomino "jocoDomino (1 commits)")

---

Tags

datalaravelphprequesttransform

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/konsulting-laravel-transformer/health.svg)

```
[![Health](https://phpackages.com/badges/konsulting-laravel-transformer/health.svg)](https://phpackages.com/packages/konsulting-laravel-transformer)
```

###  Alternatives

[dapr/php-sdk

Dapr Implementation in PHP

74100.5k6](/packages/dapr-php-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
