Sanitizing and formatting data with the Transformer PHP package
- 2022年11月07日
- 技術情報
Transformer is a PHP package for sanitizing and formatting data powered by Laravel’s validation components. The package uses a familiar Laravel validation-like syntax to transform data using classes, callable functions and etc.
Here is a quick sample.
$input = [
'name' => ' yuuma ',
'phone_number' => '123-4567-7891',
'date_of_birth' => "1998-04-12",
];
(new DataTransformer($input, [
'name' => 'trim|ucfirst',
'phone_number' => 'only_numbers',
'date_of_birth' => 'to_carbon|->format:m/d/y',
]))->transform();
// Returns:
// [
// "name" => "Yuuma",
// "phone_number" => "12345677891",
// "date_of_birth" => "04/12/98",
// ]
Data can also be transformed using closures or classes that implement the provided Transformable interface. Additionally, this package supports nested array data using dot notation, wildcard input (applying functions to keys matching a wildcard pattern), and more.
Learn more about this package, get full installation instructions, and view the source code on GitHub.
Yuuma
yuuma at 2022年11月07日 10:00:00