frank-houweling/cartograph

将对象映射到对象

3.0.0 2019-10-14 14:37 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来创建自定义映射。