emreuyguc/structured-mapper-bundle
This Symfony bundle enables seamless usage of the emreuyguc/structured-mapper package within Symfony applications.
It is designed to manage DTO ↔ Entity transformations in a simple and powerful way.
Add the Euu Structured Mapper to your project using Composer:
composer require emreuyguc/structured-mapper-bundle
You can use Symfony autowiring as follows:
public function __construct(private readonly StructuredMapper $mapper)
{
}
Alternatively, you can call the structured_mapper service from the container.
use Euu\Bundle\StructuredMapperBundle\StructuredMapper\StructuredMapper;
$mapper = $container->get('structured_mapper');
$inputDto = new InputDto();
$mapper->map(sourceObject: $source, targetClass: MyEntity::class, context: [
//object mapper context parameters..
]);
src/config/packages/structured_mapper.yaml
structured_mapper:
default_mapper_context:
# see ObjectMapper settings
cache:
enabled: true
ttl: 3600
prefix: 'structured_mapper.'
service: 'cache.app'
preload:
enabled: true
readers:
attribute_reader:
instance_of: ~ #example : 'App\Dto\BaseDto'
read_directory: ~ #currently not supported
$inputDto = new InputDto();
$target = new Person(name:'Emre', age: 28);
$mapper->map(sourceObject: $source, targetClass: Person::class, context: [
ObjectMapper::TO_POPULATE => $target
]);
The default context parameters defined for the ObjectMapper are as follows;
public const SKIP_NULL_VALUES = 'skip_null_values';
public const TO_POPULATE = 'to_populate';
public const ALLOW_AUTO_MAPPING = 'allow_auto_mapping';
public const GROUPS = 'groups';
public const AUTO_SUB_MAPPING = 'auto_sub_mapping';
public const TYPE_ENFORCEMENT = 'type_enforcement';
public const ARRAY_ADD_METHOD = 'array_add_method';
public const ARRAY_CLEAR_METHOD = 'array_clear_method';
public const ARRAY_CLEAR_EXPRESSION = 'array_clear_expression';
You can find the latest list of parameters here.
#[MapTo(
ProductEntity::class,
mappings: [
new Mapping('productCode', 'productNumber'),
new Mapping('barcode', 'barcodeNumber')
],
mapperContext: [
ObjectMapper::SKIP_NULL_VALUES => true,
ObjectMapper::ALLOW_AUTO_MAPPING => false,
ObjectMapper::TYPE_ENFORCEMENT => false
])]
class ProductDto{
class StockDto
{
#[OnMapTo(StockEntity::class, targetPath: 'stock')]
public ?int $stockCount = null;
#[OnMapTo(StockEntity::class, targetPath: 'warehouse')]
public ?string $stockWarehouse = null;
}
For a nested target property:
#[OnMapTo(ProductEntity::class, targetPath: 'owner.fullName')]
public string $ownerName;
For a nested source property:
#[MapTo(
ProductEntity::class,
mappings: [
new Mapping('stock.stockWarehouse', 'stockWarehouse')
])]
class ProductDto
{
#[OnMapTo(
targetClass: ProductEntity::class,
transformerMeta: new WithMapper(targetClass: StockEntity::class)
)]
public StockDto $stock;
#[OnMapTo(ProductEntity::class, transformerMeta: new ArrayItemTransform(
itemTransformerMeta: new WithMapper(targetClass: SellerEntity::class)
))]
public array $sellers;
Note: ImplodeTransformer merges multiple source properties into one target property. Can only be used with MapTo as OnMapTo only supports single source.
#[MapTo(
ProductEntity::class,
mappings: [
new Mapping(['sku', 'code'], 'productCode', new ImplodeTransform('-')),
])]
#[OnMapTo(ProductEntity::class, targetPath: 'model', transformerMeta: new ExplodeTransform('-', 1, 2))]
#[OnMapTo(ProductEntity::class, targetPath: 'brand', transformerMeta: new ExplodeTransform('-', 0, 2))]
public string $brandModel;
#[OnMapTo(ProductEntity::class, targetPath: 'unit', transformerMeta: new EnumTransform(UnitType::class))]
public string $unit;
Basic usage with default ID resolver:
#[OnMapTo(ProductEntity::class, targetPath: 'taxGroup', transformerMeta: new EntityResolve(TaxGroup::class))]
public string $sellTaxGroupId;
Advanced usage for updatable collections:
#[OnMapTo(
ProductEntity::class,
targetPath: 'subCategories',
transformerMeta: new ArrayItemTransform(
itemTransformerMeta: new EntityResolve(SubCategoryEntity::class),
transformerContext: [
ArrayItemTransformer::USE_ADD_METHOD => 'addSubCategory',
ArrayItemTransformer::CLEAR_METHOD => 'subCategories.clear()',
ArrayItemTransformer::CLEAR_EXPRESSION => "'update' in context['groups']"
]
)
)]
public array $subCategoryIds;
EntityResolve class parameter descriptions:
a -> b mappings are defined, reverse mappings (b -> a) are theoretically possible, but currently unsupported. (Priority: Medium)type_enforcement to false for simple cases. (Priority: Low)canMap method could be added for verification. (Priority: High)This package is licensed under the MIT License.
How can I help you explore Laravel packages today?