承接 rekalogika/mapper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

rekalogika/mapper

Composer 安装命令:

composer require rekalogika/mapper

包简介

An object mapper for PHP and Symfony. Maps an object to another object. Primarily used for transforming an entity to a DTO and vice versa.

README 文档

README

rekalogika/mapper is an object mapper for PHP and Symfony, also commonly known as an automapper. It maps an object to another object. Primarily used to map an entity to a DTO, but also useful for other mapping purposes. It removes the complexity of mapping an object to another object, and even an object graph to another object graph.

Full documentation is available at rekalogika.dev/mapper.

Installation

composer require rekalogika/mapper

Usage

use App\Entity\Book;
use Rekalogika\Mapper\MapperInterface;

// map a single object:

/** @var MapperInterface $mapper */
/** @var Book $book */

$result = $mapper->map($book, BookDto::class);

// map a single object to an existing object:

$bookDto = new BookDto();
$mapper->map($book, $bookDto);

// map an iterable of objects:

/** @var IterableMapperInterface $iterableMapper */
/** @var iterable<Book> $books */

$bookDtos = $iterableMapper->mapIterable($books, BookDto::class);

Why Use a Mapper?

Why do we need to use a mapper to save a few keystrokes, and not just use something simple like this?

class BookDto
{
    public static function create(Book $book): self
    {
        $dto = new self();
        // ...

        return $dto;
    }
}

Everyone must have that idea at some point. However, as the project grows, the target classes (DTOs) may start to reference each other, and become a rich object graph. Your code will start to have many special cases, and is no longer as simple as you thought it would be. It becomes harder to maintain, and then eventually forces you to sit back and try to resolve the problem. When (if?) you successfully engineer a solution, you will end up with something that resembles a mapping framework anyway.

Mapping can be simple, but can also become a highly complex task. A mapper is created out of necessity to handle the complexity, not just as a means of saving a few keystrokes.

Features

General

  • Automatically lists the properties of the source and target, detects their types, and maps them accordingly.
  • Reads the type from PHP type declaration and PHPDoc annotations, including the type of the nested objects.
  • Does not attempt to circumvent your class constraints. Reads only from and writes only to public properties, getters, setters. Does not instantiate objects without their constructor.
  • Constructor initialization.
  • Handles nested objects.
  • Handles recursion and circular references.
  • Inheritance support. Maps to abstract classes and interfaces using an inheritance map attribute.
  • Maps to and from stdClass, objects extending stdClass, and other objects with dynamic properties (#[AllowDynamicProperties]).
  • Maps an object to an array, and vice versa.
  • Support for third-party objects: Doctrine Collections, Symfony Uid, Ramsey UUID.

Custom Mapping

  • Override the mapping of a specific property using a custom property mapper.
  • Override the mapping between two specific classes using a custom object mapper.
  • Extend the mapper by creating new transformers, or decorating the existing ones.
  • Match classes using attributes in your transformers, in addition to using class names.
  • Preset mapping. Provide a table of predetermined mappings that the mapper can use.

Object Lazy-Loading

  • If possible, target objects are lazy-loaded. The mapping does not take place until the target is accessed, and will never take place if it is never accessed.
  • Attempts to detect identifier properties on the source side. Those properties will be mapped eagerly to the target side, as they should not trigger the hydration of the source. As an example, API Platform will be able to generate IRIs without causing Doctrine to hydrate the entire object graph.
  • Uses native PHP > 8.4 lazy objects if available, and falls back to symfony/var-exporter if not available.

Arrays and Array-Like Objects

  • Handles the mapping between array or array-like objects.
  • Handles adder and remover methods on the target side.
  • Handles non-string & non-integer keys in array-like objects, including SplObjectStorage.
  • Option to remove existing items from the target if they are not present in the source.

Array-Like Lazy-Loading

  • Lazy loading if the target is type-hinted with ArrayAccess, Traversable or CollectionInterface. The target will not iterate the source object until it is accessed, or never if it is never accessed.
  • Stream mapping. Maps the source members to the target side as they are being iterated. This may consume less memory.
  • With lazy loading, if the source is a Countable, then the target will also be a Countable. With an extra-lazy Doctrine Collection, the consumer will be able to count the target without causing a full hydration of the source.

Development Experience (DX)

  • Helpful exception messages.
  • Console commands for debugging.
  • Data collector and profiler integration.
  • Coded from the start using PHP 8, strict types, and maxed-out PHPStan and Psalm level.

To-Do List

  • Option to read & write to private properties.
  • Migrate engine to symfony/type-info.
  • Auto-detect static factory method.
  • Use our own interface for proxy objects.
  • Improve non-framework usage.
  • Warm up proxies on build time from the list of classes provided by the user.

Documentation

rekalogika.dev/mapper

License

MIT

rekalogika/mapper 适用场景与选型建议

rekalogika/mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 52.58k 次下载、GitHub Stars 达 38, 最近一次更新时间为 2024 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「api」 「proxy」 「mapping」 「mapper」 「dto」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rekalogika/mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rekalogika/mapper 我们能提供哪些服务?
定制开发 / 二次开发

基于 rekalogika/mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 52.58k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 38
  • 点击次数: 28
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 38
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-10