senet-eindhoven / cartograph
Map objects to objects
4.0.1
2023-07-17 12:27 UTC
Requires
- php: ^7.4||^8.0
- psr/container: ^1.0||^2.0
Requires (Dev)
- phpunit/phpunit: ^8.5
Suggests
- doctrine/orm: Allows usage of the EntityMappingRepository, which handles proxy classes.
This package is auto-updated.
Last update: 2024-12-17 15:50:47 UTC
README
This library is used to Map PHP object to other objects
Installation
Install this library using composer
composer require senet-eindhoven/cartograph:^4.0
Getting Started
To use this library create an instance of the MapperService
, using your preffered MappingRepositoryInterface
<?php use Senet\Cartograph\MapperService; use Senet\Cartograph\Mapping\DirectMapping; use Senet\Cartograph\MappingRepository; $foo = new Foo(); $bar = new Bar(); // Initiate the MapperService $mappingRepository = new MappingRepository(); $mapperService = new MapperService($mappingRepository); // Register a mapping from Foo, to Bar using the DirectMapping $mappingRepository->addMapping(Foo::class, Bar::class, DirectMapping::class); // Map Foo -> Bar. Will fetch the previously registered Mapping $mapperService->map($foo, $bar);
Creating custom Mappings
The DirectMapping
uses reflection to map attributes 1:1. Should this not meet your requirements, you can create
a custom mapping by implementing the MappingInterface
and registering it to the MappingRepository
as shown above.