The Mapper
class under the Dalue
namespace offers a powerful and flexible method for data transformation and restructuring. This class allows you to create a new structure from a given data set, optionally performing transformations on the data in the process.
Before you begin using the Mapper
class, ensure that the library containing the StrObj\StringObjects
class is installed. The Mapper
class utilizes the functionalities provided by this class for data access and manipulation.
First, install the required packages via Composer:
composer require uuur86/dalue:0.1.1-beta
Below is an example of how to use the Mapper
class:
use Dalue\Mapper;
use StrObj\StringObjects;
// Sample data set
$data = new StringObjects([
'user' => [
'id' => 1,
'name' => 'John Doe',
'email' => '[email protected]',
'details' => [
'age' => 30,
'city' => 'New York'
]
]
]);
// Defining the data structure we want to transform into.
$paths = [
'userID' => '@data/user/id',
'userName' => '@data/user/name',
'userDetails' => [
'userAge' => '@data/user/details/age',
'userCity' => '@data/user/details/city'
]
];
// Using the Mapper::map function to perform the transformation.
$mappedData = Mapper::map($data, $paths);
print_r($mappedData);
- Data Initialization:
- A sample data set is created using the
StringObjects
class.
- A sample data set is created using the
- Path Definition:
- The structure we want to transform into is defined in the
$paths
array.
- The structure we want to transform into is defined in the
- Data Transformation:
- The
Mapper::map
function is used to transform the data according to the defined paths.
- The
The above example will produce the following output:
Array
(
[userID] => 1
[userName] => John Doe
[userDetails] => Array
(
[userAge] => 30
[userCity] => New York
)
)
You can also perform more complex transformations by incorporating custom functions in the mapping paths. Here is an example:
use Dalue\Mapper;
use StrObj\StringObjects;
$data = new StringObjects([
'user' => [
'id' => 1,
'name' => 'John Doe',
'email' => '[email protected]',
'details' => [
'age' => 30,
'city' => 'New York'
]
]
]);
$paths = [
'userID' => '@data/user/id',
'userName' => '@data/user/name',
'userEmail' => function($data) {
return strtoupper($data->get('user.email'));
},
'userDetails' => [
'userAge' => '@data/user/details/age',
'userCity' => '@data/user/details/city',
'description' => function($data) {
return $data->get('user.details.age') . ' years old, lives in ' . $data->get('user.details.city');
}
]
];
$mappedData = Mapper::map($data, $paths);
print_r($mappedData);
The advanced example will produce the following output:
Array
(
[userID] => 1
[userName] => John Doe
[userEmail] => JOHN.DOE@EXAMPLE.COM
[userDetails] => Array
(
[userAge] => 30
[userCity] => New York
[description] => 30 years old, lives in New York
)
)
This project is licensed under the GPL-3.0-or-later License. See the LICENSE file for more details.
Uğur Biçer - @uuur86
If you want to contribute to this project, you can send pull requests. We expect all contributors to follow our Code of Conduct.
You can contact me via email: [email protected]
You can report bugs via GitHub issues.
If you find a security issue, please report it via email: [email protected]
If you want to support me, you can donate via GitHub sponsors: https://github.com/sponsors/uuur86
- uuur86/strobj - PHP String Objects
- uuur86/wpoauth - WordPress OAuth2 Client
- @codeplusdev - Codeplus Development