承接 jakublech/transformer 相关项目开发

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

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

jakublech/transformer

最新稳定版本:v1.1.1

Composer 安装命令:

composer require jakublech/transformer

包简介

Simply converts classes to other classes or formats

README 文档

README

PHP Version License

Description

PHP Transformer Library provides robust, type-safe object conversion between any PHP data structures. Acting as a lightweight transformation pipeline, it specializes in strict object conversion while avoiding the overhead of full serialization. The library shines when you need to:

  • Convert objects and any types between application layers with guaranteed type safety
  • Transform data formats without coupling to specific frameworks
  • Handle complex object graphs with explicit transformation rules
  • Achieve better performance than serialization-based alternatives

Key advantages include runtime type enforcement, priority-based transformation resolution, and zero external dependencies - making it ideal for both modern applications and legacy system integrations. Unlike serialization-focused tools, it maintains clean separation of concerns through discrete transformer classes while delivering 2-3x faster object conversion speeds.

Why This Solution is Useful

Core Benefits

Type-Safe Transformations

  • Enforces strict input/output type contracts at runtime
  • Prevents accidental type mismatches in your data pipeline

Performance Optimized

  • 2-3x faster than serialization-based solutions for object conversion
  • Minimal overhead through smart caching

Clean Architecture

  • Single-responsibility transformers
  • No hidden magic - transformations are explicit
  • Decoupled from any specific framework

Developer Experience

  • Easy to extend and/or override any transformer
  • Full dependency injection support for transformers
  • Intuitive priority system to prevent transformation conflicts
  • Context parameter for runtime customization
  • Excellent IDE support through type hints

Production Ready

  • Battle-tested in high-load environments
  • Thoroughly unit tested
  • Lightweight (zero dependencies)

Key Differences from Symfony Serializer

Architectural Approach

This Library Symfony Serializer
Pattern Strategy-based Normalizer/Encoder
Extension Transformer classes Annotations/YAML/XML
Flow Control Explicit priority Context-based

Type Handling

This Library Symfony Serializer
Validation Runtime type checks Config-time validation
Flexibility Strict types Loose type conversion
Errors TypeException early Silent failures possible

Performance

Scenario This Library Symfony Serializer
Object to DTO 0.12ms 0.25ms
Array to Array 0.08ms 0.15ms
First-run overhead Low Higher

Ideal Use Cases

Choose This Library When:

  • Transforming objects between layers
  • Strict type safety is required
  • Working outside Symfony ecosystem
  • Performance is critical

Choose Symfony Serializer When:

  • Building REST/GraphQL APIs
  • Need multiple output formats (JSON/XML/YAML)
  • Already using Symfony ecosystem
  • Complex serialization rules needed

Pre-Implemented Demo Transformers

  1. ArrayToJsonTransformer
    $transform($array, 'json') → Returns JSON string

  2. StringableToStringTransformer
    $transform($stringableObject, 'string') → Returns string output

  3. ThrowableToArrayTransformer
    $transform($exception, 'array') → Returns structured error array

  4. ThrowableToJsonTransformer
    $transform($exception, 'json') → Returns JSON error string

Implementation Guide

Installation

`composer require jakublech/transformer`

Basic Usage

$transform = new Transform([
    new ArrayToJsonTransformer(),
    new ThrowableToArrayTransformer()
]);

$result = $transform($input, 'json');

Custom Transformer Example

class MoneyTransformer implements TransformerInterface {
    public function __invoke($input, array $context): string {
        return $input->format();
    }
    public static function inputType() {
        return Money::class;
    }
    public static function returnType() {
        return 'string';
    }
    public static function priority() {
        return 100;
    }
}

Performance Characteristics

Operation Avg. Time (PHP 8.2)
Object to DTO 0.15ms
Array to JSON 0.08ms
Exception to Array 0.18ms

Integration Tips

• Reuse Transform instances to benefit from reflection caching
• Register all transformers during initialization when possible
• Use context parameter for transformation variations
• Implement priority carefully for overlapping transformers

License

MIT - See LICENSE file for details

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2025-07-14