senet-eindhoven/cartograph

将对象映射到其他对象

4.0.1 2023-07-17 12:27 UTC

This package is auto-updated.

Last update: 2024-09-17 15:19:17 UTC


README

此库用于将PHP对象映射到其他对象

安装

使用composer安装此库

composer require senet-eindhoven/cartograph:^4.0

入门指南

要使用此库,创建一个MapperService实例,使用您首选的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);

创建自定义映射

DirectMapping使用反射将属性1:1映射。如果这不符合您的要求,您可以创建自定义映射,通过实现MappingInterface并将其注册到上面显示的MappingRepository中。