Download the PHP package ehyiah/mapping-bundle without Composer
On this page you can find all versions of the php package ehyiah/mapping-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mapping-bundle
MappingBundle
Symfony bundle to automap an object into another object easily.
It was mainly built in order to avoid passing full entities to symfony forms when you want to edit them. But you can use it in many ways.
It was designed to be as easy as possible, all the mapping is located into a single object that is "aware" of the target class and properties destinations. There is no configuration outside of the main object.
The main service have two methods :
-
mapToTarget : This method will map the mapped Aware object to a target object. The target object can be another simple object or an entity. e.g : You have a form with a data_class that is an object that will represent some properties of an entity, for a User for example.
- mapFromTarget : this method will do the opposite. It will take an object (or an entity that is tagged as the target) and map properties into the mapped Aware object.
Installation
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Applications that use Symfony Flex
Open a command console, enter your project directory and execute:
Applications that don't use Symfony Flex
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
Usage
In order to use this bundle, you will need to mark an object (basically a DTO) with the attribute Ehyiah\MappingBundle\Attributes\MappingAware
And in the same class, simply tag every properties you want to map to the target object with the same attribute. If the properties in the target class and the mapped DTO have the same name, there is nothing else to do (see property example). If the properties have a different name, you can use the option to specify the name in the target class.
As the mapping logic is using the propertyAccess component, you can specify nested properties in the target class. see and properties in the example below.
If you want to ignore null
values when mapping from an object to the other, juste use the ignoreNullValue
option on each properties you need to.
Simple Usage
1 - Create a DTO that will 'hold' the mapping logic
2 - Inject the into your code, and call the methods.
If you call function of the service tagged with in a controller, handler or wherever you need it, the service will take properties tagged with MappingAware attribute and will put the values stored in the DTO into the properties in the entity and flush them (default value).
Advanced usage
Replace the Built-in service
A Default service implementing exist in the bundle. But you may need to create a service that implements the to replace the default built-in.
Create your own service
To create your own service :
- Just create a service that implements the , there is nothing more to do, no need to register or override the default built-in. The compiler pass do the work for you.
- In this service you will handle the logic yourself.
- Of course you can take the basis methods of the default service and modify them to your needs inside your custom service.
Transformers
Sometimes you need to modify data between the objects.
Example : in your SourceObject you have a string and need a Datetime in the TargetObject. Or the opposite
Well there is a simple way to do this via Transformers. You can easily create them or use some of prebuilt. To create them just create a class and implements
Transformers will have 2 methods and . and you can pass an array of options that will be used in both.
method is used in mapToTarget
method is used in mapFromTarget
In each method you have access ot the SourceObject and the TargetObject.
Going Further with Transformers
Transformers can be "open-minded" or "narrow-minded". For a better understanding there is an easy-to-understand example built-in with and .
Which one to choose is entirely to your mind ! The only difference between them is how you code the transform and reverseTransform methods.
Narrow-minded transformers
If you pick as examples the : It is said as a narrow-minded transformer as it will only accept to transform String to DateTime and reverseTransform DateTime to String. It will pick a string from the sourceObject and transform it into a DateTime object in the target object (via the mapToTarget method). And will pick the DateTime object from the target object and reverseTransform it into the sourceObject (via the mapFromTarget method)
If you try to do the opposite an Exception will be thrown.
Open-minded transformers
If you pick as examples the : It is said as an open-minded transformer as it will accept :
- to transform String to DateTime and reverseTransform DateTime to String.
- but also to transform Datetime to String and reverseTransform String to Datetime.
List Of built-in Transformers
Open-minded :
Transformer | transform and reverseTransform | options |
---|---|---|
DateTimeTransformer | string to DateTime OR DateTime to string | (use to transform the string with the provided format) example a valid DateTimeZone object example : |
BooleanTransformer | string|int to boolean OR boolean to string|int | or example: : true or false |
EnumTransformer | Enum|Enum[] into string|string[] OR string|string[] to enum|enum[] | the class of the enum example : |
Narrow-minded:
Transformer | transform | transform available options | reverseTransform | reverseTransform available options |
---|---|---|---|---|
StringToDateTimeTransformer | string to DateTime | a valid DateTimeZone object example : | DateTime to string | (use to transform the string with the provided format) example |
StringToBooleanTransformer | string to Boolean | : true or false | Boolean to string | or example: |
StringToEnumTransformer | string or array of strings to enum or array of enum | the class of the enum example : | enum or array of enum to string or array of strings | the class of the enum example : in case you would need to return the NAME instead of the VALUE example: |
CollectionTransformer | collection of object to collection of object | the expected class for elements in the collection (mandatory), the collection property from where you would load data to simulate | collection of object to collection of object | the current class of elements in the collection (mandatory) |
Mapped collection inside Mapped object
If you need to have sub mapped elements inside your initial mapped class, the is the perfect fit.
- it can allow you to map collection elements to another (or also same to benefit of other feature!) class
- it can simulate the clearMissing feature to avoid default mapping clear on element not posted
- it can be deep chained
All versions of mapping-bundle with dependencies
doctrine/orm Version ^2.10|3.0
symfony/property-access Version ^6.1|^7.0
symfony/monolog-bundle Version ^3.1
symfony/yaml Version ^6.1|^7.0