A powerful and flexible data transformation component for PHP, part of the KaririCode Framework. It uses attribute-based transformation with configurable processors to ensure consistent data transformation and formatting in your applications.
- Features
- Installation
- Usage
- Available Transformers
- Configuration
- Integration with Other KaririCode Components
- Development and Testing
- Contributing
- License
- Support and Community
- Attribute-based transformation for object properties
- Comprehensive set of built-in transformers for common use cases
- Easy integration with other KaririCode components
- Configurable processors for customized transformation logic
- Extensible architecture allowing custom transformers
- Robust error handling and reporting
- Chainable transformation pipelines for complex data transformation
- Built-in support for multiple transformation scenarios
- Type-safe transformation with PHP 8.3 features
- Preservation of original data types
- Flexible formatting options for various data types
You can install the Transformer component via Composer:
composer require kariricode/transformer
- PHP 8.3 or higher
- Composer
- Extensions:
ext-mbstring
,ext-json
- Define your data class with transformation attributes:
use KaririCode\Transformer\Attribute\Transform;
class DataFormatter
{
#[Transform(
processors: ['date' => ['inputFormat' => 'd/m/Y', 'outputFormat' => 'Y-m-d']]
)]
private string $date = '25/12/2024';
#[Transform(
processors: ['number' => ['decimals' => 2, 'decimalPoint' => ',', 'thousandsSeparator' => '.']]
)]
private float $price = 1234.56;
#[Transform(
processors: ['mask' => ['type' => 'phone']]
)]
private string $phone = '11999887766';
// Getters and setters...
}
- Set up the transformer and use it:
use KaririCode\ProcessorPipeline\ProcessorRegistry;
use KaririCode\Transformer\Transformer;
use KaririCode\Transformer\Processor\Data\{DateTransformer, NumberTransformer};
use KaririCode\Transformer\Processor\String\MaskTransformer;
$registry = new ProcessorRegistry();
$registry->register('transformer', 'date', new DateTransformer());
$registry->register('transformer', 'number', new NumberTransformer());
$registry->register('transformer', 'mask', new MaskTransformer());
$transformer = new Transformer($registry);
$formatter = new DataFormatter();
$result = $transformer->transform($formatter);
if ($result->isValid()) {
echo "Date: " . $formatter->getDate() . "\n"; // Output: 2024-12-25
echo "Price: " . $formatter->getPrice() . "\n"; // Output: 1.234,56
echo "Phone: " . $formatter->getPhone() . "\n"; // Output: (11) 99988-7766
}
Here's an example of how to use the KaririCode Transformer in a real-world scenario, demonstrating various transformation capabilities:
use KaririCode\Transformer\Attribute\Transform;
class ComplexDataTransformer
{
#[Transform(
processors: ['case' => ['case' => 'snake']]
)]
private string $text = 'transformThisTextToSnakeCase';
#[Transform(
processors: ['slug' => []]
)]
private string $title = 'This is a Title for URL!';
#[Transform(
processors: ['arrayKey' => ['case' => 'camel']]
)]
private array $data = [
'user_name' => 'John Doe',
'email_address' => 'john@example.com',
'phone_number' => '1234567890'
];
#[Transform(
processors: [
'template' => [
'template' => 'Hello {{name}}, your order #{{order_id}} is {{status}}',
'removeUnmatchedTags' => true,
'preserveData' => true
]
]
)]
private array $templateData = [
'name' => 'John',
'order_id' => '12345',
'status' => 'completed'
];
// Getters and setters...
}
class StringTransformerExample
{
#[Transform(
processors: ['case' => ['case' => 'snake']]
)]
private string $methodName = 'getUserProfileData';
#[Transform(
processors: ['case' => ['case' => 'camel']]
)]
private string $variableName = 'user_profile_data';
#[Transform(
processors: ['slug' => ['separator' => '-']]
)]
private string $articleTitle = 'How to Use PHP 8.3 Features!';
#[Transform(
processors: ['mask' => ['type' => 'phone']]
)]
private string $phoneNumber = '11999887766';
}
// Output:
// methodName: get_user_profile_data
// variableName: userProfileData
// articleTitle: how-to-use-php-8-3-features
// phoneNumber: (11) 99988-7766
class CurrencyTransformerExample
{
#[Transform(
processors: ['number' => [
'decimals' => 2,
'decimalPoint' => ',',
'thousandsSeparator' => '.'
]]
)]
private float $price = 1234567.89;
#[Transform(
processors: ['number' => [
'decimals' => 0,
'thousandsSeparator' => ','
]]
)]
private int $quantity = 1000000;
}
// Output:
// price: 1.234.567,89
// quantity: 1,000,000
class DateTransformerExample
{
#[Transform(
processors: ['date' => [
'inputFormat' => 'd/m/Y',
'outputFormat' => 'Y-m-d'
]]
)]
private string $sqlDate = '25/12/2024';
#[Transform(
processors: ['date' => [
'inputFormat' => 'Y-m-d',
'outputFormat' => 'F j, Y'
]]
)]
private string $displayDate = '2024-12-25';
#[Transform(
processors: ['date' => [
'inputFormat' => 'Y-m-d H:i:s',
'outputFormat' => 'd/m/Y H:i',
'inputTimezone' => 'UTC',
'outputTimezone' => 'America/Sao_Paulo'
]]
)]
private string $timestamp = '2024-12-25 15:30:00';
}
// Output:
// sqlDate: 2024-12-25
// displayDate: December 25, 2024
// timestamp: 25/12/2024 12:30
class ApiResponseTransformerExample
{
#[Transform(
processors: ['arrayKey' => ['case' => 'camel']]
)]
private array $userData = [
'user_id' => 123,
'first_name' => 'John',
'last_name' => 'Doe',
'email_address' => 'john@example.com',
'phone_numbers' => [
'home_phone' => '1234567890',
'work_phone' => '0987654321'
]
];
#[Transform(
processors: ['arrayFlat' => ['separator' => '.']]
)]
private array $nestedConfig = [
'database' => [
'mysql' => [
'host' => 'localhost',
'port' => 3306
]
],
'cache' => [
'redis' => [
'host' => '127.0.0.1',
'port' => 6379
]
]
];
}
// Output:
// userData:
// {
// "userId": 123,
// "firstName": "John",
// "lastName": "Doe",
// "emailAddress": "john@example.com",
// "phoneNumbers": {
// "homePhone": "1234567890",
// "workPhone": "0987654321"
// }
// }
//
// nestedConfig:
// {
// "database.mysql.host": "localhost",
// "database.mysql.port": 3306,
// "cache.redis.host": "127.0.0.1",
// "cache.redis.port": 6379
// }
class NotificationTransformerExample
{
#[Transform(
processors: [
'template' => [
'template' => <<<TEMPLATE
Dear {{userName}},
Your order #{{orderId}} has been {{status}}.
{{#if tracking}}
Track your package: {{tracking}}
{{/if}}
Total: {{currency}}{{amount}}
Best regards,
{{companyName}}
TEMPLATE,
'preserveData' => true
]
]
)]
private array $emailData = [
'userName' => 'John Doe',
'orderId' => 'ORD-12345',
'status' => 'shipped',
'tracking' => 'TRACK-XYZ-789',
'currency' => '$',
'amount' => '299.99',
'companyName' => 'KaririCode Store'
];
}
// Output:
// Original Data:
// {
// "userName": "John Doe",
// "orderId": "ORD-12345",
// "status": "shipped",
// "tracking": "TRACK-XYZ-789",
// "currency": "$",
// "amount": "299.99",
// "companyName": "KaririCode Store"
// }
//
// Rendered Template:
// Dear John Doe,
//
// Your order #ORD-12345 has been shipped.
// Track your package: TRACK-XYZ-789
//
// Total: $299.99
//
// Best regards,
// KaririCode Store
class ChainTransformerExample
{
#[Transform(
processors: [
'case' => ['case' => 'lower'],
'slug' => ['separator' => '-'],
'template' => [
'template' => '{{date}}-{{slug}}',
'preserveData' => true
]
]
)]
private array $urlData = [
'date' => '2024-01-15',
'slug' => 'How to Chain Multiple Transformers'
];
}
// Output:
// 2024-01-15-how-to-chain-multiple-transformers
-
CaseTransformer: Transforms string case (camel, snake, pascal, kebab).
- Configuration Options:
case
: Target case format (lower, upper, title, sentence, camel, pascal, snake, kebab)preserveNumbers
: Whether to preserve numbers in transformation
- Configuration Options:
-
MaskTransformer: Applies masks to strings (phone, CPF, CNPJ, etc.).
- Configuration Options:
mask
: Custom mask patterntype
: Predefined mask typeplaceholder
: Mask placeholder character
- Configuration Options:
-
SlugTransformer: Generates URL-friendly slugs.
- Configuration Options:
separator
: Separator characterlowercase
: Convert to lowercasereplacements
: Custom character replacements
- Configuration Options:
-
TemplateTransformer: Processes templates with variable substitution.
- Configuration Options:
template
: Template stringremoveUnmatchedTags
: Remove unmatched placeholderspreserveData
: Keep original data in result
- Configuration Options:
-
DateTransformer: Converts between date formats.
- Configuration Options:
inputFormat
: Input date formatoutputFormat
: Output date formatinputTimezone
: Input timezoneoutputTimezone
: Output timezone
- Configuration Options:
-
NumberTransformer: Formats numbers with locale-specific settings.
- Configuration Options:
decimals
: Number of decimal placesdecimalPoint
: Decimal separatorthousandsSeparator
: Thousands separatorroundUp
: Round up decimals
- Configuration Options:
-
JsonTransformer: Handles JSON encoding/decoding.
- Configuration Options:
encodeOptions
: JSON encoding optionspreserveType
: Keep original data typeassoc
: Use associative arrays
- Configuration Options:
-
ArrayFlattenTransformer: Flattens nested arrays.
- Configuration Options:
depth
: Maximum depth to flattenseparator
: Key separator for flattened structure
- Configuration Options:
-
ArrayGroupTransformer: Groups array elements by key.
- Configuration Options:
groupBy
: Key to group bypreserveKeys
: Maintain original keys
- Configuration Options:
-
ArrayKeyTransformer: Transforms array keys.
- Configuration Options:
case
: Target case for keysrecursive
: Apply to nested arrays
- Configuration Options:
-
ArrayMapTransformer: Maps array keys to new structure.
- Configuration Options:
mapping
: Key mapping configurationremoveUnmapped
: Remove unmapped keysrecursive
: Apply to nested arrays
- Configuration Options:
-
ChainTransformer: Executes multiple transformers in sequence.
- Configuration Options:
transformers
: Array of transformers to executestopOnError
: Stop chain on first error
- Configuration Options:
-
ConditionalTransformer: Applies transformations based on conditions.
- Configuration Options:
condition
: Condition callbacktransformer
: Transformer to applydefaultValue
: Value when condition fails
- Configuration Options:
Transformers can be configured globally or per-instance. Example of configuring the NumberTransformer:
use KaririCode\Transformer\Processor\Data\NumberTransformer;
$numberTransformer = new NumberTransformer();
$numberTransformer->configure([
'decimals' => 2,
'decimalPoint' => ',',
'thousandsSeparator' => '.',
]);
$registry->register('transformer', 'number', $numberTransformer);
The Transformer component integrates with:
- KaririCode\Contract: Provides interfaces for component integration
- KaririCode\ProcessorPipeline: Used for transformation pipelines
- KaririCode\PropertyInspector: Processes transformation attributes
Complete registry setup example:
$registry = new ProcessorRegistry();
// Register String Transformers
$registry->register('transformer', 'case', new CaseTransformer())
->register('transformer', 'mask', new MaskTransformer())
->register('transformer', 'slug', new SlugTransformer())
->register('transformer', 'template', new TemplateTransformer());
// Register Data Transformers
$registry->register('transformer', 'date', new DateTransformer())
->register('transformer', 'number', new NumberTransformer())
->register('transformer', 'json', new JsonTransformer());
// Register Array Transformers
$registry->register('transformer', 'arrayFlat', new ArrayFlattenTransformer())
->register('transformer', 'arrayGroup', new ArrayGroupTransformer())
->register('transformer', 'arrayKey', new ArrayKeyTransformer())
->register('transformer', 'arrayMap', new ArrayMapTransformer());
Similar development setup as the Validator component, using Docker and Make commands.
make up
: Start servicesmake down
: Stop servicesmake test
: Run testsmake coverage
: Generate coverage reportmake cs-fix
: Fix code stylemake quality
: Run quality checks
Contributions are welcome! Please see our Contributing Guide.
MIT License - see LICENSE file.
- Documentation: https://kariricode.org/docs/transformer
- Issues: GitHub Issues
- Forum: KaririCode Club Community
- Stack Overflow: Tag with
kariricode-transformer
Built with ❤️ by the KaririCode team. Transforming data with elegance and precision.# KaririCode Framework: Transformer Component
A powerful and flexible data transformation component for PHP, part of the KaririCode Framework. It uses attribute-based transformation with configurable processors to ensure consistent data transformation and formatting in your applications.
- Features
- Installation
- Usage
- Available Transformers
- Configuration
- Integration with Other KaririCode Components
- Development and Testing
- Contributing
- License
- Support and Community
- Attribute-based transformation for object properties
- Comprehensive set of built-in transformers for common use cases
- Easy integration with other KaririCode components
- Configurable processors for customized transformation logic
- Extensible architecture allowing custom transformers
- Robust error handling and reporting
- Chainable transformation pipelines for complex data transformation
- Built-in support for multiple transformation scenarios
- Type-safe transformation with PHP 8.3 features
- Preservation of original data types
- Flexible formatting options for various data types
You can install the Transformer component via Composer:
composer require kariricode/transformer
- PHP 8.3 or higher
- Composer
- Extensions:
ext-mbstring
,ext-json
- Define your data class with transformation attributes:
use KaririCode\Transformer\Attribute\Transform;
class DataFormatter
{
#[Transform(
processors: ['date' => ['inputFormat' => 'd/m/Y', 'outputFormat' => 'Y-m-d']]
)]
private string $date = '25/12/2024';
#[Transform(
processors: ['number' => ['decimals' => 2, 'decimalPoint' => ',', 'thousandsSeparator' => '.']]
)]
private float $price = 1234.56;
#[Transform(
processors: ['mask' => ['type' => 'phone']]
)]
private string $phone = '11999887766';
// Getters and setters...
}
- Set up the transformer and use it:
use KaririCode\ProcessorPipeline\ProcessorRegistry;
use KaririCode\Transformer\Transformer;
use KaririCode\Transformer\Processor\Data\{DateTransformer, NumberTransformer};
use KaririCode\Transformer\Processor\String\MaskTransformer;
$registry = new ProcessorRegistry();
$registry->register('transformer', 'date', new DateTransformer());
$registry->register('transformer', 'number', new NumberTransformer());
$registry->register('transformer', 'mask', new MaskTransformer());
$transformer = new Transformer($registry);
$formatter = new DataFormatter();
$result = $transformer->transform($formatter);
if ($result->isValid()) {
echo "Date: " . $formatter->getDate() . "\n"; // Output: 2024-12-25
echo "Price: " . $formatter->getPrice() . "\n"; // Output: 1.234,56
echo "Phone: " . $formatter->getPhone() . "\n"; // Output: (11) 99988-7766
}
Here's an example of how to use the KaririCode Transformer in a real-world scenario, demonstrating various transformation capabilities:
use KaririCode\Transformer\Attribute\Transform;
class ComplexDataTransformer
{
#[Transform(
processors: ['case' => ['case' => 'snake']]
)]
private string $text = 'transformThisTextToSnakeCase';
#[Transform(
processors: ['slug' => []]
)]
private string $title = 'This is a Title for URL!';
#[Transform(
processors: ['arrayKey' => ['case' => 'camel']]
)]
private array $data = [
'user_name' => 'John Doe',
'email_address' => 'john@example.com',
'phone_number' => '1234567890'
];
#[Transform(
processors: [
'template' => [
'template' => 'Hello {{name}}, your order #{{order_id}} is {{status}}',
'removeUnmatchedTags' => true,
'preserveData' => true
]
]
)]
private array $templateData = [
'name' => 'John',
'order_id' => '12345',
'status' => 'completed'
];
// Getters and setters...
}
-
CaseTransformer: Transforms string case (camel, snake, pascal, kebab).
- Configuration Options:
case
: Target case format (lower, upper, title, sentence, camel, pascal, snake, kebab)preserveNumbers
: Whether to preserve numbers in transformation
- Configuration Options:
-
MaskTransformer: Applies masks to strings (phone, CPF, CNPJ, etc.).
- Configuration Options:
mask
: Custom mask patterntype
: Predefined mask typeplaceholder
: Mask placeholder character
- Configuration Options:
-
SlugTransformer: Generates URL-friendly slugs.
- Configuration Options:
separator
: Separator characterlowercase
: Convert to lowercasereplacements
: Custom character replacements
- Configuration Options:
-
TemplateTransformer: Processes templates with variable substitution.
- Configuration Options:
template
: Template stringremoveUnmatchedTags
: Remove unmatched placeholderspreserveData
: Keep original data in result
- Configuration Options:
-
DateTransformer: Converts between date formats.
- Configuration Options:
inputFormat
: Input date formatoutputFormat
: Output date formatinputTimezone
: Input timezoneoutputTimezone
: Output timezone
- Configuration Options:
-
NumberTransformer: Formats numbers with locale-specific settings.
- Configuration Options:
decimals
: Number of decimal placesdecimalPoint
: Decimal separatorthousandsSeparator
: Thousands separatorroundUp
: Round up decimals
- Configuration Options:
-
JsonTransformer: Handles JSON encoding/decoding.
- Configuration Options:
encodeOptions
: JSON encoding optionspreserveType
: Keep original data typeassoc
: Use associative arrays
- Configuration Options:
-
ArrayFlattenTransformer: Flattens nested arrays.
- Configuration Options:
depth
: Maximum depth to flattenseparator
: Key separator for flattened structure
- Configuration Options:
-
ArrayGroupTransformer: Groups array elements by key.
- Configuration Options:
groupBy
: Key to group bypreserveKeys
: Maintain original keys
- Configuration Options:
-
ArrayKeyTransformer: Transforms array keys.
- Configuration Options:
case
: Target case for keysrecursive
: Apply to nested arrays
- Configuration Options:
-
ArrayMapTransformer: Maps array keys to new structure.
- Configuration Options:
mapping
: Key mapping configurationremoveUnmapped
: Remove unmapped keysrecursive
: Apply to nested arrays
- Configuration Options:
-
ChainTransformer: Executes multiple transformers in sequence.
- Configuration Options:
transformers
: Array of transformers to executestopOnError
: Stop chain on first error
- Configuration Options:
-
ConditionalTransformer: Applies transformations based on conditions.
- Configuration Options:
condition
: Condition callbacktransformer
: Transformer to applydefaultValue
: Value when condition fails
- Configuration Options:
Transformers can be configured globally or per-instance. Example of configuring the NumberTransformer:
use KaririCode\Transformer\Processor\Data\NumberTransformer;
$numberTransformer = new NumberTransformer();
$numberTransformer->configure([
'decimals' => 2,
'decimalPoint' => ',',
'thousandsSeparator' => '.',
]);
$registry->register('transformer', 'number', $numberTransformer);
The Transformer component integrates with:
- KaririCode\Contract: Provides interfaces for component integration
- KaririCode\ProcessorPipeline: Used for transformation pipelines
- KaririCode\PropertyInspector: Processes transformation attributes
Complete registry setup example:
$registry = new ProcessorRegistry();
// Register String Transformers
$registry->register('transformer', 'case', new CaseTransformer())
->register('transformer', 'mask', new MaskTransformer())
->register('transformer', 'slug', new SlugTransformer())
->register('transformer', 'template', new TemplateTransformer());
// Register Data Transformers
$registry->register('transformer', 'date', new DateTransformer())
->register('transformer', 'number', new NumberTransformer())
->register('transformer', 'json', new JsonTransformer());
// Register Array Transformers
$registry->register('transformer', 'arrayFlat', new ArrayFlattenTransformer())
->register('transformer', 'arrayGroup', new ArrayGroupTransformer())
->register('transformer', 'arrayKey', new ArrayKeyTransformer())
->register('transformer', 'arrayMap', new ArrayMapTransformer());
Similar development setup as the Validator component, using Docker and Make commands.
make up
: Start servicesmake down
: Stop servicesmake test
: Run testsmake coverage
: Generate coverage reportmake cs-fix
: Fix code stylemake quality
: Run quality checks
Contributions are welcome! Please see our Contributing Guide.
MIT License - see LICENSE file.
- Documentation: https://kariricode.org/docs/transformer
- Issues: GitHub Issues
- Forum: KaririCode Club Community
- Stack Overflow: Tag with
kariricode-transformer
Built with ❤️ by the KaririCode team. Empowering developers to create more secure and robust PHP applications.