Sanitize and Formatting Data: Transformer is a PHP package deal for sanitizing and formatting data powered by Laravel’s validation components. The package deal uses a familiar Laravel validation like syntax to remodel data utilizing callable functions, classes, and more:

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' => '    Tilak ',
  'last_name' => '   KC',
  'phone_number' => '987-587-2588',
  'date_of_birth' => "1989-05-01",
];
 
(new DataTransformer($input, [
    'first_name' => 'trim|ucfirst',
    'last_name' => 'trim|ucfirst',
    'phone_number' => 'only_numbers',
    'date_of_birth' => 'to_carbon|->format:m/d/y',
]))->transform();
 

These familiar with Laravel’s validation API will discover the string-based transformer rules. Also, as seen within the snippet, this package has a “chainable” syntax (to_carbon|->format:m/d/y) that may chain additional calls on a bit of data.

How to Generate QR code in Laravel App

You can also rework data utilizing closures or a class implementing the supplied Transformable interface. Additionally, this package supports nested array data utilizing dot notation, wildcard inputs (apply functions on keys matching a wildcard pattern), and more.

Leave Your Comment