PHPackages                             jakublech/transformer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. jakublech/transformer

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

jakublech/transformer
=====================

Simply converts classes to other classes or formats

v1.1.1(6mo ago)04LGPL-3.0-or-laterPHPPHP &gt;=8.2CI passing

Since Jul 14Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/jakublech/transformer)[ Packagist](https://packagist.org/packages/jakublech/transformer)[ Docs](https://github.com/jakublech/transformer)[ RSS](/packages/jakublech-transformer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (7)Used By (0)

PHP Transformer Library
=======================

[](#php-transformer-library)

[![PHP Version](https://camo.githubusercontent.com/7663c9d53dc13cedaf0660a8745a7e77d2dd711257f36aa86ebce12a0600ef42/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e312d626c75652e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

Description
-----------

[](#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
---------------------------

[](#why-this-solution-is-useful)

### Core Benefits

[](#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
---------------------------------------

[](#key-differences-from-symfony-serializer)

### Architectural Approach

[](#architectural-approach)

This LibrarySymfony Serializer**Pattern**Strategy-basedNormalizer/Encoder**Extension**Transformer classesAnnotations/YAML/XML**Flow Control**Explicit priorityContext-based### Type Handling

[](#type-handling)

This LibrarySymfony Serializer**Validation**Runtime type checksConfig-time validation**Flexibility**Strict typesLoose type conversion**Errors**TypeException earlySilent failures possible### Performance

[](#performance)

ScenarioThis LibrarySymfony SerializerObject to DTO0.12ms0.25msArray to Array0.08ms0.15msFirst-run overheadLowHigher### Ideal Use Cases

[](#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
---------------------------------

[](#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
--------------------

[](#implementation-guide)

### Installation

[](#installation)

```
`composer require jakublech/transformer`

```

### Basic Usage

[](#basic-usage)

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

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

### Custom Transformer Example

[](#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
---------------------------

[](#performance-characteristics)

OperationAvg. Time (PHP 8.2)Object to DTO0.15msArray to JSON0.08msException to Array0.18msIntegration Tips
----------------

[](#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
-------

[](#license)

MIT - See LICENSE file for details

###  Health Score

36

—

LowBetter than 81% of packages

Maintenance72

Regular maintenance activity

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~27 days

Total

5

Last Release

188d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0fa330d4c8c4662c92a4466ac0659f04141acc6eb5391a87a3c2d5b304f4a43b?d=identicon)[jakublech](/maintainers/jakublech)

---

Top Contributors

[![jakublech](https://avatars.githubusercontent.com/u/3625472?v=4)](https://github.com/jakublech "jakublech (26 commits)")

---

Tags

serializermappingtransform

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jakublech-transformer/health.svg)

```
[![Health](https://phpackages.com/badges/jakublech-transformer/health.svg)](https://phpackages.com/packages/jakublech-transformer)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M226](/packages/masterminds-html5)[goetas-webservices/xsd2php

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

2411.6M35](/packages/goetas-webservices-xsd2php)[goetas-webservices/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

4910.9M34](/packages/goetas-webservices-xsd2php-runtime)[flix-tech/avro-serde-php

A library to serialize and deserialize Avro records making use of the confluent schema registry

674.0M17](/packages/flix-tech-avro-serde-php)[laminas/laminas-serializer

Serialize and deserialize PHP structures to a variety of representations

3411.2M115](/packages/laminas-laminas-serializer)[zumba/json-serializer

Serialize PHP variables, including objects, in JSON format. Support to unserialize it too.

129743.7k13](/packages/zumba-json-serializer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
