guyliangilsing/php-class-mapper

一个简单的包,为您提供简单的类映射系统。

1.1.2 2022-11-23 13:02 UTC

This package is auto-updated.

Last update: 2024-09-23 17:07:16 UTC


README

一个简单的包,为您提供简单的类映射系统。

目录

特性

PHPClassMapper具有以下特性

  • 类到类映射
  • 数组到类映射
  • 类到数组映射

安装

$ composer require guyliangilsing/php-class-mapper

使用

类到类映射

配置映射器

在创建映射类之前,您需要提供一个配置类。您可以通过以下方式创建配置

use PHPClassMapper\Configuration\MapperConfiguration;

$configuration = new MapperConfiguration();

在此配置中,您可以使用以下代码添加映射

use PHPClassMapper\Configuration\MapperConfiguration;

$configuration = new MapperConfiguration();
$configuration->addMapping(Source::class, Destination::class, new MyMapping());

创建映射

为了让映射器知道如何映射您的类,您需要提供映射。映射可以创建如下

use PHPClassMapper\Exceptions\MissingContextDataFieldException;
use PHPClassMapper\Exceptions\MissingMappingException;
use PHPClassMapper\MapperInterface;

class MyMapping implements MappingInterface
{
    /**
     * Maps one class into another class.
     *
     * @param object $source The class that needs to be mapped to a different class.
     * @param array<string, mixed> $contextData An associative array (key => value) that gives the mapper additional
     * data to work with.
     * @param MapperInterface $mapper An extra mapper instance to map objects with.
     *
     * @throws InvalidArgumentException when a `MyClass::name` string refers to a class that does not exist.
     * @throws MissingMappingException when a mapping between two class types can't be found.
     * @throws MissingContextDataFieldException when a specific context data field can't be found.
     */
    public function mapObject(object $source, array $contextData, MapperInterface $mapper): object;
    {
        if (!($source instanceof Source))
        {
            throw new MissingMappingException($source::class, Destination::class);
        }

        return new Destination();
    }
}

创建映射器

一旦您有了配置,您就可以实例化映射器

use PHPClassMapper\Configuration\MapperConfiguration;
use PHPClassMapper\Mapper;

$configuration = new MapperConfiguration();

// Do your configuration logic here...

$mapper = new Mapper($configuration);

使用映射器

一旦实例化了映射器,您可以使用以下方式使用它

use PHPClassMapper\Configuration\MapperConfiguration;
use PHPClassMapper\Mapper;

$configuration = new MapperConfiguration();

// Do your configuration logic here...

$mapper = new Mapper($configuration);

// Without contextvariables
$mapper->map($source, Destination::class);

// With context variables
$mappedClass = $mapper->map($source, Destination::class, [
    'contextFieldOne' => 'Hello',
    'contextFieldTwo' => 'World'
]);

数组映射

配置映射器

在创建映射类之前,您需要提供一个配置类。您可以通过以下方式创建配置

use PHPClassMapper\Configuration\ArrayMapperConfiguration;

$configuration = new ArrayMapperConfiguration();

在此配置中,您可以添加两种不同的映射类型

  • 到数组映射(类 -> 数组)
  • 从数组映射(数组 -> 类)

到数组映射

use PHPClassMapper\Configuration\ArrayMapperConfiguration;

$configuration = new ArrayMapperConfiguration();
$configuration->addToArrayMapping(Source::class, new ToArrayMapping());

从数组映射

use PHPClassMapper\Configuration\ArrayMapperConfiguration;

$configuration = new ArrayMapperConfiguration();
$configuration->addFromArrayMapping(Destination::class, new FromArrayMapping());

创建映射

由于您可以以两种不同的方式映射数组,因此使用了两个不同的接口

  • ToArrayMappingInterface
  • FromArrayMappingInterface

到数组映射

use PHPClassMapper\ArrayMapperInterface;
use PHPClassMapper\Configuration\ToArrayMappingInterface;

final class ToArrayMapping implements ToArrayMappingInterface
{
    /**
     * Maps an object into an array.
     *
     * @param object $source The class that needs to be mapped to an array.
     * @param array<string, mixed> $contextData An associative array (key => value) that gives the mapper additional
     * data to work with.
     * @param ArrayMapperInterface $mapper An extra mapper instance to map objects with.
     *
     * @throws InvalidArgumentException when a `MyClass::name` string refers to a class that does not exist.
     * @throws MissingMappingException when a mapping between two class types can't be found.
     * @throws MissingContextDataFieldException when a specific context data field can't be found.
     *
     * @return array<mixed>
     */
    public function mapObject(object $source, array $contextData, ArrayMapperInterface $mapper): array;
    {
        return [];
    }
}

从数组映射

use PHPClassMapper\ArrayMapperInterface;
use PHPClassMapper\Configuration\FromArrayMappingInterface;

final class FromArrayMapping implements FromArrayMappingInterface
{
    /**
     * Maps an array into an object.
     *
     * @param array<string, mixed> $source The class that needs to be mapped to a different class.
     * @param array<string, mixed> $contextData An associative array (key => value) that gives the mapper additional
     * data to work with.
     * @param ArrayMapperInterface $mapper An extra mapper instance to map nested arrays with.
     *
     * @throws InvalidArgumentException when a `MyClass::name` string refers to a class that does not exist.
     * @throws MissingMappingException when a mapping between two class types can't be found.
     * @throws MissingContextDataFieldException when a specific context data field can't be found.
     */
    public function mapObject(array $source, array $contextData, ArrayMapperInterface $mapper): object;
    {
        return new Destination();
    }
}

创建映射器

一旦您有了配置,您就可以实例化映射器

use PHPClassMapper\Configuration\ArrayMapperConfiguration;
use PHPClassMapper\ArrayMapper;

$configuration = new ArrayMapperConfiguration();

// Do your configuration logic here...

$mapper = new ArrayMapper($configuration);

使用映射器

一旦实例化了映射器,您可以使用以下方式使用它

use PHPClassMapper\Configuration\ArrayMapperConfiguration;
use PHPClassMapper\ArrayMapper;

$configuration = new ArrayMapperConfiguration();

// Do your configuration logic here...

$mapper = new ArrayMapper($configuration);

// To array mapping
$objToMap = // Your object here...
$mappedArray = $mapper->toArray($objToMap);

// From array mapping
$mappedObject = $mapper->fromArray([], Destination::class);

依赖注入容器

您可以使用MapperInterfaceMapperConfigurationInterface接口将映射器注册到您最喜欢的依赖注入容器中。