kariricode / transformer
Composable, rule-based data transformation engine for PHP 8.4+ — 32 rules, #[Transform] attributes, case conversion, powered by kariricode/property-inspector. ARFA 1.3.
Fund package maintenance!
Requires
- php: ^8.4
- kariricode/property-inspector: ^2.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-27 02:29:33 UTC
README
Composable, rule-based data transformation engine for PHP 8.4+.
32 built-in rules · case conversion · Brazilian formats · #[Transform] attributes · zero dependencies.
Installation · Quick Start · Attribute API · All 32 Rules · CI Integration · Architecture
The Problem
Data flowing through your application constantly needs reshaping — inconsistent casing, incoming formats that don't match your domain, and repetitive string operations scattered across layers:
// Manual transformation noise — repeated everywhere $slug = strtolower(preg_replace('/\s+/', '-', trim($name))); $camel = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $snake)))); $cpf = preg_replace('/\D/', '', $cpf); // strip mask $date = (new \DateTime($rawDate))->format('Y-m-d'); // 👆 Duplicated across controllers, jobs, mappers — no single contract
The Solution
use KaririCode\Transformer\Provider\TransformerServiceProvider; $engine = (new TransformerServiceProvider())->createEngine(); $result = $engine->transform( data: [ 'name' => ' walmir silva ', 'cpf' => '123.456.789-01', 'joinedAt'=> '2025-06-15', 'bio' => ' Developer and creator ', ], fieldRules: [ 'name' => ['trim', 'capitalize'], 'cpf' => ['remove_non_numeric'], 'joinedAt' => [['date.format', ['from' => 'Y-m-d', 'to' => 'd/m/Y']]], 'bio' => ['trim', 'normalize_whitespace', ['truncate', ['length' => 100]]], ], ); // $result->get('name') === "Walmir Silva" // $result->get('cpf') === "12345678901" // $result->get('joinedAt') === "15/06/2025" // $result->get('bio') === "Developer and creator"
Features
- ✅ 32 built-in rules across 6 categories (String, Date, Numeric, Brazilian, Structure, Encoding)
- ✅ Case conversion —
camel_case,snake_case,pascal_case,kebab_case - ✅ Brazilian formats — CPF mask/unmask, CNPJ, CEP, phone number
- ✅ Attribute-driven API — annotate DTOs with
#[Transform] - ✅ Rule chaining — output of rule N becomes input of rule N+1
- ✅ Zero external dependencies — only
kariricode/property-inspectorfor reflection - ✅ PHPStan Level 9 + Psalm Level 1 — full static-analysis compliance
- ✅ 100% test coverage — 100% Classes/Methods/Lines
- ✅ ARFA 1.3 compliant —
final readonlydomain classes, immutable context
Requirements
| Requirement | Version |
|---|---|
| PHP | 8.4+ |
| Composer | 2.x |
| kariricode/property-inspector | ^2.0 |
Installation
composer require kariricode/transformer
Quick Start
1. Engine API
use KaririCode\Transformer\Provider\TransformerServiceProvider; $engine = (new TransformerServiceProvider())->createEngine(); $result = $engine->transform( data: ['title' => ' hello world ', 'slug' => 'Hello World!'], fieldRules: ['title' => ['trim', 'capitalize'], 'slug' => ['slug']], ); echo $result->get('title'); // "Hello World" echo $result->get('slug'); // "hello-world"
2. Attribute API (DTO-driven)
use KaririCode\Transformer\Attribute\Transform; use KaririCode\Transformer\Provider\TransformerServiceProvider; final class UserDTO { #[Transform('trim', 'lowercase')] public string $email; #[Transform('trim', 'capitalize')] public string $name; #[Transform('remove_non_numeric')] public string $cpf; #[Transform([['truncate', ['length' => 160, 'suffix' => '…']]])] public string $bio; } $provider = new TransformerServiceProvider(); $transformer = $provider->createAttributeTransformer($provider->createConfiguration()); $result = $transformer->transform($dto);
Real-World Use Cases
Use Case 1 — User Profile Form Submission
final class ProfileUpdateDTO { #[Transform('trim', 'lowercase')] public string $email; #[Transform('trim', 'capitalize')] public string $fullName; #[Transform('slug')] public string $username; #[Transform('trim', 'normalize_whitespace', ['truncate', ['length' => 500]])] public string $bio; }
Use Case 2 — Brazilian Document Formatting
$engine->transform( data: [ 'cpf' => '123.456.789-01', 'cnpj' => '12.345.678/0001-99', 'phone' => '(11) 9 9999-9999', 'cep' => '01310-200', ], fieldRules: [ 'cpf' => ['remove_non_numeric'], // "12345678901" 'cnpj' => ['remove_non_numeric'], // "12345678000199" 'phone' => ['remove_non_numeric'], // "11999999999" 'cep' => ['remove_non_numeric'], // "01310200" ], );
Use Case 3 — API Response Key Normalisation
// External API returns camelCase — your DB expects snake_case $engine->transform( data: $apiPayload, fieldRules: ['firstName' => ['snake_case'], 'lastName' => ['snake_case']], ); // "firstName" → "first_name", "lastName" → "last_name"
Use Case 4 — Date Format Conversion Pipeline
$engine->transform( data: ['createdAt' => '2025-06-15', 'updatedAt' => '2025-12-01'], fieldRules: [ 'createdAt' => [['date.format', ['from' => 'Y-m-d', 'to' => 'd/m/Y']]], 'updatedAt' => [['date.format', ['from' => 'Y-m-d', 'to' => 'D, d M Y']]], ], ); // "2025-06-15" → "15/06/2025" // "2025-12-01" → "Mon, 01 Dec 2025"
All 32 Rules
String (14 rules)
| Alias | Description |
|---|---|
trim |
Remove surrounding whitespace |
lowercase / uppercase |
Case conversion |
capitalize |
Capitalise first letter of each word |
camel_case |
Convert to camelCase |
snake_case |
Convert to snake_case |
pascal_case |
Convert to PascalCase |
kebab_case |
Convert to kebab-case |
slug |
URL-safe slug |
truncate |
Truncate with suffix · length, suffix |
remove_whitespace |
Remove all whitespace |
normalize_whitespace |
Collapse multiple spaces to one |
remove_special_chars |
Strip non-alphanumeric |
remove_non_numeric |
Digits only |
reverse |
Reverse string characters |
Date (4 rules)
| Alias | Parameters | Description |
|---|---|---|
date.format |
from, to |
Reformat between two format strings |
date.to_timestamp |
— | Date string → Unix timestamp |
date.to_iso |
— | Any parseable date → ISO 8601 |
date.age |
— | Birthdate → current age (integer) |
Numeric (4 rules)
| Alias | Parameters | Description |
|---|---|---|
number.round |
precision: int |
Round to decimal places |
number.abs |
— | Absolute value |
number.clamp |
min, max |
Clamp to value range |
number.format |
decimals, dec_sep, thou_sep |
Format number |
Brazilian (4 rules)
| Alias | Description |
|---|---|
cpf.mask |
Digits → 000.000.000-00 |
cnpj.mask |
Digits → 00.000.000/0000-00 |
cep.mask |
Digits → 00000-000 |
phone.mask |
Digits → (00) 00000-0000 |
Encoding (4 rules)
| Alias | Description |
|---|---|
encode.base64 / decode.base64 |
Base64 round-trip |
encode.url / decode.url |
URL encode/decode |
Structure (2 rules)
| Alias | Description |
|---|---|
json.encode |
Array → JSON string |
json.decode |
JSON string → array |
Attribute-Driven DTO Transformation
use KaririCode\Transformer\Attribute\Transform; final class NewsArticleDTO { #[Transform('trim', 'capitalize')] public string $title; #[Transform('slug')] public string $permalink; #[Transform('trim', 'html.encode', ['truncate', ['length' => 300, 'suffix' => '…']])] public string $excerpt; #[Transform([['date.format', ['from' => 'Y-m-d', 'to' => 'd/m/Y']]])] public string $publishedAt; }
CI Integration
- name: Run quality pipeline run: | kcode init kcode quality
| Tool | Level | Result |
|---|---|---|
php-cs-fixer |
KaririCode standard | ✅ |
phpstan |
Level 9 | ✅ |
psalm |
Level 1 | ✅ |
phpunit + pcov |
100% coverage | ✅ |
Architecture
Design Decisions
| Decision | Rationale | ADR |
|---|---|---|
| Alias-based rule registry | Decouples engine; custom rules register without subclassing engine | ADR-001 |
PropertyInspector for #[Transform] |
Reflection caching across multiple transform calls | ADR-002 |
TransformationContext immutability |
final readonly + builder; prevents cross-field parameter pollution |
ADR-003 |
| Left-to-right rule chaining | Predictable, order-dependent composition | ADR-004 |
| Zero external business deps | PHP 8.4+ built-ins only — ARFA Principle 16 | ADR-005 |
Project Stats
| Metric | Value |
|---|---|
| Rule classes | 32 |
| External dependencies | 1 (kariricode/property-inspector) |
| Line coverage | 100% |
| PHPStan level | 9 (0 errors) |
| Psalm level | 1 (0 errors) |
Ecosystem Integration
| Library | Integration |
|---|---|
| kariricode/sanitizer | Sanitize input before or after transformation |
| kariricode/normalizer | ProcessorBridge — plug normalizer into transform pipeline |
| kariricode/validator | Validate after transforming into canonical format |
| kariricode/property-inspector | Reflection caching for #[Transform] attribute scanning |
| kariricode/devkit | kcode quality CI pipeline, PHPStan/Psalm/cs-fixer unified runner |
Development
This package keeps no QA tools in require-dev. KaririCode Devkit (kcode) installs PHPUnit, PHPStan, Psalm, PHP-CS-Fixer and Rector into .kcode/ (git-ignored) the first time a command needs them.
Install kcode once (PHP 8.4+):
wget https://github.com/KaririCode-PHP/devkit/releases/latest/download/kcode.phar chmod +x kcode.phar sudo mv kcode.phar /usr/local/bin/kcode
From the repository root:
composer install # runtime dependencies only kcode init # generate .kcode/ and install the toolchain (first run) kcode test # PHPUnit (--coverage, --suite=Unit, any PHPUnit option) kcode analyse # PHPStan, then Psalm kcode cs:fix # apply the code style (--check reports only) kcode quality # the CI gate: cs-fixer, PHPStan, Psalm, PHPUnit kcode quality --docker # the same gate inside the kcode PHP container
Project overrides go in devkit.php (kcode init --config writes a template); run kcode init after editing it. kcode clean --all removes .kcode/.
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feat/my-rule - Run:
kcode quality— all 4 tools must pass - Submit a pull request against
main
Part of the KaririCode Framework ecosystem.
Built with ❤️ by Walmir Silva · kariricode.org