Blog Detail

20

Oct
Sanitize & Format Your Data in Laravel with Transformer cover image

arrow_back Sanitize & Format Your Data in Laravel with Transformer

The Transformer is a php package for transforming values or input. It is powered by the Laravel framework’s validation components.

Installation

Run this command to install this package via composer.

composer require surgiie/transformer

Usage

The most basic use is simple, just pass your data and array of callable functions that your data should be called against:

<?php

use Closure;

// example available functions at runtime:
function to_carbon($value)
{
    return new Carbon\Carbon($value);
}

function only_numbers($value)
{
    return preg_replace("/[^0-9]/",'',$value);
}

$input = [
  'first_name'=>'    jim    ',
  'last_name'=>'   thompson',
  'phone_number'=>'123-456-7890',
  'date_of_birth'=>"1991-05-01",
];

$transformers = [
    'first_name'=>'trim|ucfirst',
    'last_name'=>'trim|ucfirst',
    'phone_number'=>'only_numbers',
    'date_of_birth'=>'to_carbon|->format:m/d/y', // more on "object values and method chaining below:"
];

$transformer = new DataTransformer($input, $transformers);

$newData = $transformer->transform();
// Returns:
// [
//     "first_name" => "Jim",
//     "last_name" => "Thompson",
//     "phone_number" => "1234567890",
//     "date_of_birth" => "05/01/91",
// ]

Passing Arguments/Specifying Value Argument Order

Arguments can be specified to your functions using a <function>:<comma-delimited-list> syntax. An example:

$transformers = [
    'example'=>'your_function:arg1,arg2',
];

By default, your function will pass the value being formatted as the first argument and then pass the arguments in the order you specify them. However, if your function does not accept the value as the first argument, you may use the :value: placeholder to specify an order. For example, preg_replace accepts the value to change as the 3rd argument:

$input = ['phone_number'=>'123-456-3235'];
$transformers = [
    'example'=>'preg_replace:/[^0-9]/,,:value:',
];

$transformer = new DataTransformer($input, $transformers);
$transformer->transform();

Optional Transformation/Blank Input

Sometimes you may only want to transform a value if the value isn’t null or “blank”: You can specify? anywhere in the chain of functions to specify if we should break out of processing functions, often this should be defined in front of all your functions:

$input = ['first_name'=>null];

$transformers = [
    'example'=>'?|function_one|function_two',
];
// no functions will be processed because first_name is null.
$transformer = new DataTransformer($input, $transformers);
$transformer->transform();

Note: This package uses Laravel’s blank helper to determine blank/empty values. If you have more complicated logic to break out of rules, use a closure or a \Surgiie\Transformer\Contracts\Transformable class and call the 2nd argument exit callback:

Closures/Transformable Classes

You can use closures for transforming your value as well:

$input = ['first_name'=>' Bob'];
$transformers = [
    'first_name'=>['trim', function ($value) {
        // change the value.
        return $value;
    }],
]

$transformer = new DataTransformer($input, $transformers);
$transformer->transform();

Other Methods

  • Array Input
  • Wildcards
  • Object Values/Method Delegation
  • Guard Layer Over Execution
  • Manually Transforming Values/Single Values
  • Use Traits

You can visit the details of all available methods of this package on Github.

Published at : 20-10-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project