PHP code example of kariricode / transformer
1. Go to this page and download the library: Download kariricode/transformer library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kariricode / transformer example snippets
// Scattered everywhere, no composition, no audit trail
$name = lcfirst(str_replace('_', '', ucwords($input, '_'))); // camelCase
$price = 'R$ ' . number_format($price, 2, ',', '.'); // currency
$rank = $rank . 'th'; // ordinal
$cpf = preg_replace('/\D/', '', $cpf); // strip formatting
// No attribute DSL, no pipeline composition, no tracking
use KaririCode\Transformer\Provider\TransformerServiceProvider;
$engine = (new TransformerServiceProvider())->createEngine();
$result = $engine->transform(
data: [
'name' => 'walmir_silva',
'price' => 1234.5,
'rank' => 3,
'cpf' => '529.982.247-25',
],
fieldRules: [
'name' => ['pascal_case'],
'price' => [['currency_format', ['prefix' => 'R$ ', 'dec_point' => ',', 'thousands' => '.']]],
'rank' => ['ordinal'],
'cpf' => ['cpf_to_digits'],
],
);
echo $result->get('name'); // "WalmirSilva"
echo $result->get('price'); // "R$ 1.234,50"
echo $result->get('rank'); // "3rd"
echo $result->get('cpf'); // "52998224725"
aririCode\Transformer\Provider\TransformerServiceProvider;
$engine = (new TransformerServiceProvider())->createEngine();
$result = $engine->transform(
data: ['name' => 'walmir_silva', 'price' => 1234.5],
fieldRules: [
'name' => ['camel_case'],
'price' => [['currency_format', ['prefix' => '$']]],
],
);
echo $result->get('name'); // "walmirSilva"
echo $result->get('price'); // "$1,234.50"
use KaririCode\Transformer\Attribute\Transform;
final class ApiResponse
{
#[Transform('camel_case')]
public string $fieldName = 'user_first_name';
#[Transform(['mask', ['keep_start' => 3, 'keep_end' => 2]])]
public string $cpf = '52998224725';
#[Transform(['currency_format', ['prefix' => 'R$ ', 'dec_point' => ',', 'thousands' => '.']])]
public float $price = 1234.5;
}
$transformer = (new TransformerServiceProvider())->createAttributeTransformer();
$result = $transformer->transform(new ApiResponse());
// $dto->fieldName === 'userFirstName'
// $dto->cpf === '529******25'
// $dto->price === 'R$ 1.234,50'
$result = $engine->transform(
['a' => 'helloWorld', 'b' => 'hello_world', 'c' => 'Hello World', 'd' => 'hello-world'],
['a' => ['snake_case'], 'b' => ['camel_case'], 'c' => ['kebab_case'], 'd' => ['pascal_case']],
);
// a: "hello_world", b: "helloWorld", c: "hello-world", d: "HelloWorld"
// Flatten nested arrays
$result = $engine->transform(
['config' => ['a' => ['b' => 1, 'c' => 2], 'd' => 3]],
['config' => ['flatten']],
);
// config: {"a.b": 1, "a.c": 2, "d": 3}
// Group by field
$result = $engine->transform(
['users' => [
['dept' => 'eng', 'name' => 'Alice'],
['dept' => 'hr', 'name' => 'Bob'],
['dept' => 'eng', 'name' => 'Carol'],
]],
['users' => [['group_by', ['field' => 'dept']]]],
);
// users: {"eng": [{...Alice}, {...Carol}], "hr": [{...Bob}]}
$result = $engine->transform(
['cpf' => '529.982.247-25', 'phone' => '85999991234'],
['cpf' => ['cpf_to_digits'], 'phone' => ['phone_format']],
);
// cpf: "52998224725"
// phone: "(85) 99999-1234"
$engine = (new TransformerServiceProvider())->createEngine();
$result = $engine->transform(
['price' => 1234.5, 'name' => 'hello_world'],
['price' => [['currency_format', ['prefix' => '$']]], 'name' => ['camel_case']],
);
$result->get('price'); // "$1,234.50"
$result->get('name'); // "helloWorld"
$result->wasTransformed(); // true
$result->transformedFields(); // ['price', 'name']
foreach ($result->transformationsFor('name') as $t) {
echo "{$t->ruleName}: '{$t->before}' → '{$t->after}'\n";
}
// string.camel_case: 'hello_world' → 'helloWorld'