lucleads / automap
Objects mapper library
This package is auto-updated.
Last update: 2025-06-22 00:05:25 UTC
README
Description
Library that automatically copies the attributes of an object to a Data Transfer Object.
This library works in a similar way to libraries like MapStruct in other languages such as Java.
DEPLOY
To deploy a sandbox of the library, modify de .env
file located in the project root with your local settings, open a terminal in the root directory and execute the next command:
docker-compose up -d
[to build and deploy the docker container]
*NOTE: If you don't modify the .env
file, the default values are:
- Container name: dto-mapper
- Php version: 8.0.10
- Deployable port: 81
HOW TO USE IT
In the path /src/app/ExampleUseCase
you can find an example of how to implement a mapper.
The purpose of this library is to copy the values of the common fields of two objects.
For each pair of objects we have to create a mapper class (Example: PersonOutputDtoMapper
).
That mapper class must extend from the abstract class Mapper
and must contain in its constructor the entity which contains the fields values.
In that class, we can make a function (map()
) that returns an instance of the class needed, for example a Data Transfer Object class.
The only content of this function should be a static call to its parent class method mapAutomatically()
with the next parameters:
- 1st parameter: The source object
- 2nd parameter: The output object class expected
- 3rd parameter:
self::class
(The mapper class)
To make our mapper find the source for the value of each field, there are three ways to do it:
-
The value is in a public field with the same name in the source object and in the target object.
-
The source object have a getter with the same name of the field of the target object.
-
Example:
[ "TargetObject", { "age": ? } ]
[ "SourceObject": { "lifetime": 40 } ]
class SourceObject { private int $lifetime; public function getAge(): int { return $this->lifetime; } }
-
-
In the specific MapperClass, we can define the layers to find the value as a class Map attribute.
-
Example:
[ "TargetObject", { "age": ? } ]
[ "SourceObject": { "Age": { "value": 40 } } ]
#[Map('Age.value', 'age')] class SourceObjectMapper { //logic }
*NOTE: To define a map attributes, we must follow the next structure:
#[Map(
+
layers up the value separated by dots as string
+,
+field name in target object as string
+]
-
REQUIREMENTS
Needed to deploy the proyect:
Tools used in docker build (Don't need previous installation):
*NOTE: The PHP version established in the .env
file must be 8.0 or higher.