etshy/automapper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

etshy/automapper

最新稳定版本:1.2

Composer 安装命令:

composer require etshy/automapper

包简介

Automapper for PHP

README 文档

README

pipeline status coverage report

A lightweight library to quickly map an object or an array to another object, greatly inspired by Automapper-plus. I hope this cover most of you use-cases

This library doesn't use any Reflection !

Installation

composer require etshy/automapper

Future bundle for symfony here

Usage

Here is an example on how to configure a Mapping

<?php

use Etshy\AutoMapper\Configuration\AutoMapperConfiguration;
use Etshy\AutoMapper\AutoMapper;

$config = new AutoMapperConfiguration();

$config
    ->registerMapping(Source::class, Target::class) // -> this is enough if Source and Target have the same property name
    ->forMember('updatedAt', function (Source $source) { // -> use callback function or PropertyMapper on the forMember method
        return $source->getCreatedAt();
    });
                            
$mapper = new AutoMapper($config);

$source = new Source();
$target = $mapper->map($source, Target::class);
// $mapper->mapToObject(source,target); -> use mapToObject() to map to an existing object
// $mapper->mapMultiple([$source],Target::class); -> use mapMultiple to map an iterable of source 

echo $target->getUpdatedAt();

PropertyMapper

PropertyMappers are used to customize a mapping

FromCallable

PropertyMapper::fromCallable(function($souce) {})

equivalent to directly using a callback.

FromProperty

Propertymapper::fromProperty('propName')

Defines from which property you get the data

ToClass

PropertyMapper::toClass($targetClass, $sourceIsObjectArray)

Defines a class to map the property, useful if you have nested classes. You'll have to register a mapping for the subclasses

Ignore

PropertyMapper::ignore()

Simply ignore the target property

Options

IgnoreNullProperties/dontIgnoreNullProperties

$mapping = $this->config->registerMapping(Source::class, Target::class);
$mapping->getOptions()->ignoreNullProperties();

This option will ignore the source's null properties. Particularly useful when used with mapToObject, but can be useful if you have default value in you Target class

setPropertyWriter/setPropertyReader

$mapping = $this->config->registerMapping(Source::class, Target::class);
$mapping->getOptions()->setPropertyWriter(new SwaggerModelPropertyAccessor());

This option is useful if you want to use another PropertyReader/PropertyWriter than the default ones (ObjectPropertyAccessor and ArrayPropertyAccessor). I provide a SwaggerModelPropertyAccessor for Swagger generated Models.

And that's it (for now). Hope this light library can help you !

统计信息

  • 总下载量: 16
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固