frank-houweling / cartograph
将对象映射到对象
3.0.0
2019-10-14 14:37 UTC
Requires
- php: ^7.1
- psr/container: ^1.0.0
Suggests
- doctrine/orm: Allows usage of the EntityMappingRepository, which handles proxy classes.
This package is auto-updated.
Last update: 2024-09-28 16:36:23 UTC
README
这个库用于将PHP对象映射到其他对象
安装
使用composer安装此库
composer require frank-houweling/cartograph
入门
要使用此库,创建一个MapperService
实例,使用您首选的MappingRepositoryInterface
<?php use FrankHouweling\Cartograph\MapperService; use FrankHouweling\Cartograph\Mapping\DirectMapping; use FrankHouweling\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);
创建自定义映射
DirectMapping
使用反射将属性1:1映射。如果这不符合您的需求,您可以通过实现MappingInterface
并注册到上面显示的MappingRepository
来创建自定义映射。