zelak / mapper

此包已被 废弃 且不再维护。未建议替代包。
此包的最新版本(1.2.0)没有可用的许可证信息。

1.2.0 2021-07-01 23:03 UTC

This package is auto-updated.

Last update: 2023-10-18 03:57:08 UTC


README

安装

composer require zelak/mapper

用法

创建简单的 DTO

DTO 需要类型提示才能使 Mapper 正确工作

class UserDto {
    public int $id;
    public string $username;
    public string $password;
}

创建 Mapper、注册和使用 Mapper

$mapper = new Mapper(); // Creating
$mapper->createMap(UserDto::class); // Registering

// Using
$user = new stdClass();
$user->id = "1";
$user->username = "username";
$user->password = "password":

$userDto = $mapper->map($user, UserDto::class);

结果

// Before
{
    "id": "1",
    "username": "username",
    "password": "password"
}

// After
{
    "id": 1,
    "username": "username",
    "password": "password"
}

创建关联 DTO

class ProductDto {
    public string $name;
    public BuyerDto $buyer;
}

class BuyerDto {
    public string $name;
}

创建 Mapper、注册和使用 Mapper

$mapper = new Mapper(); // Creating
$mapper->createMap(ProductDto::class); // Registering
$mapper->createMap(BuyerDto::class);

// Using
$buyer = new stdClass();
$buyer->name = "buyerName";
$product = new stdClass();
$product->name = "propName";
$product->buyer = $buyer;

$productDto = $mapper->map($product, ProductDto::class);

结果

// Before
{
    "name": "propName",
    "buyer": {
        "name": "buyerName"
    }
}

// After
{
    "name": "propName",
    "buyer": {
        "name": "buyerName"
    }
}

创建关联数组 DTO

class ProductArrDto {
    public string $name;
    public array $buyer;
}

class BuyerDto {
    public string $name;
}

创建 Mapper 并注册 Mapper,由于 ProductArrDto 使用数组,我们需要指定它需要使用的类
我们通过在创建 Map 时使用 specify 函数来实现这一点

$mapper = new Mapper(); // Creating
$mapper->createMap(ProductArrDto::class) // Registering
    ->specify("buyer", BuyerDto::class); // <-- specifying here
$mapper->createMap(BuyerDto::class);

结果

// Before
{
    "name": "propName",
    "buyer": [
        {
            "name": "buyerName"
        }
    ]
}

// After
{
    "name": "propName",
    "buyer": [
        {
            "name": "buyerName"
        }
    ]
}

文档

Mapper 类

为某个类创建映射

createMap(string className)
* className -> The name of the class to register the map for

将映射对象映射到已注册的类

map(mixed data, string className)
* data      -> The data to map to the class
* className -> The name of the class to map to

MappedClass 类

自定义对象到类的转换

from(Closure closure)
* closure -> A closure with first parameter being the From Object and the second parameter being the To Object

示例

$mapper = new Mapper();
$mapper->createMap(UserDto::class)
    ->from(function ($from, $to) {
        $to->username = $from->username . " $from->id"
    };);

结果

// Before
{
    "id": "1",
    "username": "username",
    "password": "password"
}

// After
{
    "id": "1",
    "username": "username 1",
    "password": "password"
}

忽略对象中的一个属性

ignore(string propName)
* propName -> The property name to ignore when mapping

示例

$mapper = new Mapper();
$mapper->createMap(UserDto::class)
    ->ignore("password")

结果

// Before
{
    "id": "1",
    "username": "username",
    "password": "password"
}

// After
{
    "id": "1",
    "username": "username"
}

指定一个关联数组到 DTO

specify(string propName, string toClass)
* propName -> The property name to specify the link to
* toClass  -> The name of the class that the array represents

重命名属性

renameProp(string propName, string renamedPropName)
* propName -> The property name to rename
* renamedPropName  -> The renamed property name
class UserRenameDto {
    public string $id;
    public string $name;
    public string $password;
}

创建 Mapper、注册和使用 Mapper

$mapper = new Mapper(); // Creating
$mapper->createMap(UserRenameDto::class) // Registering
    ->renameProp("username", "name"); // Rename the property

// Using
$user = new stdClass();
$user->id = "1";
$user->username = "username";
$user->password = "password":

$userDto = $mapper->map($user, UserRenameDto::class);

结果

// Before
{
    "id": "1",
    "username": "username",
    "password": "password"
}

// After
{
    "id": "1",
    "name": "username",
    "password": "password"
}

数组关联到具有特定属性的数组

toArrayProp(string propName, string propToUse)
* propName -> The property name for the array
* propToUse-> The property to use when making the array

创建 Mapper、注册和使用 Mapper

$mapper = new Mapper(); // Creating
$mapper->createMap(ProductDto::class) // Registering
    ->toArrayProp("buyer", "name"); // Map to array
$mapper->createMap(BuyerDto::class);

// Using
$buyer = new stdClass();
$buyer->name = "buyerName";
$product = new stdClass();
$product->name = "propName";
$product->buyer = $buyer;

$productDto = $mapper->map($user, ProductDto::class);

结果

// Before
{
    "name": "propName",
    "buyer": [
        {
            "name": "buyerName"
        }
    ]
}

// After
{
    "name": "propName",
    "buyer": [
        "buyerName"
    ]
}