DataMapper
DataMapper is a powerful tool for transforming data structures. It provides a fluent API for mapping, filtering and transforming data with templates, pipelines and SQL-like queries. Supports multi-format output (JSON, XML, YAML, CSV).
Quick Example
Section titled “Quick Example”use event4u\DataHelpers\DataMapper;
$source = [ 'profile' => [ 'firstName' => 'John', 'lastName' => 'Doe', 'contact' => [ 'email' => 'john@example.com', 'phone' => '+1234567890', ], ], 'settings' => [ 'theme' => 'dark', 'language' => 'en', ],];
// Simple template mapping$result = DataMapper::source($source) ->template([ 'name' => 'profile.firstName', 'email' => 'profile.contact.email', 'theme' => 'settings.theme', ]) ->map();
// Result:// [// 'name' => 'John',// 'email' => 'john@example.com',// 'theme' => 'dark',// ]Key Features
Section titled “Key Features”Template Expressions
Section titled “Template Expressions”Map data using simple dot-notation paths or complex expressions:
DataMapper::source($data) ->template([ 'fullName' => '{{ profile.firstName }} {{ profile.lastName }}', 'email' => 'profile.contact.email', 'isActive' => 'status.active', ]) ->map();Filtering Data
Section titled “Filtering Data”Filter data using array functions:
$users = [ ['name' => 'John', 'age' => 25, 'status' => 'active'], ['name' => 'Jane', 'age' => 17, 'status' => 'active'], ['name' => 'Bob', 'age' => 30, 'status' => 'inactive'],];
$filtered = array_filter($users, function($user) { return $user['age'] > 18 && $user['status'] === 'active';});
$filtered = array_slice($filtered, 0, 10);Transforming Data
Section titled “Transforming Data”Transform data using DataMapper:
$data = [ ['name' => ' john ', 'email' => 'john@example.com'], ['name' => ' jane ', 'email' => 'jane@example.com'],];
$result = DataMapper::source($data) ->template([ 'name' => '{{ name | trim | upper }}', 'email' => '{{ email }}', ]) ->map();When to Use DataMapper
Section titled “When to Use DataMapper”Use DataMapper when you need to:
- Transform API responses - Map external API data to your internal structure
- Aggregate data - Group and summarize data with SQL-like operations
- Filter collections - Query data with complex conditions
- Normalize data - Convert data from one format to another
- Build ETL pipelines - Extract, transform and load data
Performance
Section titled “Performance”DataMapper is optimized for performance:
- Lazy evaluation - Only processes data when needed
- Efficient memory usage - Streams large datasets
- Cached templates - Compiled templates for repeated use
- Minimal overhead - Direct array access without unnecessary copies
Learn More
Section titled “Learn More”For detailed documentation and advanced features, see:
- DataMapper Introduction - Complete guide
- Template Expressions - Advanced templating
- Query Builder - SQL-like queries
- GROUP BY Operator - Aggregation and grouping
- Pipelines - Transformation chains
- Reverse Mapping - Bidirectional mapping